1 /* DirectByteBufferImpl.java --
2 Copyright (C) 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)
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., 51 Franklin Street, Fifth Floor, 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. */
41 import gnu
.gcj
.RawData
;
43 abstract class DirectByteBufferImpl
extends ByteBuffer
46 * The owner is used to keep alive the object that actually owns the
47 * memory. There are three possibilities:
48 * 1) owner == this: We allocated the memory and we should free it,
49 * but *only* in finalize (if we've been sliced
50 * other objects will also have access to the
52 * 2) owner == null: The byte buffer was created thru
53 * JNI.NewDirectByteBuffer. The JNI code is
54 * responsible for freeing the memory.
55 * 3) owner == some other object: The other object allocated the
56 * memory and should free it.
58 private final Object owner
;
60 static final class ReadOnly
extends DirectByteBufferImpl
62 ReadOnly(Object owner
, RawData address
,
63 int capacity
, int limit
,
66 super(owner
, address
, capacity
, limit
, position
);
69 public ByteBuffer
put(byte value
)
71 throw new ReadOnlyBufferException ();
74 public ByteBuffer
put(int index
, byte value
)
76 throw new ReadOnlyBufferException ();
79 public boolean isReadOnly()
85 static final class ReadWrite
extends DirectByteBufferImpl
87 ReadWrite(int capacity
)
92 ReadWrite(RawData address
, int capacity
)
94 super(address
, capacity
);
97 ReadWrite(Object owner
, RawData address
,
98 int capacity
, int limit
,
101 super(owner
, address
, capacity
, limit
, position
);
104 public boolean isReadOnly()
110 DirectByteBufferImpl(int capacity
)
112 super(capacity
, capacity
, 0, -1,
113 VMDirectByteBuffer
.allocate(capacity
), null, 0);
117 DirectByteBufferImpl(RawData address
, int capacity
)
119 super(capacity
, capacity
, 0, -1, address
, null, 0);
123 DirectByteBufferImpl(Object owner
, RawData address
,
124 int capacity
, int limit
,
127 super(capacity
, limit
, position
, -1, address
, null, 0);
132 * Allocates a new direct byte buffer.
134 public static ByteBuffer
allocate(int capacity
)
136 return new DirectByteBufferImpl
.ReadWrite(capacity
);
139 protected void finalize() throws Throwable
142 VMDirectByteBuffer
.free(address
);
149 int pos
= position();
150 byte result
= VMDirectByteBuffer
.get(address
, pos
);
155 public byte get(int index
)
159 return VMDirectByteBuffer
.get(address
, index
);
162 public ByteBuffer
get(byte[] dst
, int offset
, int length
)
164 checkArraySize(dst
.length
, offset
, length
);
165 checkForUnderflow(length
);
167 int index
= position();
168 VMDirectByteBuffer
.get(address
, index
, dst
, offset
, length
);
169 position(index
+length
);
174 public ByteBuffer
put(byte value
)
178 int pos
= position();
179 VMDirectByteBuffer
.put(address
, pos
, value
);
184 public ByteBuffer
put(int index
, byte value
)
188 VMDirectByteBuffer
.put(address
, index
, value
);
192 void shiftDown(int dst_offset
, int src_offset
, int count
)
194 VMDirectByteBuffer
.shiftDown(address
, dst_offset
, src_offset
, count
);
197 public ByteBuffer
compact()
201 int pos
= position();
204 int count
= remaining();
205 VMDirectByteBuffer
.shiftDown(address
, 0, pos
, count
);
217 public ByteBuffer
slice()
219 int rem
= remaining();
221 return new DirectByteBufferImpl
.ReadOnly
222 (owner
, VMDirectByteBuffer
.adjustAddress(address
, position()),
225 return new DirectByteBufferImpl
.ReadWrite
226 (owner
, VMDirectByteBuffer
.adjustAddress(address
, position()),
230 private ByteBuffer
duplicate(boolean readOnly
)
232 int pos
= position();
234 int mark
= position();
236 DirectByteBufferImpl result
;
238 result
= new DirectByteBufferImpl
.ReadOnly(owner
, address
, capacity(),
241 result
= new DirectByteBufferImpl
.ReadWrite(owner
, address
, capacity(),
246 result
.position(mark
);
248 result
.position(pos
);
253 public ByteBuffer
duplicate()
255 return duplicate(isReadOnly());
258 public ByteBuffer
asReadOnlyBuffer()
260 return duplicate(true);
263 public boolean isDirect()
268 public CharBuffer
asCharBuffer()
270 return new CharViewBufferImpl(this, remaining() >> 1);
273 public ShortBuffer
asShortBuffer()
275 return new ShortViewBufferImpl(this, remaining() >> 1);
278 public IntBuffer
asIntBuffer()
280 return new IntViewBufferImpl(this, remaining() >> 2);
283 public LongBuffer
asLongBuffer()
285 return new LongViewBufferImpl(this, remaining() >> 3);
288 public FloatBuffer
asFloatBuffer()
290 return new FloatViewBufferImpl(this, remaining() >> 2);
293 public DoubleBuffer
asDoubleBuffer()
295 return new DoubleViewBufferImpl(this, remaining() >> 3);
298 public char getChar()
300 return ByteBufferHelper
.getChar(this, order());
303 public ByteBuffer
putChar(char value
)
305 ByteBufferHelper
.putChar(this, value
, order());
309 public char getChar(int index
)
311 return ByteBufferHelper
.getChar(this, index
, order());
314 public ByteBuffer
putChar(int index
, char value
)
316 ByteBufferHelper
.putChar(this, index
, value
, order());
320 public short getShort()
322 return ByteBufferHelper
.getShort(this, order());
325 public ByteBuffer
putShort(short value
)
327 ByteBufferHelper
.putShort(this, value
, order());
331 public short getShort(int index
)
333 return ByteBufferHelper
.getShort(this, index
, order());
336 public ByteBuffer
putShort(int index
, short value
)
338 ByteBufferHelper
.putShort(this, index
, value
, order());
344 return ByteBufferHelper
.getInt(this, order());
347 public ByteBuffer
putInt(int value
)
349 ByteBufferHelper
.putInt(this, value
, order());
353 public int getInt(int index
)
355 return ByteBufferHelper
.getInt(this, index
, order());
358 public ByteBuffer
putInt(int index
, int value
)
360 ByteBufferHelper
.putInt(this, index
, value
, order());
364 public long getLong()
366 return ByteBufferHelper
.getLong(this, order());
369 public ByteBuffer
putLong(long value
)
371 ByteBufferHelper
.putLong(this, value
, order());
375 public long getLong(int index
)
377 return ByteBufferHelper
.getLong(this, index
, order());
380 public ByteBuffer
putLong(int index
, long value
)
382 ByteBufferHelper
.putLong(this, index
, value
, order());
386 public float getFloat()
388 return ByteBufferHelper
.getFloat(this, order());
391 public ByteBuffer
putFloat(float value
)
393 ByteBufferHelper
.putFloat(this, value
, order());
397 public float getFloat(int index
)
399 return ByteBufferHelper
.getFloat(this, index
, order());
402 public ByteBuffer
putFloat(int index
, float value
)
404 ByteBufferHelper
.putFloat(this, index
, value
, order());
408 public double getDouble()
410 return ByteBufferHelper
.getDouble(this, order());
413 public ByteBuffer
putDouble(double value
)
415 ByteBufferHelper
.putDouble(this, value
, order());
419 public double getDouble(int index
)
421 return ByteBufferHelper
.getDouble(this, index
, order());
424 public ByteBuffer
putDouble(int index
, double value
)
426 ByteBufferHelper
.putDouble(this, index
, value
, order());