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., 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. */
41 import gnu
.classpath
.Configuration
;
42 import gnu
.gcj
.RawData
;
44 class DirectByteBufferImpl
extends ByteBuffer
48 // load the shared library needed for native methods.
49 if (Configuration
.INIT_LOAD_LIBRARY
)
51 System
.loadLibrary ("javanio");
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
,
68 super (capacity
, limit
, position
, mark
);
69 this.address
= address
;
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
82 public static ByteBuffer
allocateDirect (int capacity
)
84 RawData address
= allocateImpl (capacity
);
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
);
97 byte result
= getImpl (position () + offset
);
98 position (position () + 1);
102 public byte get (int index
)
104 return getImpl (index
);
107 public ByteBuffer
put (byte value
)
109 putImpl (position (), value
);
110 position (position () + 1);
114 public ByteBuffer
put (int index
, byte value
)
116 putImpl (index
, value
);
120 native void shiftDown (int dst_offset
, int src_offset
, int count
);
122 public ByteBuffer
compact ()
124 int pos
= position();
127 int count
= remaining();
128 shiftDown(0, pos
, count
);
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 ()
157 public boolean isDirect ()
162 public CharBuffer
asCharBuffer ()
164 return new CharViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
167 public DoubleBuffer
asDoubleBuffer ()
169 return new DoubleViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
172 public FloatBuffer
asFloatBuffer ()
174 return new FloatViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
177 public IntBuffer
asIntBuffer ()
179 return new IntViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
182 public LongBuffer
asLongBuffer ()
184 return new LongViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
187 public ShortBuffer
asShortBuffer ()
189 return new ShortViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
192 final public char getChar ()
194 return ByteBufferHelper
.getChar(this, order());
197 final public ByteBuffer
putChar (char value
)
199 ByteBufferHelper
.putChar(this, value
, order());
203 final public char getChar (int index
)
205 return ByteBufferHelper
.getChar(this, index
, order());
208 final public ByteBuffer
putChar (int index
, char value
)
210 ByteBufferHelper
.putChar(this, index
, value
, order());
214 final public short getShort ()
216 return ByteBufferHelper
.getShort(this, order());
219 final public ByteBuffer
putShort (short value
)
221 ByteBufferHelper
.putShort(this, value
, order());
225 final public short getShort (int index
)
227 return ByteBufferHelper
.getShort(this, index
, order());
230 final public ByteBuffer
putShort (int index
, short value
)
232 ByteBufferHelper
.putShort(this, index
, value
, order());
236 final public int getInt ()
238 return ByteBufferHelper
.getInt(this, order());
241 final public ByteBuffer
putInt (int value
)
243 ByteBufferHelper
.putInt(this, value
, order());
247 final public int getInt (int index
)
249 return ByteBufferHelper
.getInt(this, index
, order());
252 final public ByteBuffer
putInt (int index
, int value
)
254 ByteBufferHelper
.putInt(this, index
, value
, order());
258 final public long getLong ()
260 return ByteBufferHelper
.getLong(this, order());
263 final public ByteBuffer
putLong (long value
)
265 ByteBufferHelper
.putLong (this, value
, order());
269 final public long getLong (int index
)
271 return ByteBufferHelper
.getLong (this, index
, order());
274 final public ByteBuffer
putLong (int index
, long value
)
276 ByteBufferHelper
.putLong (this, index
, value
, order());
280 final public float getFloat ()
282 return ByteBufferHelper
.getFloat (this, order());
285 final public ByteBuffer
putFloat (float value
)
287 ByteBufferHelper
.putFloat (this, value
, order());
291 public final float getFloat (int index
)
293 return ByteBufferHelper
.getFloat (this, index
, order());
296 final public ByteBuffer
putFloat (int index
, float value
)
298 ByteBufferHelper
.putFloat (this, index
, value
, order());
302 final public double getDouble ()
304 return ByteBufferHelper
.getDouble (this, order());
307 final public ByteBuffer
putDouble (double value
)
309 ByteBufferHelper
.putDouble (this, value
, order());
313 final public double getDouble (int index
)
315 return ByteBufferHelper
.getDouble (this, index
, order());
318 final public ByteBuffer
putDouble (int index
, double value
)
320 ByteBufferHelper
.putDouble (this, index
, value
, order());