2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / nio / DirectByteBufferImpl.java
bloba54c20693feb279ccdb38d1a8265c0000cd56767
1 /* DirectByteBufferImpl.java --
2 Copyright (C) 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 import gnu.classpath.Configuration;
42 import gnu.gcj.RawData;
44 class DirectByteBufferImpl extends ByteBuffer
46 static
48 // load the shared library needed for native methods.
49 if (Configuration.INIT_LOAD_LIBRARY)
51 System.loadLibrary ("javanio");
55 RawData address;
56 private int offset;
57 private boolean readOnly;
59 public DirectByteBufferImpl (RawData address, long len)
61 this (address, 0, (int) len, (int) len, 0, -1, false);
64 public DirectByteBufferImpl (RawData address, int offset, int capacity,
65 int limit, int position, int mark,
66 boolean readOnly)
68 super (capacity, limit, position, mark);
69 this.address = address;
70 this.offset = offset;
71 this.readOnly = readOnly;
74 private static native RawData allocateImpl (int capacity);
75 private static native void freeImpl (RawData address);
77 protected void finalize () throws Throwable
79 freeImpl (address);
82 public static ByteBuffer allocateDirect (int capacity)
84 RawData address = allocateImpl (capacity);
86 if (address == null)
87 throw new InternalError ("Not enough memory to create direct buffer");
89 return new DirectByteBufferImpl (address, 0, capacity, capacity, 0, -1, false);
92 private native byte getImpl (int index);
93 private native void putImpl (int index, byte value);
95 public byte get ()
97 byte result = getImpl (position () + offset);
98 position (position () + 1);
99 return result;
102 public byte get (int index)
104 return getImpl (index);
107 public ByteBuffer put (byte value)
109 putImpl (position (), value);
110 position (position () + 1);
111 return this;
114 public ByteBuffer put (int index, byte value)
116 putImpl (index, value);
117 return this;
120 public ByteBuffer compact ()
122 // FIXME this can sure be optimized using memcpy()
123 int copied = 0;
125 while (remaining () > 0)
127 put (copied, get ());
128 copied++;
131 position (copied);
132 return this;
135 public ByteBuffer duplicate ()
137 return new DirectByteBufferImpl (
138 address, offset, capacity (), limit (), position (), -1, isReadOnly ());
141 public ByteBuffer slice ()
143 return new DirectByteBufferImpl (address, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
146 public ByteBuffer asReadOnlyBuffer ()
148 return new DirectByteBufferImpl (
149 address, offset, capacity (), limit (), position (), -1, true);
152 public boolean isReadOnly ()
154 return readOnly;
157 public boolean isDirect ()
159 return true;
162 public CharBuffer asCharBuffer ()
164 return new CharViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
167 public DoubleBuffer asDoubleBuffer ()
169 return new DoubleViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
172 public FloatBuffer asFloatBuffer ()
174 return new FloatViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
177 public IntBuffer asIntBuffer ()
179 return new IntViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
182 public LongBuffer asLongBuffer ()
184 return new LongViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
187 public ShortBuffer asShortBuffer ()
189 return new ShortViewBufferImpl (this, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
192 final public char getChar ()
194 // FIXME: this handles little endian only
195 return (char) (((get () & 0xff) << 8)
196 + (get () & 0xff));
199 final public ByteBuffer putChar (char value)
201 // FIXME: this handles little endian only
202 put ((byte) ((((int) value) & 0xff00) >> 8));
203 put ((byte) (((int) value) & 0x00ff));
204 return this;
207 final public char getChar (int index)
209 // FIXME: this handles little endian only
210 return (char) (((get (index) & 0xff) << 8)
211 + (get (index + 1) & 0xff));
214 final public ByteBuffer putChar (int index, char value)
216 // FIXME: this handles little endian only
217 put (index, (byte) ((((int) value) & 0xff00) >> 8));
218 put (index + 1, (byte) (((int) value) & 0x00ff));
219 return this;
222 final public short getShort ()
224 // FIXME: this handles little endian only
225 return (short) (((get () & 0xff) << 8)
226 + (get () & 0xff));
229 final public ByteBuffer putShort (short value)
231 // FIXME: this handles little endian only
232 put ((byte) ((((int) value) & 0xff00) >> 8));
233 put ((byte) (((int) value) & 0x00ff));
234 return this;
237 final public short getShort (int index)
239 // FIXME: this handles little endian only
240 return (short) (((get (index) & 0xff) << 8)
241 + (get (index + 1) & 0xff));
244 final public ByteBuffer putShort (int index, short value)
246 // FIXME: this handles little endian only
247 put (index, (byte) ((((int) value) & 0xff00) >> 8));
248 put (index + 1, (byte) (((int) value) & 0x00ff));
249 return this;
252 final public int getInt ()
254 // FIXME: this handles little endian only
255 return (int) (((get () & 0xff) << 24)
256 + ((get () & 0xff) << 16)
257 + ((get () & 0xff) << 8)
258 + (get () & 0xff));
261 final public ByteBuffer putInt (int value)
263 // FIXME: this handles little endian only
264 put ((byte) ((((int) value) & 0xff000000) >> 24));
265 put ((byte) ((((int) value) & 0x00ff0000) >> 16));
266 put ((byte) ((((int) value) & 0x0000ff00) >> 8));
267 put ((byte) (((int) value) & 0x000000ff));
268 return this;
271 final public int getInt (int index)
273 // FIXME: this handles little endian only
274 return (int) (((get (index) & 0xff) << 24)
275 + ((get (index + 1) & 0xff) << 16)
276 + ((get (index + 2) & 0xff) << 8)
277 + (get (index + 3) & 0xff));
280 final public ByteBuffer putInt (int index, int value)
282 // FIXME: this handles little endian only
283 put (index, (byte) ((((int) value) & 0xff000000) >> 24));
284 put (index + 1, (byte) ((((int) value) & 0x00ff0000) >> 16));
285 put (index + 2, (byte) ((((int) value) & 0x0000ff00) >> 8));
286 put (index + 3, (byte) (((int) value) & 0x000000ff));
287 return this;
290 final public long getLong ()
292 // FIXME: this handles little endian only
293 return (long) (((get () & 0xff) << 56)
294 + ((get () & 0xff) << 48)
295 + ((get () & 0xff) << 40)
296 + ((get () & 0xff) << 32)
297 + ((get () & 0xff) << 24)
298 + ((get () & 0xff) << 16)
299 + ((get () & 0xff) << 8)
300 + (get () & 0xff));
303 final public ByteBuffer putLong (long value)
305 return ByteBufferHelper.putLong (this, value);
308 final public long getLong (int index)
310 return ByteBufferHelper.getLong (this, index);
313 final public ByteBuffer putLong (int index, long value)
315 return ByteBufferHelper.putLong (this, index, value);
318 final public float getFloat ()
320 return ByteBufferHelper.getFloat (this);
323 final public ByteBuffer putFloat (float value)
325 return ByteBufferHelper.putFloat (this, value);
328 public final float getFloat (int index)
330 return ByteBufferHelper.getFloat (this, index);
333 final public ByteBuffer putFloat (int index, float value)
335 return ByteBufferHelper.putFloat (this, index, value);
338 final public double getDouble ()
340 return ByteBufferHelper.getDouble (this);
343 final public ByteBuffer putDouble (double value)
345 return ByteBufferHelper.putDouble (this, value);
348 final public double getDouble (int index)
350 return ByteBufferHelper.getDouble (this, index);
353 final public ByteBuffer putDouble (int index, double value)
355 return ByteBufferHelper.putDouble (this, index, value);