2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / nio / ByteBufferImpl.java
blob76c965d73224152a99248f82b6ad39ace0143411
1 /* ByteBufferImpl.java --
2 Copyright (C) 2002, 2003 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 ());
64 public ShortBuffer asShortBuffer ()
66 return new ShortViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ());
69 public IntBuffer asIntBuffer ()
71 return new IntViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ());
74 public LongBuffer asLongBuffer ()
76 return new LongViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ());
79 public FloatBuffer asFloatBuffer ()
81 return new FloatViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ());
84 public DoubleBuffer asDoubleBuffer ()
86 return new DoubleViewBufferImpl (this, position (), remaining(), remaining (), 0, -1, isReadOnly ());
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 copied = 0;
113 while (remaining () > 0)
115 put (copied, get ());
116 copied++;
119 position (copied);
120 return this;
123 public boolean isDirect ()
125 return false;
129 * Relative get method. Reads the next <code>byte</code> from the buffer.
131 final public byte get ()
133 byte result = backing_buffer [position ()];
134 position (position () + 1);
135 return result;
139 * Relative put method. Writes <code>value</code> to the next position
140 * in the buffer.
142 * @exception ReadOnlyBufferException If this buffer is read-only.
144 final public ByteBuffer put (byte value)
146 if (readOnly)
147 throw new ReadOnlyBufferException ();
149 backing_buffer [position ()] = value;
150 position (position () + 1);
151 return this;
155 * Absolute get method. Reads the <code>byte</code> at position
156 * <code>index</code>.
158 * @exception IndexOutOfBoundsException If index is negative or not smaller
159 * than the buffer's limit.
161 final public byte get (int index)
163 return backing_buffer [index];
167 * Absolute put method. Writes <code>value</value> to position
168 * <code>index</code> in the buffer.
170 * @exception IndexOutOfBoundsException If index is negative or not smaller
171 * than the buffer's limit.
172 * @exception ReadOnlyBufferException If this buffer is read-only.
174 final public ByteBuffer put (int index, byte value)
176 if (readOnly)
177 throw new ReadOnlyBufferException ();
179 backing_buffer [index] = value;
180 return this;
183 final public char getChar ()
185 return ByteBufferHelper.getChar (this);
188 final public ByteBuffer putChar (char value)
190 return ByteBufferHelper.putChar (this, value);
193 final public char getChar (int index)
195 return ByteBufferHelper.getChar (this, index);
198 final public ByteBuffer putChar (int index, char value)
200 return ByteBufferHelper.putChar (this, index, value);
203 final public short getShort ()
205 return ByteBufferHelper.getShort (this);
208 final public ByteBuffer putShort (short value)
210 return ByteBufferHelper.putShort (this, value);
213 final public short getShort (int index)
215 return ByteBufferHelper.getShort (this, index);
218 final public ByteBuffer putShort (int index, short value)
220 return ByteBufferHelper.putShort (this, index, value);
223 final public int getInt ()
225 return ByteBufferHelper.getInt (this);
228 final public ByteBuffer putInt (int value)
230 return ByteBufferHelper.putInt (this, value);
233 final public int getInt (int index)
235 return ByteBufferHelper.getInt (this, index);
238 final public ByteBuffer putInt (int index, int value)
240 return ByteBufferHelper.putInt (this, index, value);
243 final public long getLong ()
245 return ByteBufferHelper.getLong (this);
248 final public ByteBuffer putLong (long value)
250 return ByteBufferHelper.putLong (this, value);
253 final public long getLong (int index)
255 return ByteBufferHelper.getLong (this, index);
258 final public ByteBuffer putLong (int index, long value)
260 return ByteBufferHelper.putLong (this, index, value);
263 final public float getFloat ()
265 return ByteBufferHelper.getFloat (this);
268 final public ByteBuffer putFloat (float value)
270 return ByteBufferHelper.putFloat (this, value);
273 final public float getFloat (int index)
275 return ByteBufferHelper.getFloat (this, index);
278 public final ByteBuffer putFloat (int index, float value)
280 return ByteBufferHelper.putFloat (this, index, value);
283 final public double getDouble ()
285 return ByteBufferHelper.getDouble (this);
288 final public ByteBuffer putDouble (double value)
290 return ByteBufferHelper.putDouble (this, value);
293 final public double getDouble (int index)
295 return ByteBufferHelper.getDouble (this, index);
298 final public ByteBuffer putDouble (int index, double value)
300 return ByteBufferHelper.putDouble (this, index, value);