1 /* DirectByteBufferImpl.java --
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 
39 package java.nio;
40 
41 import gnu.classpath.Configuration;
42 import gnu.gcj.RawData;
43 
44 class DirectByteBufferImpl extends ByteBuffer
45 {
46   static
47   {
48     // load the shared library needed for native methods.
49     if (Configuration.INIT_LOAD_LIBRARY)
50       {
51         System.loadLibrary ("javanio");
52       }
53   }
54 
55   RawData address;
56   private int offset;
57   private boolean readOnly;
58 
DirectByteBufferImpl(RawData address, long len)59   public DirectByteBufferImpl (RawData address, long len)
60   {
61     this (address, 0, (int) len, (int) len, 0, -1, false);
62   }
63 
DirectByteBufferImpl(RawData address, int offset, int capacity, int limit, int position, int mark, boolean readOnly)64   public DirectByteBufferImpl (RawData address, int offset, int capacity,
65                                int limit, int position, int mark,
66                                boolean readOnly)
67   {
68     super (capacity, limit, position, mark);
69     this.address = address;
70     this.offset = offset;
71     this.readOnly = readOnly;
72   }
73 
allocateImpl(int capacity)74   private static native RawData allocateImpl (int capacity);
freeImpl(RawData address)75   private static native void freeImpl (RawData address);
76 
finalize()77   protected void finalize () throws Throwable
78   {
79     freeImpl (address);
80   }
81 
allocateDirect(int capacity)82   public static ByteBuffer allocateDirect (int capacity)
83   {
84     RawData address = allocateImpl (capacity);
85 
86     if (address == null)
87       throw new InternalError ("Not enough memory to create direct buffer");
88 
89     return new DirectByteBufferImpl (address, 0, capacity, capacity, 0, -1, false);
90   }
91 
getImpl(int index)92   private native byte getImpl (int index);
putImpl(int index, byte value)93   private native void putImpl (int index, byte value);
94 
get()95   public byte get ()
96   {
97     byte result = getImpl (position () + offset);
98     position (position () + 1);
99     return result;
100   }
101 
get(int index)102   public byte get (int index)
103   {
104     return getImpl (index);
105   }
106 
put(byte value)107   public ByteBuffer put (byte value)
108   {
109     putImpl (position (), value);
110     position (position () + 1);
111     return this;
112   }
113 
put(int index, byte value)114   public ByteBuffer put (int index, byte value)
115   {
116     putImpl (index, value);
117     return this;
118   }
119 
shiftDown(int dst_offset, int src_offset, int count)120   native void shiftDown (int dst_offset, int src_offset, int count);
121 
compact()122   public ByteBuffer compact ()
123   {
124     int pos = position();
125     if (pos > 0)
126       {
127 	int count = remaining();
128 	shiftDown(0, pos, count);
129 	position(count);
130 	limit(capacity());
131       }
132     return this;
133   }
134 
duplicate()135   public ByteBuffer duplicate ()
136   {
137     return new DirectByteBufferImpl (
138       address, offset, capacity (), limit (), position (), -1, isReadOnly ());
139   }
140 
slice()141   public ByteBuffer slice ()
142   {
143     return new DirectByteBufferImpl (address, position () + offset, remaining (), remaining (), 0, -1, isReadOnly ());
144   }
145 
asReadOnlyBuffer()146   public ByteBuffer asReadOnlyBuffer ()
147   {
148     return new DirectByteBufferImpl (
149       address, offset, capacity (), limit (), position (), -1, true);
150   }
151 
isReadOnly()152   public boolean isReadOnly ()
153   {
154     return readOnly;
155   }
156 
isDirect()157   public boolean isDirect ()
158   {
159     return true;
160   }
161 
asCharBuffer()162   public CharBuffer asCharBuffer ()
163   {
164     return new CharViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
165   }
166 
asDoubleBuffer()167   public DoubleBuffer asDoubleBuffer ()
168   {
169     return new DoubleViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
170   }
171 
asFloatBuffer()172   public FloatBuffer asFloatBuffer ()
173   {
174     return new FloatViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
175   }
176 
asIntBuffer()177   public IntBuffer asIntBuffer ()
178   {
179     return new IntViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
180   }
181 
asLongBuffer()182   public LongBuffer asLongBuffer ()
183   {
184     return new LongViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
185   }
186 
asShortBuffer()187   public ShortBuffer asShortBuffer ()
188   {
189     return new ShortViewBufferImpl (this, position (), remaining (), remaining (), 0, -1, isReadOnly (), order());
190   }
191 
getChar()192   final public char getChar ()
193   {
194     return ByteBufferHelper.getChar(this, order());
195   }
196 
putChar(char value)197   final public ByteBuffer putChar (char value)
198   {
199     ByteBufferHelper.putChar(this, value, order());
200     return this;
201   }
202 
getChar(int index)203   final public char getChar (int index)
204   {
205     return ByteBufferHelper.getChar(this, index, order());
206   }
207 
putChar(int index, char value)208   final public ByteBuffer putChar (int index, char value)
209   {
210     ByteBufferHelper.putChar(this, index, value, order());
211     return this;
212   }
213 
getShort()214   final public short getShort ()
215   {
216     return ByteBufferHelper.getShort(this, order());
217   }
218 
putShort(short value)219   final public ByteBuffer putShort (short value)
220   {
221     ByteBufferHelper.putShort(this, value, order());
222     return this;
223   }
224 
getShort(int index)225   final public short getShort (int index)
226   {
227     return ByteBufferHelper.getShort(this, index, order());
228   }
229 
putShort(int index, short value)230   final public ByteBuffer putShort (int index, short value)
231   {
232     ByteBufferHelper.putShort(this, index, value, order());
233     return this;
234   }
235 
getInt()236   final public int getInt ()
237   {
238     return ByteBufferHelper.getInt(this, order());
239   }
240 
putInt(int value)241   final public ByteBuffer putInt (int value)
242   {
243     ByteBufferHelper.putInt(this, value, order());
244     return this;
245   }
246 
getInt(int index)247   final public int getInt (int index)
248   {
249     return ByteBufferHelper.getInt(this, index, order());
250   }
251 
putInt(int index, int value)252   final public ByteBuffer putInt (int index, int value)
253   {
254     ByteBufferHelper.putInt(this, index, value, order());
255     return this;
256   }
257 
getLong()258   final public long getLong ()
259   {
260     return ByteBufferHelper.getLong(this, order());
261   }
262 
putLong(long value)263   final public ByteBuffer putLong (long value)
264   {
265     ByteBufferHelper.putLong (this, value, order());
266     return this;
267   }
268 
getLong(int index)269   final public long getLong (int index)
270   {
271     return ByteBufferHelper.getLong (this, index, order());
272   }
273 
putLong(int index, long value)274   final public ByteBuffer putLong (int index, long value)
275   {
276     ByteBufferHelper.putLong (this, index, value, order());
277     return this;
278   }
279 
getFloat()280   final public float getFloat ()
281   {
282     return ByteBufferHelper.getFloat (this, order());
283   }
284 
putFloat(float value)285   final public ByteBuffer putFloat (float value)
286   {
287     ByteBufferHelper.putFloat (this, value, order());
288     return this;
289   }
290 
getFloat(int index)291   public final float getFloat (int index)
292   {
293     return ByteBufferHelper.getFloat (this, index, order());
294   }
295 
putFloat(int index, float value)296   final public ByteBuffer putFloat (int index, float value)
297   {
298     ByteBufferHelper.putFloat (this, index, value, order());
299     return this;
300   }
301 
getDouble()302   final public double getDouble ()
303   {
304     return ByteBufferHelper.getDouble (this, order());
305   }
306 
putDouble(double value)307   final public ByteBuffer putDouble (double value)
308   {
309     ByteBufferHelper.putDouble (this, value, order());
310     return this;
311   }
312 
getDouble(int index)313   final public double getDouble (int index)
314   {
315     return ByteBufferHelper.getDouble (this, index, order());
316   }
317 
putDouble(int index, double value)318   final public ByteBuffer putDouble (int index, double value)
319   {
320     ByteBufferHelper.putDouble (this, index, value, order());
321     return this;
322   }
323 }
324