1 /* ByteBufferImpl.java --
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
42 * This is a Heap memory implementation
44 final class ByteBufferImpl
extends ByteBuffer
46 private final boolean readOnly
;
48 ByteBufferImpl (byte[] buffer
, int offset
, int capacity
, int limit
,
49 int position
, int mark
, boolean readOnly
)
51 super (capacity
, limit
, position
, mark
, null, buffer
, offset
);
52 this.readOnly
= readOnly
;
55 public CharBuffer
asCharBuffer ()
57 return new CharViewBufferImpl (this, remaining() >> 1);
60 public ShortBuffer
asShortBuffer ()
62 return new ShortViewBufferImpl (this, remaining() >> 1);
65 public IntBuffer
asIntBuffer ()
67 return new IntViewBufferImpl (this, remaining() >> 2);
70 public LongBuffer
asLongBuffer ()
72 return new LongViewBufferImpl (this, remaining() >> 3);
75 public FloatBuffer
asFloatBuffer ()
77 return new FloatViewBufferImpl (this, remaining() >> 2);
80 public DoubleBuffer
asDoubleBuffer ()
82 return new DoubleViewBufferImpl (this, remaining() >> 3);
85 public boolean isReadOnly ()
90 public ByteBuffer
slice ()
92 return new ByteBufferImpl (backing_buffer
, array_offset
+ position (),
93 remaining (), remaining (), 0, -1, isReadOnly ());
96 public ByteBuffer
duplicate ()
98 return new ByteBufferImpl (backing_buffer
, array_offset
, capacity (),
99 limit (), position (), mark
, isReadOnly ());
102 public ByteBuffer
asReadOnlyBuffer ()
104 return new ByteBufferImpl (backing_buffer
, array_offset
, capacity (),
105 limit (), position (), mark
, true);
108 void shiftDown (int dst_offset
, int src_offset
, int count
)
110 System
.arraycopy(backing_buffer
, array_offset
+ src_offset
,
111 backing_buffer
, array_offset
+ dst_offset
,
115 public ByteBuffer
compact ()
119 int pos
= position();
120 int n
= limit() - pos
;
122 shiftDown(0, pos
, n
);
128 public boolean isDirect ()
134 * Reads the <code>byte</code> at this buffer's current position,
135 * and then increments the position.
137 * @exception BufferUnderflowException If there are no remaining
138 * <code>bytes</code> in this buffer.
143 throw new BufferUnderflowException();
145 return backing_buffer
[(pos
++) + array_offset
];
151 public ByteBuffer
get (byte[] dst
, int offset
, int length
)
153 checkArraySize(dst
.length
, offset
, length
);
154 if ( (limit
- pos
) < length
) // check for overflow
155 throw new BufferUnderflowException();
157 System
.arraycopy(backing_buffer
, pos
+ array_offset
,
158 dst
, offset
, length
);
165 * Relative bulk put(), overloads the ByteBuffer impl.
167 public ByteBuffer
put (byte[] src
, int offset
, int length
)
169 if ( (limit
- pos
) < length
) // check for overflow
170 throw new BufferOverflowException();
171 checkArraySize(src
.length
, offset
, length
);
173 System
.arraycopy(src
, offset
, backing_buffer
, pos
+ array_offset
, length
);
180 * Relative put method. Writes <code>value</code> to the next position
183 * @exception BufferOverflowException If there is no remaining
184 * space in this buffer.
185 * @exception ReadOnlyBufferException If this buffer is read-only.
187 public ByteBuffer
put (byte value
)
190 throw new ReadOnlyBufferException();
192 throw new BufferOverflowException();
194 backing_buffer
[(pos
++) + array_offset
] = value
;
199 * Absolute get method. Reads the <code>byte</code> at position
200 * <code>index</code>.
202 * @exception IndexOutOfBoundsException If index is negative or not smaller
203 * than the buffer's limit.
205 public byte get (int index
)
209 return backing_buffer
[index
+ array_offset
];
213 * Absolute put method. Writes <code>value</code> to position
214 * <code>index</code> in the buffer.
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.
220 public ByteBuffer
put (int index
, byte value
)
225 backing_buffer
[index
+ array_offset
] = value
;
229 public char getChar ()
231 return ByteBufferHelper
.getChar(this, order());
234 public ByteBuffer
putChar (char value
)
237 throw new ReadOnlyBufferException ();
238 if ( (limit
-pos
) < 2)
239 throw new BufferOverflowException();
241 if (endian
== ByteOrder
.LITTLE_ENDIAN
)
243 backing_buffer
[(pos
++) + array_offset
] = (byte)(value
&0xFF);
244 backing_buffer
[(pos
++) + array_offset
] = (byte)(value
>>8);
248 backing_buffer
[(pos
++) + array_offset
] = (byte)(value
>>8);
249 backing_buffer
[(pos
++) + array_offset
] = (byte)(value
&0xFF);
254 public char getChar (int index
)
256 return ByteBufferHelper
.getChar(this, index
, order());
259 public ByteBuffer
putChar (int index
, char value
)
261 ByteBufferHelper
.putChar(this, index
, value
, order());
265 public short getShort ()
267 return ByteBufferHelper
.getShort(this, order());
270 public ByteBuffer
putShort (short value
)
272 ByteBufferHelper
.putShort(this, value
, order());
276 public short getShort (int index
)
278 return ByteBufferHelper
.getShort(this, index
, order());
281 public ByteBuffer
putShort (int index
, short value
)
283 ByteBufferHelper
.putShort(this, index
, value
, order());
289 return ByteBufferHelper
.getInt(this, order());
292 public ByteBuffer
putInt (int value
)
294 ByteBufferHelper
.putInt(this, value
, order());
298 public int getInt (int index
)
300 return ByteBufferHelper
.getInt(this, index
, order());
303 public ByteBuffer
putInt (int index
, int value
)
305 ByteBufferHelper
.putInt(this, index
, value
, order());
309 public long getLong ()
311 return ByteBufferHelper
.getLong(this, order());
314 public ByteBuffer
putLong (long value
)
316 ByteBufferHelper
.putLong (this, value
, order());
320 public long getLong (int index
)
322 return ByteBufferHelper
.getLong (this, index
, order());
325 public ByteBuffer
putLong (int index
, long value
)
327 ByteBufferHelper
.putLong (this, index
, value
, order());
331 public float getFloat ()
333 return ByteBufferHelper
.getFloat (this, order());
336 public ByteBuffer
putFloat (float value
)
338 ByteBufferHelper
.putFloat (this, value
, order());
342 public float getFloat (int index
)
344 return ByteBufferHelper
.getFloat (this, index
, order());
347 public ByteBuffer
putFloat (int index
, float value
)
349 ByteBufferHelper
.putFloat (this, index
, value
, order());
353 public double getDouble ()
355 return ByteBufferHelper
.getDouble (this, order());
358 public ByteBuffer
putDouble (double value
)
360 ByteBufferHelper
.putDouble (this, value
, order());
364 public double getDouble (int index
)
366 return ByteBufferHelper
.getDouble (this, index
, order());
369 public ByteBuffer
putDouble (int index
, double value
)
371 ByteBufferHelper
.putDouble (this, index
, value
, order());