This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / java / nio / DirectByteBufferImpl.java
blob37b96a7c02eb94c1bc61943aa0e64bdc0255ab3c
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)
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 native void shiftDown (int dst_offset, int src_offset, int count);
122 public ByteBuffer compact ()
124 int pos = position();
125 if (pos > 0)
127 int count = remaining();
128 shiftDown(0, pos, count);
129 position(count);
130 limit(capacity());
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 (), 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());
200 return this;
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());
211 return this;
214 final public short getShort ()
216 return ByteBufferHelper.getShort(this, order());
219 final public ByteBuffer putShort (short value)
221 ByteBufferHelper.putShort(this, value, order());
222 return this;
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());
233 return this;
236 final public int getInt ()
238 return ByteBufferHelper.getInt(this, order());
241 final public ByteBuffer putInt (int value)
243 ByteBufferHelper.putInt(this, value, order());
244 return this;
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());
255 return this;
258 final public long getLong ()
260 return ByteBufferHelper.getLong(this, order());
263 final public ByteBuffer putLong (long value)
265 ByteBufferHelper.putLong (this, value, order());
266 return this;
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());
277 return this;
280 final public float getFloat ()
282 return ByteBufferHelper.getFloat (this, order());
285 final public ByteBuffer putFloat (float value)
287 ByteBufferHelper.putFloat (this, value, order());
288 return this;
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());
299 return this;
302 final public double getDouble ()
304 return ByteBufferHelper.getDouble (this, order());
307 final public ByteBuffer putDouble (double value)
309 ByteBufferHelper.putDouble (this, value, order());
310 return this;
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());
321 return this;