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)
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
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
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. */
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
;
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
];
63 public ByteBufferImpl (byte[] array
, int offset
, int length
)
65 super (array
.length
, length
, offset
, 0);
66 this.backing_buffer
= array
;
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;
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);
108 ByteBufferImpl (char[] copy
)
110 super (copy
.length
* 2, copy
.length
* 2, 0, 0);
111 this.backing_buffer
= copy
!= null ?
nio_cast (copy
) : null;
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);
126 ByteBufferImpl (short[] copy
)
128 super (copy
.length
, copy
.length
, 0, 0);
129 this.backing_buffer
= copy
!= null ?
nio_cast (copy
) : null;
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);
144 ByteBufferImpl (int[] copy
)
146 super (copy
.length
* 4, copy
.length
* 4, 0, 0);
147 this.backing_buffer
= copy
!= null ?
nio_cast(copy
) : null;
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);
162 ByteBufferImpl (long[] copy
)
164 super (copy
.length
* 8, copy
.length
* 8, 0, 0);
165 this.backing_buffer
= copy
!= null ?
nio_cast (copy
) : null;
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);
180 ByteBufferImpl (float[] copy
)
182 super (copy
.length
* 4, copy
.length
* 4, 0, 0);
183 this.backing_buffer
= copy
!= null ?
nio_cast (copy
) : null;
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);
198 ByteBufferImpl (double[] copy
)
200 super (copy
.length
* 8, copy
.length
* 8, 0, 0);
201 this.backing_buffer
= copy
!= null ?
nio_cast (copy
) : null;
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);
216 public boolean isReadOnly()
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);
238 public ByteBuffer
compact()
243 public boolean isDirect()
248 final public byte get()
250 byte e
= backing_buffer
[position()];
251 position(position()+1);
255 final public ByteBuffer
put(byte b
)
258 throw new ReadOnlyBufferException ();
260 backing_buffer
[position()] = b
;
261 position(position()+1);
265 final public byte get(int index
)
267 return backing_buffer
[index
];
270 final public ByteBuffer
put(int index
, byte b
)
273 throw new ReadOnlyBufferException ();
275 backing_buffer
[index
] = b
;
279 final public char getChar ()
281 char a
= nio_get_Char (this, position (), limit ());
286 final public ByteBuffer
putChar (char value
)
289 throw new ReadOnlyBufferException ();
291 nio_put_Char (this, position (), limit (), value
);
296 final public char getChar (int index
)
298 char a
= nio_get_Char (this, index
, limit ());
302 final public ByteBuffer
putChar (int index
, char value
)
305 throw new ReadOnlyBufferException ();
307 nio_put_Char (this, index
, limit (), value
);
311 final public short getShort ()
313 short a
= nio_get_Short (this, position (), limit ());
318 final public ByteBuffer
putShort (short value
)
321 throw new ReadOnlyBufferException ();
323 nio_put_Short (this, position (), limit(), value
);
328 final public short getShort (int index
)
330 short a
= nio_get_Short (this, index
, limit ());
334 final public ByteBuffer
putShort (int index
, short value
)
337 throw new ReadOnlyBufferException ();
339 nio_put_Short (this, index
, limit (), value
);
343 final public int getInt ()
345 int a
= nio_get_Int (this, position (), limit ());
350 final public ByteBuffer
putInt (int value
)
353 throw new ReadOnlyBufferException ();
355 nio_put_Int (this, position (), limit
, value
);
360 final public int getInt (int index
)
362 int a
= nio_get_Int (this, index
, limit ());
366 final public ByteBuffer
putInt (int index
, int value
)
369 throw new ReadOnlyBufferException ();
371 nio_put_Int(this, index
, limit (), value
);
375 final public long getLong ()
377 long a
= nio_get_Long (this, position (), limit ());
382 final public ByteBuffer
putLong (long value
)
385 throw new ReadOnlyBufferException ();
387 nio_put_Long (this, position (), limit (), value
);
392 final public long getLong (int index
)
394 long a
= nio_get_Long (this, index
, limit ());
398 final public ByteBuffer
putLong (int index
, long value
)
401 throw new ReadOnlyBufferException ();
403 nio_put_Long (this, index
, limit (), value
);
407 final public float getFloat ()
409 float a
= nio_get_Float (this, position (), limit ());
414 final public ByteBuffer
putFloat (float value
)
417 throw new ReadOnlyBufferException ();
419 nio_put_Float (this, position (), limit (), value
);
424 final public float getFloat (int index
)
426 float a
= nio_get_Float (this, index
, limit ());
430 final public ByteBuffer
putFloat (int index
, float value
)
433 throw new ReadOnlyBufferException ();
435 nio_put_Float (this, index
, limit(), value
);
439 final public double getDouble ()
441 double a
= nio_get_Double (this, position (), limit ());
446 final public ByteBuffer
putDouble (double value
)
449 throw new ReadOnlyBufferException ();
451 nio_put_Double (this, position(), limit (), value
);
456 final public double getDouble (int index
)
458 return nio_get_Double (this, index
, limit ());
461 final public ByteBuffer
putDouble (int index
, double value
)
464 throw new ReadOnlyBufferException ();
466 nio_put_Double (this, index
, limit (), value
);