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