Merge from mainline.
[official-gcc.git] / libjava / classpath / java / nio / ByteBufferImpl.java
blobaa51a65bdd5b02141de565434f4bad58ae68d01b
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)
9 any later version.
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
19 02110-1301 USA.
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.
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. */
39 package java.nio;
41 /**
42 * This is a Heap memory implementation
44 final class ByteBufferImpl extends ByteBuffer
46 private boolean readOnly;
48 ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
50 super (capacity, limit, position, mark);
51 this.backing_buffer = buffer;
52 this.array_offset = offset;
53 this.readOnly = readOnly;
56 public CharBuffer asCharBuffer ()
58 return new CharViewBufferImpl (this, remaining() >> 1);
61 public ShortBuffer asShortBuffer ()
63 return new ShortViewBufferImpl (this, remaining() >> 1);
66 public IntBuffer asIntBuffer ()
68 return new IntViewBufferImpl (this, remaining() >> 2);
71 public LongBuffer asLongBuffer ()
73 return new LongViewBufferImpl (this, remaining() >> 3);
76 public FloatBuffer asFloatBuffer ()
78 return new FloatViewBufferImpl (this, remaining() >> 2);
81 public DoubleBuffer asDoubleBuffer ()
83 return new DoubleViewBufferImpl (this, remaining() >> 3);
86 public boolean isReadOnly ()
88 return readOnly;
91 public ByteBuffer slice ()
93 return new ByteBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ());
96 public ByteBuffer duplicate ()
98 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ());
101 public ByteBuffer asReadOnlyBuffer ()
103 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true);
106 void shiftDown (int dst_offset, int src_offset, int count)
108 System.arraycopy(backing_buffer, array_offset + src_offset,
109 backing_buffer, array_offset + dst_offset,
110 count);
113 public ByteBuffer compact ()
115 checkIfReadOnly();
116 mark = -1;
117 int pos = position();
118 int n = limit() - pos;
119 if (n > 0)
120 shiftDown(0, pos, n);
121 position(n);
122 limit(capacity());
123 return this;
126 public boolean isDirect ()
128 return false;
132 * Reads the <code>byte</code> at this buffer's current position,
133 * and then increments the position.
135 * @exception BufferUnderflowException If there are no remaining
136 * <code>bytes</code> in this buffer.
138 public byte get ()
140 if (pos >= limit)
141 throw new BufferUnderflowException();
143 return backing_buffer [(pos++) + array_offset];
147 * Bulk get
149 public ByteBuffer get (byte[] dst, int offset, int length)
151 checkArraySize(dst.length, offset, length);
152 if ( (limit - pos) < length) // check for overflow
153 throw new BufferUnderflowException();
155 System.arraycopy(backing_buffer, pos + array_offset,
156 dst, offset, length);
157 pos += length;
159 return this;
163 * Relative bulk put(), overloads the ByteBuffer impl.
165 public ByteBuffer put (byte[] src, int offset, int length)
167 if ( (limit - pos) < length) // check for overflow
168 throw new BufferOverflowException();
169 checkArraySize(src.length, offset, length);
171 System.arraycopy(src, offset, backing_buffer, pos + array_offset, length);
172 pos += length;
174 return this;
178 * Relative put method. Writes <code>value</code> to the next position
179 * in the buffer.
181 * @exception BufferOverflowException If there is no remaining
182 * space in this buffer.
183 * @exception ReadOnlyBufferException If this buffer is read-only.
185 public ByteBuffer put (byte value)
187 if (readOnly)
188 throw new ReadOnlyBufferException();
189 if (pos >= limit)
190 throw new BufferOverflowException();
192 backing_buffer [(pos++) + array_offset] = value;
193 return this;
197 * Absolute get method. Reads the <code>byte</code> at position
198 * <code>index</code>.
200 * @exception IndexOutOfBoundsException If index is negative or not smaller
201 * than the buffer's limit.
203 public byte get (int index)
205 checkIndex(index);
207 return backing_buffer [index + array_offset];
211 * Absolute put method. Writes <code>value</code> to position
212 * <code>index</code> in the buffer.
214 * @exception IndexOutOfBoundsException If index is negative or not smaller
215 * than the buffer's limit.
216 * @exception ReadOnlyBufferException If this buffer is read-only.
218 public ByteBuffer put (int index, byte value)
220 checkIfReadOnly();
221 checkIndex(index);
223 backing_buffer [index + array_offset] = value;
224 return this;
227 public char getChar ()
229 return ByteBufferHelper.getChar(this, order());
232 public ByteBuffer putChar (char value)
234 if (readOnly)
235 throw new ReadOnlyBufferException ();
236 if ( (limit-pos) < 2)
237 throw new BufferOverflowException();
239 if (endian == ByteOrder.LITTLE_ENDIAN)
241 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
242 backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
244 else
246 backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
247 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
249 return this;
252 public char getChar (int index)
254 return ByteBufferHelper.getChar(this, index, order());
257 public ByteBuffer putChar (int index, char value)
259 ByteBufferHelper.putChar(this, index, value, order());
260 return this;
263 public short getShort ()
265 return ByteBufferHelper.getShort(this, order());
268 public ByteBuffer putShort (short value)
270 ByteBufferHelper.putShort(this, value, order());
271 return this;
274 public short getShort (int index)
276 return ByteBufferHelper.getShort(this, index, order());
279 public ByteBuffer putShort (int index, short value)
281 ByteBufferHelper.putShort(this, index, value, order());
282 return this;
285 public int getInt ()
287 return ByteBufferHelper.getInt(this, order());
290 public ByteBuffer putInt (int value)
292 ByteBufferHelper.putInt(this, value, order());
293 return this;
296 public int getInt (int index)
298 return ByteBufferHelper.getInt(this, index, order());
301 public ByteBuffer putInt (int index, int value)
303 ByteBufferHelper.putInt(this, index, value, order());
304 return this;
307 public long getLong ()
309 return ByteBufferHelper.getLong(this, order());
312 public ByteBuffer putLong (long value)
314 ByteBufferHelper.putLong (this, value, order());
315 return this;
318 public long getLong (int index)
320 return ByteBufferHelper.getLong (this, index, order());
323 public ByteBuffer putLong (int index, long value)
325 ByteBufferHelper.putLong (this, index, value, order());
326 return this;
329 public float getFloat ()
331 return ByteBufferHelper.getFloat (this, order());
334 public ByteBuffer putFloat (float value)
336 ByteBufferHelper.putFloat (this, value, order());
337 return this;
340 public float getFloat (int index)
342 return ByteBufferHelper.getFloat (this, index, order());
345 public ByteBuffer putFloat (int index, float value)
347 ByteBufferHelper.putFloat (this, index, value, order());
348 return this;
351 public double getDouble ()
353 return ByteBufferHelper.getDouble (this, order());
356 public ByteBuffer putDouble (double value)
358 ByteBufferHelper.putDouble (this, value, order());
359 return this;
362 public double getDouble (int index)
364 return ByteBufferHelper.getDouble (this, index, order());
367 public ByteBuffer putDouble (int index, double value)
369 ByteBufferHelper.putDouble (this, index, value, order());
370 return this;