1 /* ByteBufferImpl.java -- 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package java.nio; 40 41 /** 42 * This is a Heap memory implementation 43 */ 44 final class ByteBufferImpl extends ByteBuffer 45 { 46 private final boolean readOnly; 47 ByteBufferImpl(byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)48 ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, 49 int position, int mark, boolean readOnly) 50 { 51 super (capacity, limit, position, mark, null, buffer, offset); 52 this.readOnly = readOnly; 53 } 54 asCharBuffer()55 public CharBuffer asCharBuffer () 56 { 57 return new CharViewBufferImpl (this, remaining() >> 1); 58 } 59 asShortBuffer()60 public ShortBuffer asShortBuffer () 61 { 62 return new ShortViewBufferImpl (this, remaining() >> 1); 63 } 64 asIntBuffer()65 public IntBuffer asIntBuffer () 66 { 67 return new IntViewBufferImpl (this, remaining() >> 2); 68 } 69 asLongBuffer()70 public LongBuffer asLongBuffer () 71 { 72 return new LongViewBufferImpl (this, remaining() >> 3); 73 } 74 asFloatBuffer()75 public FloatBuffer asFloatBuffer () 76 { 77 return new FloatViewBufferImpl (this, remaining() >> 2); 78 } 79 asDoubleBuffer()80 public DoubleBuffer asDoubleBuffer () 81 { 82 return new DoubleViewBufferImpl (this, remaining() >> 3); 83 } 84 isReadOnly()85 public boolean isReadOnly () 86 { 87 return readOnly; 88 } 89 slice()90 public ByteBuffer slice () 91 { 92 return new ByteBufferImpl (backing_buffer, array_offset + position (), 93 remaining (), remaining (), 0, -1, isReadOnly ()); 94 } 95 duplicate()96 public ByteBuffer duplicate () 97 { 98 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), 99 limit (), position (), mark, isReadOnly ()); 100 } 101 asReadOnlyBuffer()102 public ByteBuffer asReadOnlyBuffer () 103 { 104 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), 105 limit (), position (), mark, true); 106 } 107 shiftDown(int dst_offset, int src_offset, int count)108 void shiftDown (int dst_offset, int src_offset, int count) 109 { 110 System.arraycopy(backing_buffer, array_offset + src_offset, 111 backing_buffer, array_offset + dst_offset, 112 count); 113 } 114 compact()115 public ByteBuffer compact () 116 { 117 checkIfReadOnly(); 118 mark = -1; 119 int pos = position(); 120 int n = limit() - pos; 121 if (n > 0) 122 shiftDown(0, pos, n); 123 position(n); 124 limit(capacity()); 125 return this; 126 } 127 isDirect()128 public boolean isDirect () 129 { 130 return false; 131 } 132 133 /** 134 * Reads the <code>byte</code> at this buffer's current position, 135 * and then increments the position. 136 * 137 * @exception BufferUnderflowException If there are no remaining 138 * <code>bytes</code> in this buffer. 139 */ get()140 public byte get () 141 { 142 if (pos >= limit) 143 throw new BufferUnderflowException(); 144 145 return backing_buffer [(pos++) + array_offset]; 146 } 147 148 /** 149 * Bulk get 150 */ get(byte[] dst, int offset, int length)151 public ByteBuffer get (byte[] dst, int offset, int length) 152 { 153 checkArraySize(dst.length, offset, length); 154 if ( (limit - pos) < length) // check for overflow 155 throw new BufferUnderflowException(); 156 157 System.arraycopy(backing_buffer, pos + array_offset, 158 dst, offset, length); 159 pos += length; 160 161 return this; 162 } 163 164 /** 165 * Relative bulk put(), overloads the ByteBuffer impl. 166 */ put(byte[] src, int offset, int length)167 public ByteBuffer put (byte[] src, int offset, int length) 168 { 169 if ( (limit - pos) < length) // check for overflow 170 throw new BufferOverflowException(); 171 checkArraySize(src.length, offset, length); 172 173 System.arraycopy(src, offset, backing_buffer, pos + array_offset, length); 174 pos += length; 175 176 return this; 177 } 178 179 /** 180 * Relative put method. Writes <code>value</code> to the next position 181 * in the buffer. 182 * 183 * @exception BufferOverflowException If there is no remaining 184 * space in this buffer. 185 * @exception ReadOnlyBufferException If this buffer is read-only. 186 */ put(byte value)187 public ByteBuffer put (byte value) 188 { 189 if (readOnly) 190 throw new ReadOnlyBufferException(); 191 if (pos >= limit) 192 throw new BufferOverflowException(); 193 194 backing_buffer [(pos++) + array_offset] = value; 195 return this; 196 } 197 198 /** 199 * Absolute get method. Reads the <code>byte</code> at position 200 * <code>index</code>. 201 * 202 * @exception IndexOutOfBoundsException If index is negative or not smaller 203 * than the buffer's limit. 204 */ get(int index)205 public byte get (int index) 206 { 207 checkIndex(index); 208 209 return backing_buffer [index + array_offset]; 210 } 211 212 /** 213 * Absolute put method. Writes <code>value</code> to position 214 * <code>index</code> in the buffer. 215 * 216 * @exception IndexOutOfBoundsException If index is negative or not smaller 217 * than the buffer's limit. 218 * @exception ReadOnlyBufferException If this buffer is read-only. 219 */ put(int index, byte value)220 public ByteBuffer put (int index, byte value) 221 { 222 checkIfReadOnly(); 223 checkIndex(index); 224 225 backing_buffer [index + array_offset] = value; 226 return this; 227 } 228 getChar()229 public char getChar () 230 { 231 return ByteBufferHelper.getChar(this, order()); 232 } 233 putChar(char value)234 public ByteBuffer putChar (char value) 235 { 236 if (readOnly) 237 throw new ReadOnlyBufferException (); 238 if ( (limit-pos) < 2) 239 throw new BufferOverflowException(); 240 241 if (endian == ByteOrder.LITTLE_ENDIAN) 242 { 243 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF); 244 backing_buffer [(pos++) + array_offset] = (byte)(value>>8); 245 } 246 else 247 { 248 backing_buffer [(pos++) + array_offset] = (byte)(value>>8); 249 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF); 250 } 251 return this; 252 } 253 getChar(int index)254 public char getChar (int index) 255 { 256 return ByteBufferHelper.getChar(this, index, order()); 257 } 258 putChar(int index, char value)259 public ByteBuffer putChar (int index, char value) 260 { 261 ByteBufferHelper.putChar(this, index, value, order()); 262 return this; 263 } 264 getShort()265 public short getShort () 266 { 267 return ByteBufferHelper.getShort(this, order()); 268 } 269 putShort(short value)270 public ByteBuffer putShort (short value) 271 { 272 ByteBufferHelper.putShort(this, value, order()); 273 return this; 274 } 275 getShort(int index)276 public short getShort (int index) 277 { 278 return ByteBufferHelper.getShort(this, index, order()); 279 } 280 putShort(int index, short value)281 public ByteBuffer putShort (int index, short value) 282 { 283 ByteBufferHelper.putShort(this, index, value, order()); 284 return this; 285 } 286 getInt()287 public int getInt () 288 { 289 return ByteBufferHelper.getInt(this, order()); 290 } 291 putInt(int value)292 public ByteBuffer putInt (int value) 293 { 294 ByteBufferHelper.putInt(this, value, order()); 295 return this; 296 } 297 getInt(int index)298 public int getInt (int index) 299 { 300 return ByteBufferHelper.getInt(this, index, order()); 301 } 302 putInt(int index, int value)303 public ByteBuffer putInt (int index, int value) 304 { 305 ByteBufferHelper.putInt(this, index, value, order()); 306 return this; 307 } 308 getLong()309 public long getLong () 310 { 311 return ByteBufferHelper.getLong(this, order()); 312 } 313 putLong(long value)314 public ByteBuffer putLong (long value) 315 { 316 ByteBufferHelper.putLong (this, value, order()); 317 return this; 318 } 319 getLong(int index)320 public long getLong (int index) 321 { 322 return ByteBufferHelper.getLong (this, index, order()); 323 } 324 putLong(int index, long value)325 public ByteBuffer putLong (int index, long value) 326 { 327 ByteBufferHelper.putLong (this, index, value, order()); 328 return this; 329 } 330 getFloat()331 public float getFloat () 332 { 333 return ByteBufferHelper.getFloat (this, order()); 334 } 335 putFloat(float value)336 public ByteBuffer putFloat (float value) 337 { 338 ByteBufferHelper.putFloat (this, value, order()); 339 return this; 340 } 341 getFloat(int index)342 public float getFloat (int index) 343 { 344 return ByteBufferHelper.getFloat (this, index, order()); 345 } 346 putFloat(int index, float value)347 public ByteBuffer putFloat (int index, float value) 348 { 349 ByteBufferHelper.putFloat (this, index, value, order()); 350 return this; 351 } 352 getDouble()353 public double getDouble () 354 { 355 return ByteBufferHelper.getDouble (this, order()); 356 } 357 putDouble(double value)358 public ByteBuffer putDouble (double value) 359 { 360 ByteBufferHelper.putDouble (this, value, order()); 361 return this; 362 } 363 getDouble(int index)364 public double getDouble (int index) 365 { 366 return ByteBufferHelper.getDouble (this, index, order()); 367 } 368 putDouble(int index, double value)369 public ByteBuffer putDouble (int index, double value) 370 { 371 ByteBufferHelper.putDouble (this, index, value, order()); 372 return this; 373 } 374 } 375