HeapCharBuffer.java revision 53df6f7c7727b8ba0e130ca9e05adb20c0e0d173
1/* 2 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package java.nio; 27 28/** 29 * A read/write HeapCharBuffer. 30 */ 31 32class HeapCharBuffer extends CharBuffer { 33 34 // For speed these fields are actually declared in X-Buffer; 35 // these declarations are here as documentation 36 /* 37 38 protected final char[] hb; 39 protected final int offset; 40 41 */ 42 43 private final boolean isReadOnly; 44 45 HeapCharBuffer(int cap, int lim) { // package-private 46 this(cap, lim, false); 47 } 48 49 HeapCharBuffer(int cap, int lim, boolean isReadOnly) { // package-private 50 super(-1, 0, lim, cap, new char[cap], 0); 51 this.isReadOnly = isReadOnly; 52 } 53 54 HeapCharBuffer(char[] buf, int off, int len) { // package-private 55 this(buf, off, len, false); 56 } 57 58 HeapCharBuffer(char[] buf, int off, int len, boolean isReadOnly) { // package-private 59 super(-1, off, off + len, buf.length, buf, 0); 60 this.isReadOnly = isReadOnly; 61 } 62 63 protected HeapCharBuffer(char[] buf, 64 int mark, int pos, int lim, int cap, 65 int off) { 66 this(buf, mark, pos, lim, cap, off, false); 67 } 68 69 protected HeapCharBuffer(char[] buf, 70 int mark, int pos, int lim, int cap, 71 int off, boolean isReadOnly) { 72 super(mark, pos, lim, cap, buf, off); 73 this.isReadOnly = isReadOnly; 74 } 75 76 public CharBuffer slice() { 77 return new HeapCharBuffer(hb, 78 -1, 79 0, 80 this.remaining(), 81 this.remaining(), 82 this.position() + offset, 83 isReadOnly); 84 } 85 86 public CharBuffer duplicate() { 87 return new HeapCharBuffer(hb, 88 this.markValue(), 89 this.position(), 90 this.limit(), 91 this.capacity(), 92 offset, 93 isReadOnly); 94 } 95 96 public CharBuffer asReadOnlyBuffer() { 97 98 return new HeapCharBuffer(hb, 99 this.markValue(), 100 this.position(), 101 this.limit(), 102 this.capacity(), 103 offset, 104 true); 105 } 106 107 protected int ix(int i) { 108 return i + offset; 109 } 110 111 public char get() { 112 return hb[ix(nextGetIndex())]; 113 } 114 115 public char get(int i) { 116 return hb[ix(checkIndex(i))]; 117 } 118 119 public CharBuffer get(char[] dst, int offset, int length) { 120 checkBounds(offset, length, dst.length); 121 if (length > remaining()) 122 throw new BufferUnderflowException(); 123 System.arraycopy(hb, ix(position()), dst, offset, length); 124 position(position() + length); 125 return this; 126 } 127 128 public boolean isDirect() { 129 return false; 130 } 131 132 public boolean isReadOnly() { 133 return isReadOnly; 134 } 135 136 public CharBuffer put(char x) { 137 if (isReadOnly) { 138 throw new ReadOnlyBufferException(); 139 } 140 hb[ix(nextPutIndex())] = x; 141 return this; 142 } 143 144 public CharBuffer put(int i, char x) { 145 if (isReadOnly) { 146 throw new ReadOnlyBufferException(); 147 } 148 hb[ix(checkIndex(i))] = x; 149 return this; 150 } 151 152 public CharBuffer put(char[] src, int offset, int length) { 153 if (isReadOnly) { 154 throw new ReadOnlyBufferException(); 155 } 156 checkBounds(offset, length, src.length); 157 if (length > remaining()) 158 throw new BufferOverflowException(); 159 System.arraycopy(src, offset, hb, ix(position()), length); 160 position(position() + length); 161 return this; 162 } 163 164 public CharBuffer put(CharBuffer src) { 165 if (isReadOnly) { 166 throw new ReadOnlyBufferException(); 167 } 168 if (src instanceof HeapCharBuffer) { 169 if (src == this) 170 throw new IllegalArgumentException(); 171 HeapCharBuffer sb = (HeapCharBuffer)src; 172 int n = sb.remaining(); 173 if (n > remaining()) 174 throw new BufferOverflowException(); 175 System.arraycopy(sb.hb, sb.ix(sb.position()), 176 hb, ix(position()), n); 177 sb.position(sb.position() + n); 178 position(position() + n); 179 } else if (src.isDirect()) { 180 int n = src.remaining(); 181 if (n > remaining()) 182 throw new BufferOverflowException(); 183 src.get(hb, ix(position()), n); 184 position(position() + n); 185 } else { 186 super.put(src); 187 } 188 return this; 189 } 190 191 public CharBuffer compact() { 192 if (isReadOnly) { 193 throw new ReadOnlyBufferException(); 194 } 195 System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); 196 position(remaining()); 197 limit(capacity()); 198 discardMark(); 199 return this; 200 } 201 202 String toString(int start, int end) { // package-private 203 try { 204 return new String(hb, start + offset, end - start); 205 } catch (StringIndexOutOfBoundsException x) { 206 throw new IndexOutOfBoundsException(); 207 } 208 } 209 210 public CharBuffer subSequence(int start, int end) { 211 if ((start < 0) 212 || (end > length()) 213 || (start > end)) 214 throw new IndexOutOfBoundsException(); 215 int pos = position(); 216 return new HeapCharBuffer(hb, 217 -1, 218 pos + start, 219 pos + end, 220 capacity(), 221 offset, isReadOnly); 222 } 223 224 public ByteOrder order() { 225 return ByteOrder.nativeOrder(); 226 } 227} 228