This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / java / nio / ByteBufferImpl.java
blob6a3814862c986fcf21994001343903b96e6feeff
1 /* ByteBufferImpl.java --
2 Copyright (C) 2002, 2003, 2004 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 (int capacity)
50 this (new byte [capacity], 0, capacity, capacity, 0, -1, false);
53 ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
55 super (buffer, offset, capacity, limit, position, mark);
56 this.readOnly = readOnly;
59 public CharBuffer asCharBuffer ()
61 return new CharViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
64 public ShortBuffer asShortBuffer ()
66 return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
69 public IntBuffer asIntBuffer ()
71 return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
74 public LongBuffer asLongBuffer ()
76 return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
79 public FloatBuffer asFloatBuffer ()
81 return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
84 public DoubleBuffer asDoubleBuffer ()
86 return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly (), order());
89 public boolean isReadOnly ()
91 return readOnly;
94 public ByteBuffer slice ()
96 return new ByteBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ());
99 public ByteBuffer duplicate ()
101 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ());
104 public ByteBuffer asReadOnlyBuffer ()
106 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true);
109 public ByteBuffer compact ()
111 int pos = position();
112 if (pos > 0)
114 int count = remaining();
115 shiftDown(0, pos, count);
116 position(count);
117 limit(capacity());
119 return this;
122 public boolean isDirect ()
124 return false;
128 * Relative get method. Reads the next <code>byte</code> from the buffer.
130 final public byte get ()
132 byte result = backing_buffer [position ()];
133 position (position () + 1);
134 return result;
138 * Relative put method. Writes <code>value</code> to the next position
139 * in the buffer.
141 * @exception ReadOnlyBufferException If this buffer is read-only.
143 final public ByteBuffer put (byte value)
145 if (readOnly)
146 throw new ReadOnlyBufferException ();
148 backing_buffer [position ()] = value;
149 position (position () + 1);
150 return this;
154 * Absolute get method. Reads the <code>byte</code> at position
155 * <code>index</code>.
157 * @exception IndexOutOfBoundsException If index is negative or not smaller
158 * than the buffer's limit.
160 final public byte get (int index)
162 return backing_buffer [index];
166 * Absolute put method. Writes <code>value</value> to position
167 * <code>index</code> in the buffer.
169 * @exception IndexOutOfBoundsException If index is negative or not smaller
170 * than the buffer's limit.
171 * @exception ReadOnlyBufferException If this buffer is read-only.
173 final public ByteBuffer put (int index, byte value)
175 if (readOnly)
176 throw new ReadOnlyBufferException ();
178 backing_buffer [index] = value;
179 return this;
182 final public char getChar ()
184 return ByteBufferHelper.getChar(this, order());
187 final public ByteBuffer putChar (char value)
189 ByteBufferHelper.putChar(this, value, order());
190 return this;
193 final public char getChar (int index)
195 return ByteBufferHelper.getChar(this, index, order());
198 final public ByteBuffer putChar (int index, char value)
200 ByteBufferHelper.putChar(this, index, value, order());
201 return this;
204 final public short getShort ()
206 return ByteBufferHelper.getShort(this, order());
209 final public ByteBuffer putShort (short value)
211 ByteBufferHelper.putShort(this, value, order());
212 return this;
215 final public short getShort (int index)
217 return ByteBufferHelper.getShort(this, index, order());
220 final public ByteBuffer putShort (int index, short value)
222 ByteBufferHelper.putShort(this, index, value, order());
223 return this;
226 final public int getInt ()
228 return ByteBufferHelper.getInt(this, order());
231 final public ByteBuffer putInt (int value)
233 ByteBufferHelper.putInt(this, value, order());
234 return this;
237 final public int getInt (int index)
239 return ByteBufferHelper.getInt(this, index, order());
242 final public ByteBuffer putInt (int index, int value)
244 ByteBufferHelper.putInt(this, index, value, order());
245 return this;
248 final public long getLong ()
250 return ByteBufferHelper.getLong(this, order());
253 final public ByteBuffer putLong (long value)
255 ByteBufferHelper.putLong (this, value, order());
256 return this;
259 final public long getLong (int index)
261 return ByteBufferHelper.getLong (this, index, order());
264 final public ByteBuffer putLong (int index, long value)
266 ByteBufferHelper.putLong (this, index, value, order());
267 return this;
270 final public float getFloat ()
272 return ByteBufferHelper.getFloat (this, order());
275 final public ByteBuffer putFloat (float value)
277 ByteBufferHelper.putFloat (this, value, order());
278 return this;
281 public final float getFloat (int index)
283 return ByteBufferHelper.getFloat (this, index, order());
286 final public ByteBuffer putFloat (int index, float value)
288 ByteBufferHelper.putFloat (this, index, value, order());
289 return this;
292 final public double getDouble ()
294 return ByteBufferHelper.getDouble (this, order());
297 final public ByteBuffer putDouble (double value)
299 ByteBufferHelper.putDouble (this, value, order());
300 return this;
303 final public double getDouble (int index)
305 return ByteBufferHelper.getDouble (this, index, order());
308 final public ByteBuffer putDouble (int index, double value)
310 ByteBufferHelper.putDouble (this, index, value, order());
311 return this;