2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libjava / classpath / java / nio / ByteBufferImpl.java
blob2bf319220660a2c7a92cf53b911de728919e5496
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 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 ()
87 return readOnly;
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,
112 count);
115 public ByteBuffer compact ()
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;
128 public boolean isDirect ()
130 return false;
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.
140 public byte get ()
142 if (pos >= limit)
143 throw new BufferUnderflowException();
145 return backing_buffer [(pos++) + array_offset];
149 * Bulk get
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);
159 pos += length;
161 return this;
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);
174 pos += length;
176 return this;
180 * Relative put method. Writes <code>value</code> to the next position
181 * in the buffer.
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)
189 if (readOnly)
190 throw new ReadOnlyBufferException();
191 if (pos >= limit)
192 throw new BufferOverflowException();
194 backing_buffer [(pos++) + array_offset] = value;
195 return this;
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)
207 checkIndex(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)
222 checkIfReadOnly();
223 checkIndex(index);
225 backing_buffer [index + array_offset] = value;
226 return this;
229 public char getChar ()
231 return ByteBufferHelper.getChar(this, order());
234 public ByteBuffer putChar (char value)
236 if (readOnly)
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);
246 else
248 backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
249 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
251 return this;
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());
262 return this;
265 public short getShort ()
267 return ByteBufferHelper.getShort(this, order());
270 public ByteBuffer putShort (short value)
272 ByteBufferHelper.putShort(this, value, order());
273 return this;
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());
284 return this;
287 public int getInt ()
289 return ByteBufferHelper.getInt(this, order());
292 public ByteBuffer putInt (int value)
294 ByteBufferHelper.putInt(this, value, order());
295 return this;
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());
306 return this;
309 public long getLong ()
311 return ByteBufferHelper.getLong(this, order());
314 public ByteBuffer putLong (long value)
316 ByteBufferHelper.putLong (this, value, order());
317 return this;
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());
328 return this;
331 public float getFloat ()
333 return ByteBufferHelper.getFloat (this, order());
336 public ByteBuffer putFloat (float value)
338 ByteBufferHelper.putFloat (this, value, order());
339 return this;
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());
350 return this;
353 public double getDouble ()
355 return ByteBufferHelper.getDouble (this, order());
358 public ByteBuffer putDouble (double value)
360 ByteBufferHelper.putDouble (this, value, order());
361 return this;
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());
372 return this;