FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / java / nio / ByteBufferImpl.java
blob149dc1c9683327d5543f47a2cc3b44a255d24048
1 /* ByteBufferImpl.java --
2 Copyright (C) 2002 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. */
38 package gnu.java.nio;
40 import java.nio.ByteBuffer;
41 import java.nio.CharBuffer;
42 import java.nio.DoubleBuffer;
43 import java.nio.FloatBuffer;
44 import java.nio.IntBuffer;
45 import java.nio.LongBuffer;
46 import java.nio.ReadOnlyBufferException;
47 import java.nio.ShortBuffer;
49 /**
50 * This is a Heap memory implementation
52 public final class ByteBufferImpl extends ByteBuffer
54 private boolean readOnly;
56 public ByteBufferImpl (int cap, int off, int lim)
58 super (cap, lim, off, 0);
59 this.backing_buffer = new byte [cap];
60 readOnly = false;
63 public ByteBufferImpl (byte[] array, int offset, int length)
65 super (array.length, length, offset, 0);
66 this.backing_buffer = array;
67 readOnly = false;
70 public ByteBufferImpl (ByteBufferImpl copy)
72 super (copy.capacity (), copy.limit (), copy.position (), 0);
73 backing_buffer = copy.backing_buffer;
74 readOnly = copy.isReadOnly ();
77 void inc_pos (int toAdd)
79 position (position () + toAdd);
82 private static native byte[] nio_cast(byte[]copy);
83 private static native byte[] nio_cast(char[]copy);
84 private static native byte[] nio_cast(short[]copy);
85 private static native byte[] nio_cast(long[]copy);
86 private static native byte[] nio_cast(int[]copy);
87 private static native byte[] nio_cast(float[]copy);
88 private static native byte[] nio_cast(double[]copy);
90 ByteBufferImpl (byte[] copy)
92 super (copy.length, copy.length, 0, 0);
93 this.backing_buffer = copy != null ? nio_cast (copy) : null;
94 readOnly = false;
97 private static native byte nio_get_Byte (ByteBufferImpl b, int index, int limit);
99 private static native void nio_put_Byte (ByteBufferImpl b, int index, int limit, byte value);
101 public ByteBuffer asByteBuffer ()
103 ByteBufferImpl res = new ByteBufferImpl (backing_buffer);
104 res.limit ((limit () * 1) / 1);
105 return res;
108 ByteBufferImpl (char[] copy)
110 super (copy.length * 2, copy.length * 2, 0, 0);
111 this.backing_buffer = copy != null ? nio_cast (copy) : null;
112 readOnly = false;
115 private static native char nio_get_Char (ByteBufferImpl b, int index, int limit);
117 private static native void nio_put_Char (ByteBufferImpl b, int index, int limit, char value);
119 public CharBuffer asCharBuffer ()
121 CharBufferImpl res = new CharBufferImpl (backing_buffer);
122 res.limit ((limit () * 2) / 1);
123 return res;
126 ByteBufferImpl (short[] copy)
128 super (copy.length, copy.length, 0, 0);
129 this.backing_buffer = copy != null ? nio_cast (copy) : null;
130 readOnly = false;
133 private static native short nio_get_Short (ByteBufferImpl b, int index, int limit);
135 private static native void nio_put_Short (ByteBufferImpl b, int index, int limit, short value);
137 public ShortBuffer asShortBuffer ()
139 ShortBufferImpl res = new ShortBufferImpl (backing_buffer);
140 res.limit ((limit () * 2) / 1);
141 return res;
144 ByteBufferImpl (int[] copy)
146 super (copy.length * 4, copy.length * 4, 0, 0);
147 this.backing_buffer = copy != null ? nio_cast(copy) : null;
148 readOnly = false;
151 private static native int nio_get_Int (ByteBufferImpl b, int index, int limit);
153 private static native void nio_put_Int (ByteBufferImpl b, int index, int limit, int value);
155 public IntBuffer asIntBuffer ()
157 IntBufferImpl res = new IntBufferImpl (backing_buffer);
158 res.limit ((limit() * 4) / 1);
159 return res;
162 ByteBufferImpl (long[] copy)
164 super (copy.length * 8, copy.length * 8, 0, 0);
165 this.backing_buffer = copy != null ? nio_cast (copy) : null;
166 readOnly = false;
169 private static native long nio_get_Long (ByteBufferImpl b, int index, int limit);
171 private static native void nio_put_Long (ByteBufferImpl b, int index, int limit, long value);
173 public LongBuffer asLongBuffer ()
175 LongBufferImpl res = new LongBufferImpl (backing_buffer);
176 res.limit ((limit() * 8) / 1);
177 return res;
180 ByteBufferImpl (float[] copy)
182 super (copy.length * 4, copy.length * 4, 0, 0);
183 this.backing_buffer = copy != null ? nio_cast (copy) : null;
184 readOnly = false;
187 private static native float nio_get_Float (ByteBufferImpl b, int index, int limit);
189 private static native void nio_put_Float (ByteBufferImpl b, int index, int limit, float value);
191 public FloatBuffer asFloatBuffer ()
193 FloatBufferImpl res = new FloatBufferImpl (backing_buffer);
194 res.limit ((limit() * 4) / 1);
195 return res;
198 ByteBufferImpl (double[] copy)
200 super (copy.length * 8, copy.length * 8, 0, 0);
201 this.backing_buffer = copy != null ? nio_cast (copy) : null;
202 readOnly = false;
205 private static native double nio_get_Double (ByteBufferImpl b, int index, int limit);
207 private static native void nio_put_Double (ByteBufferImpl b, int index, int limit, double value);
209 public DoubleBuffer asDoubleBuffer ()
211 DoubleBufferImpl res = new DoubleBufferImpl (backing_buffer);
212 res.limit ((limit () * 8) / 1);
213 return res;
216 public boolean isReadOnly()
218 return readOnly;
221 public ByteBuffer slice()
223 return new ByteBufferImpl(this);
226 public ByteBuffer duplicate()
228 return new ByteBufferImpl(this);
231 public ByteBuffer asReadOnlyBuffer()
233 ByteBufferImpl a = new ByteBufferImpl(this);
234 a.readOnly = true;
235 return a;
238 public ByteBuffer compact()
240 return this;
243 public boolean isDirect()
245 return false;
248 final public byte get()
250 byte e = backing_buffer[position()];
251 position(position()+1);
252 return e;
255 final public ByteBuffer put(byte b)
257 if (readOnly)
258 throw new ReadOnlyBufferException ();
260 backing_buffer[position()] = b;
261 position(position()+1);
262 return this;
265 final public byte get(int index)
267 return backing_buffer[index];
270 final public ByteBuffer put(int index, byte b)
272 if (readOnly)
273 throw new ReadOnlyBufferException ();
275 backing_buffer[index] = b;
276 return this;
279 final public char getChar ()
281 char a = nio_get_Char (this, position (), limit ());
282 inc_pos (2);
283 return a;
286 final public ByteBuffer putChar (char value)
288 if (readOnly)
289 throw new ReadOnlyBufferException ();
291 nio_put_Char (this, position (), limit (), value);
292 inc_pos (2);
293 return this;
296 final public char getChar (int index)
298 char a = nio_get_Char (this, index, limit ());
299 return a;
302 final public ByteBuffer putChar (int index, char value)
304 if (readOnly)
305 throw new ReadOnlyBufferException ();
307 nio_put_Char (this, index, limit (), value);
308 return this;
311 final public short getShort ()
313 short a = nio_get_Short (this, position (), limit ());
314 inc_pos (2);
315 return a;
318 final public ByteBuffer putShort (short value)
320 if (readOnly)
321 throw new ReadOnlyBufferException ();
323 nio_put_Short (this, position (), limit(), value);
324 inc_pos (2);
325 return this;
328 final public short getShort (int index)
330 short a = nio_get_Short (this, index, limit ());
331 return a;
334 final public ByteBuffer putShort (int index, short value)
336 if (readOnly)
337 throw new ReadOnlyBufferException ();
339 nio_put_Short (this, index, limit (), value);
340 return this;
343 final public int getInt ()
345 int a = nio_get_Int (this, position (), limit ());
346 inc_pos (4);
347 return a;
350 final public ByteBuffer putInt (int value)
352 if (readOnly)
353 throw new ReadOnlyBufferException ();
355 nio_put_Int (this, position (), limit , value);
356 inc_pos (4);
357 return this;
360 final public int getInt (int index)
362 int a = nio_get_Int (this, index, limit ());
363 return a;
366 final public ByteBuffer putInt (int index, int value)
368 if (readOnly)
369 throw new ReadOnlyBufferException ();
371 nio_put_Int(this, index, limit (), value);
372 return this;
375 final public long getLong ()
377 long a = nio_get_Long (this, position (), limit ());
378 inc_pos (8);
379 return a;
382 final public ByteBuffer putLong (long value)
384 if (readOnly)
385 throw new ReadOnlyBufferException ();
387 nio_put_Long (this, position (), limit (), value);
388 inc_pos (8);
389 return this;
392 final public long getLong (int index)
394 long a = nio_get_Long (this, index, limit ());
395 return a;
398 final public ByteBuffer putLong (int index, long value)
400 if (readOnly)
401 throw new ReadOnlyBufferException ();
403 nio_put_Long (this, index, limit (), value);
404 return this;
407 final public float getFloat ()
409 float a = nio_get_Float (this, position (), limit ());
410 inc_pos (4);
411 return a;
414 final public ByteBuffer putFloat (float value)
416 if (readOnly)
417 throw new ReadOnlyBufferException ();
419 nio_put_Float (this, position (), limit (), value);
420 inc_pos (4);
421 return this;
424 final public float getFloat (int index)
426 float a = nio_get_Float (this, index, limit ());
427 return a;
430 final public ByteBuffer putFloat (int index, float value)
432 if (readOnly)
433 throw new ReadOnlyBufferException ();
435 nio_put_Float (this, index, limit(), value);
436 return this;
439 final public double getDouble ()
441 double a = nio_get_Double (this, position (), limit ());
442 inc_pos (8);
443 return a;
446 final public ByteBuffer putDouble (double value)
448 if (readOnly)
449 throw new ReadOnlyBufferException ();
451 nio_put_Double (this, position(), limit (), value);
452 inc_pos (8);
453 return this;
456 final public double getDouble (int index)
458 return nio_get_Double (this, index, limit ());
461 final public ByteBuffer putDouble (int index, double value)
463 if (readOnly)
464 throw new ReadOnlyBufferException ();
466 nio_put_Double (this, index, limit (), value);
467 return this;