Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / nio / ByteBufferImpl.java
blobd9f24627de0f419146d16dc4b5d4487aba400880
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 if (pos > 0)
120 int count = remaining();
121 shiftDown(0, pos, count);
122 position(count);
123 limit(capacity());
125 else
127 position(limit());
128 limit(capacity());
130 return this;
133 public boolean isDirect ()
135 return false;
139 * Reads the <code>byte</code> at this buffer's current position,
140 * and then increments the position.
142 * @exception BufferUnderflowException If there are no remaining
143 * <code>bytes</code> in this buffer.
145 public byte get ()
147 checkForUnderflow();
149 byte result = backing_buffer [position () + array_offset];
150 position (position () + 1);
151 return result;
155 * Relative put method. Writes <code>value</code> to the next position
156 * in the buffer.
158 * @exception BufferOverflowException If there is no remaining
159 * space in this buffer.
160 * @exception ReadOnlyBufferException If this buffer is read-only.
162 public ByteBuffer put (byte value)
164 checkIfReadOnly();
165 checkForOverflow();
167 int pos = position();
168 backing_buffer [pos + array_offset] = value;
169 position (pos + 1);
170 return this;
174 * Absolute get method. Reads the <code>byte</code> at position
175 * <code>index</code>.
177 * @exception IndexOutOfBoundsException If index is negative or not smaller
178 * than the buffer's limit.
180 public byte get (int index)
182 checkIndex(index);
184 return backing_buffer [index + array_offset];
188 * Absolute put method. Writes <code>value</code> to position
189 * <code>index</code> in the buffer.
191 * @exception IndexOutOfBoundsException If index is negative or not smaller
192 * than the buffer's limit.
193 * @exception ReadOnlyBufferException If this buffer is read-only.
195 public ByteBuffer put (int index, byte value)
197 checkIfReadOnly();
198 checkIndex(index);
200 backing_buffer [index + array_offset] = value;
201 return this;
204 public char getChar ()
206 return ByteBufferHelper.getChar(this, order());
209 public ByteBuffer putChar (char value)
211 ByteBufferHelper.putChar(this, value, order());
212 return this;
215 public char getChar (int index)
217 return ByteBufferHelper.getChar(this, index, order());
220 public ByteBuffer putChar (int index, char value)
222 ByteBufferHelper.putChar(this, index, value, order());
223 return this;
226 public short getShort ()
228 return ByteBufferHelper.getShort(this, order());
231 public ByteBuffer putShort (short value)
233 ByteBufferHelper.putShort(this, value, order());
234 return this;
237 public short getShort (int index)
239 return ByteBufferHelper.getShort(this, index, order());
242 public ByteBuffer putShort (int index, short value)
244 ByteBufferHelper.putShort(this, index, value, order());
245 return this;
248 public int getInt ()
250 return ByteBufferHelper.getInt(this, order());
253 public ByteBuffer putInt (int value)
255 ByteBufferHelper.putInt(this, value, order());
256 return this;
259 public int getInt (int index)
261 return ByteBufferHelper.getInt(this, index, order());
264 public ByteBuffer putInt (int index, int value)
266 ByteBufferHelper.putInt(this, index, value, order());
267 return this;
270 public long getLong ()
272 return ByteBufferHelper.getLong(this, order());
275 public ByteBuffer putLong (long value)
277 ByteBufferHelper.putLong (this, value, order());
278 return this;
281 public long getLong (int index)
283 return ByteBufferHelper.getLong (this, index, order());
286 public ByteBuffer putLong (int index, long value)
288 ByteBufferHelper.putLong (this, index, value, order());
289 return this;
292 public float getFloat ()
294 return ByteBufferHelper.getFloat (this, order());
297 public ByteBuffer putFloat (float value)
299 ByteBufferHelper.putFloat (this, value, order());
300 return this;
303 public float getFloat (int index)
305 return ByteBufferHelper.getFloat (this, index, order());
308 public ByteBuffer putFloat (int index, float value)
310 ByteBufferHelper.putFloat (this, index, value, order());
311 return this;
314 public double getDouble ()
316 return ByteBufferHelper.getDouble (this, order());
319 public ByteBuffer putDouble (double value)
321 ByteBufferHelper.putDouble (this, value, order());
322 return this;
325 public double getDouble (int index)
327 return ByteBufferHelper.getDouble (this, index, order());
330 public ByteBuffer putDouble (int index, double value)
332 ByteBufferHelper.putDouble (this, index, value, order());
333 return this;