author | coffeys |
Thu, 11 Aug 2011 12:40:24 +0100 | |
changeset 10325 | b72c20cd583a |
parent 7668 | d4a77089c587 |
child 17922 | d56eec572de5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#warn This file is preprocessed before being compiled |
|
27 |
||
28 |
package java.nio; |
|
29 |
||
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
30 |
import java.io.FileDescriptor; |
2 | 31 |
import sun.misc.Cleaner; |
32 |
import sun.misc.Unsafe; |
|
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
33 |
import sun.misc.VM; |
2 | 34 |
import sun.nio.ch.DirectBuffer; |
35 |
||
36 |
||
37 |
class Direct$Type$Buffer$RW$$BO$ |
|
38 |
#if[rw] |
|
39 |
extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer} |
|
40 |
#else[rw] |
|
41 |
extends Direct$Type$Buffer$BO$ |
|
42 |
#end[rw] |
|
43 |
implements DirectBuffer |
|
44 |
{ |
|
45 |
||
46 |
#if[rw] |
|
47 |
||
48 |
// Cached unsafe-access object |
|
49 |
protected static final Unsafe unsafe = Bits.unsafe(); |
|
50 |
||
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
51 |
// Cached array base offset |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
52 |
private static final long arrayBaseOffset = (long)unsafe.arrayBaseOffset($type$[].class); |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
53 |
|
2 | 54 |
// Cached unaligned-access capability |
55 |
protected static final boolean unaligned = Bits.unaligned(); |
|
56 |
||
57 |
// Base address, used in all indexing calculations |
|
58 |
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress |
|
59 |
// protected long address; |
|
60 |
||
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
61 |
// An object attached to this buffer. If this buffer is a view of another |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
62 |
// buffer then we use this field to keep a reference to that buffer to |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
63 |
// ensure that its memory isn't freed before we are done with it. |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
64 |
private final Object att; |
2 | 65 |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
66 |
public Object attachment() { |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
67 |
return att; |
2 | 68 |
} |
69 |
||
70 |
#if[byte] |
|
71 |
||
72 |
private static class Deallocator |
|
73 |
implements Runnable |
|
74 |
{ |
|
75 |
||
76 |
private static Unsafe unsafe = Unsafe.getUnsafe(); |
|
77 |
||
78 |
private long address; |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
79 |
private long size; |
2 | 80 |
private int capacity; |
81 |
||
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
82 |
private Deallocator(long address, long size, int capacity) { |
2 | 83 |
assert (address != 0); |
84 |
this.address = address; |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
85 |
this.size = size; |
2 | 86 |
this.capacity = capacity; |
87 |
} |
|
88 |
||
89 |
public void run() { |
|
90 |
if (address == 0) { |
|
91 |
// Paranoia |
|
92 |
return; |
|
93 |
} |
|
94 |
unsafe.freeMemory(address); |
|
95 |
address = 0; |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
96 |
Bits.unreserveMemory(size, capacity); |
2 | 97 |
} |
98 |
||
99 |
} |
|
100 |
||
101 |
private final Cleaner cleaner; |
|
102 |
||
103 |
public Cleaner cleaner() { return cleaner; } |
|
104 |
||
105 |
#else[byte] |
|
106 |
||
107 |
public Cleaner cleaner() { return null; } |
|
108 |
||
109 |
#end[byte] |
|
110 |
||
111 |
#end[rw] |
|
112 |
||
113 |
#if[byte] |
|
114 |
||
115 |
// Primary constructor |
|
116 |
// |
|
117 |
Direct$Type$Buffer$RW$(int cap) { // package-private |
|
118 |
#if[rw] |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
119 |
super(-1, 0, cap, cap); |
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
120 |
boolean pa = VM.isDirectMemoryPageAligned(); |
2 | 121 |
int ps = Bits.pageSize(); |
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
122 |
long size = Math.max(1L, (long)cap + (pa ? ps : 0)); |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
123 |
Bits.reserveMemory(size, cap); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
124 |
|
2 | 125 |
long base = 0; |
126 |
try { |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
127 |
base = unsafe.allocateMemory(size); |
2 | 128 |
} catch (OutOfMemoryError x) { |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
129 |
Bits.unreserveMemory(size, cap); |
2 | 130 |
throw x; |
131 |
} |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
132 |
unsafe.setMemory(base, size, (byte) 0); |
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
133 |
if (pa && (base % ps != 0)) { |
2 | 134 |
// Round up to page boundary |
135 |
address = base + ps - (base & (ps - 1)); |
|
136 |
} else { |
|
137 |
address = base; |
|
138 |
} |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
139 |
cleaner = Cleaner.create(this, new Deallocator(base, size, cap)); |
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
140 |
att = null; |
2 | 141 |
#else[rw] |
142 |
super(cap); |
|
143 |
#end[rw] |
|
144 |
} |
|
145 |
||
146 |
#if[rw] |
|
147 |
||
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
148 |
// Invoked to construct a direct ByteBuffer referring to the block of |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
149 |
// memory. A given arbitrary object may also be attached to the buffer. |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
150 |
// |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
151 |
Direct$Type$Buffer(long addr, int cap, Object ob) { |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
152 |
super(-1, 0, cap, cap); |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
153 |
address = addr; |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
154 |
cleaner = null; |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
155 |
att = ob; |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
156 |
} |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
157 |
|
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
158 |
|
2 | 159 |
// Invoked only by JNI: NewDirectByteBuffer(void*, long) |
160 |
// |
|
161 |
private Direct$Type$Buffer(long addr, int cap) { |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
162 |
super(-1, 0, cap, cap); |
2 | 163 |
address = addr; |
164 |
cleaner = null; |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
165 |
att = null; |
2 | 166 |
} |
167 |
||
168 |
#end[rw] |
|
169 |
||
170 |
// For memory-mapped buffers -- invoked by FileChannelImpl via reflection |
|
171 |
// |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
172 |
protected Direct$Type$Buffer$RW$(int cap, long addr, |
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
173 |
FileDescriptor fd, |
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
174 |
Runnable unmapper) |
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
175 |
{ |
2 | 176 |
#if[rw] |
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
177 |
super(-1, 0, cap, cap, fd); |
2 | 178 |
address = addr; |
179 |
cleaner = Cleaner.create(this, unmapper); |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
180 |
att = null; |
2 | 181 |
#else[rw] |
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
182 |
super(cap, addr, fd, unmapper); |
2 | 183 |
#end[rw] |
184 |
} |
|
185 |
||
186 |
#end[byte] |
|
187 |
||
188 |
// For duplicates and slices |
|
189 |
// |
|
190 |
Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private |
|
191 |
int mark, int pos, int lim, int cap, |
|
192 |
int off) |
|
193 |
{ |
|
194 |
#if[rw] |
|
195 |
super(mark, pos, lim, cap); |
|
196 |
address = db.address() + off; |
|
197 |
#if[byte] |
|
198 |
cleaner = null; |
|
199 |
#end[byte] |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
200 |
att = db; |
2 | 201 |
#else[rw] |
202 |
super(db, mark, pos, lim, cap, off); |
|
203 |
#end[rw] |
|
204 |
} |
|
205 |
||
206 |
public $Type$Buffer slice() { |
|
207 |
int pos = this.position(); |
|
208 |
int lim = this.limit(); |
|
209 |
assert (pos <= lim); |
|
210 |
int rem = (pos <= lim ? lim - pos : 0); |
|
211 |
int off = (pos << $LG_BYTES_PER_VALUE$); |
|
212 |
assert (off >= 0); |
|
213 |
return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off); |
|
214 |
} |
|
215 |
||
216 |
public $Type$Buffer duplicate() { |
|
217 |
return new Direct$Type$Buffer$RW$$BO$(this, |
|
218 |
this.markValue(), |
|
219 |
this.position(), |
|
220 |
this.limit(), |
|
221 |
this.capacity(), |
|
222 |
0); |
|
223 |
} |
|
224 |
||
225 |
public $Type$Buffer asReadOnlyBuffer() { |
|
226 |
#if[rw] |
|
227 |
return new Direct$Type$BufferR$BO$(this, |
|
228 |
this.markValue(), |
|
229 |
this.position(), |
|
230 |
this.limit(), |
|
231 |
this.capacity(), |
|
232 |
0); |
|
233 |
#else[rw] |
|
234 |
return duplicate(); |
|
235 |
#end[rw] |
|
236 |
} |
|
237 |
||
238 |
#if[rw] |
|
239 |
||
240 |
public long address() { |
|
241 |
return address; |
|
242 |
} |
|
243 |
||
244 |
private long ix(int i) { |
|
245 |
return address + (i << $LG_BYTES_PER_VALUE$); |
|
246 |
} |
|
247 |
||
248 |
public $type$ get() { |
|
249 |
return $fromBits$($swap$(unsafe.get$Swaptype$(ix(nextGetIndex())))); |
|
250 |
} |
|
251 |
||
252 |
public $type$ get(int i) { |
|
253 |
return $fromBits$($swap$(unsafe.get$Swaptype$(ix(checkIndex(i))))); |
|
254 |
} |
|
255 |
||
256 |
public $Type$Buffer get($type$[] dst, int offset, int length) { |
|
257 |
#if[rw] |
|
258 |
if ((length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) { |
|
259 |
checkBounds(offset, length, dst.length); |
|
260 |
int pos = position(); |
|
261 |
int lim = limit(); |
|
262 |
assert (pos <= lim); |
|
263 |
int rem = (pos <= lim ? lim - pos : 0); |
|
264 |
if (length > rem) |
|
265 |
throw new BufferUnderflowException(); |
|
266 |
||
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
267 |
#if[!byte] |
2 | 268 |
if (order() != ByteOrder.nativeOrder()) |
269 |
Bits.copyTo$Memtype$Array(ix(pos), dst, |
|
270 |
offset << $LG_BYTES_PER_VALUE$, |
|
271 |
length << $LG_BYTES_PER_VALUE$); |
|
272 |
else |
|
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
273 |
#end[!byte] |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
274 |
Bits.copyToArray(ix(pos), dst, arrayBaseOffset, |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
275 |
offset << $LG_BYTES_PER_VALUE$, |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
276 |
length << $LG_BYTES_PER_VALUE$); |
2 | 277 |
position(pos + length); |
278 |
} else { |
|
279 |
super.get(dst, offset, length); |
|
280 |
} |
|
281 |
return this; |
|
282 |
#else[rw] |
|
283 |
throw new ReadOnlyBufferException(); |
|
284 |
#end[rw] |
|
285 |
} |
|
286 |
||
287 |
#end[rw] |
|
288 |
||
289 |
public $Type$Buffer put($type$ x) { |
|
290 |
#if[rw] |
|
291 |
unsafe.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x))); |
|
292 |
return this; |
|
293 |
#else[rw] |
|
294 |
throw new ReadOnlyBufferException(); |
|
295 |
#end[rw] |
|
296 |
} |
|
297 |
||
298 |
public $Type$Buffer put(int i, $type$ x) { |
|
299 |
#if[rw] |
|
300 |
unsafe.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x))); |
|
301 |
return this; |
|
302 |
#else[rw] |
|
303 |
throw new ReadOnlyBufferException(); |
|
304 |
#end[rw] |
|
305 |
} |
|
306 |
||
307 |
public $Type$Buffer put($Type$Buffer src) { |
|
308 |
#if[rw] |
|
309 |
if (src instanceof Direct$Type$Buffer$BO$) { |
|
310 |
if (src == this) |
|
311 |
throw new IllegalArgumentException(); |
|
312 |
Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src; |
|
313 |
||
314 |
int spos = sb.position(); |
|
315 |
int slim = sb.limit(); |
|
316 |
assert (spos <= slim); |
|
317 |
int srem = (spos <= slim ? slim - spos : 0); |
|
318 |
||
319 |
int pos = position(); |
|
320 |
int lim = limit(); |
|
321 |
assert (pos <= lim); |
|
322 |
int rem = (pos <= lim ? lim - pos : 0); |
|
323 |
||
324 |
if (srem > rem) |
|
325 |
throw new BufferOverflowException(); |
|
326 |
unsafe.copyMemory(sb.ix(spos), ix(pos), srem << $LG_BYTES_PER_VALUE$); |
|
327 |
sb.position(spos + srem); |
|
328 |
position(pos + srem); |
|
329 |
} else if (src.hb != null) { |
|
330 |
||
331 |
int spos = src.position(); |
|
332 |
int slim = src.limit(); |
|
333 |
assert (spos <= slim); |
|
334 |
int srem = (spos <= slim ? slim - spos : 0); |
|
335 |
||
336 |
put(src.hb, src.offset + spos, srem); |
|
337 |
src.position(spos + srem); |
|
338 |
||
339 |
} else { |
|
340 |
super.put(src); |
|
341 |
} |
|
342 |
return this; |
|
343 |
#else[rw] |
|
344 |
throw new ReadOnlyBufferException(); |
|
345 |
#end[rw] |
|
346 |
} |
|
347 |
||
348 |
public $Type$Buffer put($type$[] src, int offset, int length) { |
|
349 |
#if[rw] |
|
350 |
if ((length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) { |
|
351 |
checkBounds(offset, length, src.length); |
|
352 |
int pos = position(); |
|
353 |
int lim = limit(); |
|
354 |
assert (pos <= lim); |
|
355 |
int rem = (pos <= lim ? lim - pos : 0); |
|
356 |
if (length > rem) |
|
357 |
throw new BufferOverflowException(); |
|
358 |
||
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
359 |
#if[!byte] |
2 | 360 |
if (order() != ByteOrder.nativeOrder()) |
361 |
Bits.copyFrom$Memtype$Array(src, offset << $LG_BYTES_PER_VALUE$, |
|
362 |
ix(pos), length << $LG_BYTES_PER_VALUE$); |
|
363 |
else |
|
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
364 |
#end[!byte] |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
365 |
Bits.copyFromArray(src, arrayBaseOffset, offset << $LG_BYTES_PER_VALUE$, |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
366 |
ix(pos), length << $LG_BYTES_PER_VALUE$); |
2 | 367 |
position(pos + length); |
368 |
} else { |
|
369 |
super.put(src, offset, length); |
|
370 |
} |
|
371 |
return this; |
|
372 |
#else[rw] |
|
373 |
throw new ReadOnlyBufferException(); |
|
374 |
#end[rw] |
|
375 |
} |
|
376 |
||
377 |
public $Type$Buffer compact() { |
|
378 |
#if[rw] |
|
379 |
int pos = position(); |
|
380 |
int lim = limit(); |
|
381 |
assert (pos <= lim); |
|
382 |
int rem = (pos <= lim ? lim - pos : 0); |
|
383 |
||
384 |
unsafe.copyMemory(ix(pos), ix(0), rem << $LG_BYTES_PER_VALUE$); |
|
385 |
position(rem); |
|
386 |
limit(capacity()); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
1247
diff
changeset
|
387 |
discardMark(); |
2 | 388 |
return this; |
389 |
#else[rw] |
|
390 |
throw new ReadOnlyBufferException(); |
|
391 |
#end[rw] |
|
392 |
} |
|
393 |
||
394 |
public boolean isDirect() { |
|
395 |
return true; |
|
396 |
} |
|
397 |
||
398 |
public boolean isReadOnly() { |
|
399 |
return {#if[rw]?false:true}; |
|
400 |
} |
|
401 |
||
402 |
||
403 |
#if[char] |
|
404 |
||
405 |
public String toString(int start, int end) { |
|
406 |
if ((end > limit()) || (start > end)) |
|
407 |
throw new IndexOutOfBoundsException(); |
|
408 |
try { |
|
409 |
int len = end - start; |
|
410 |
char[] ca = new char[len]; |
|
411 |
CharBuffer cb = CharBuffer.wrap(ca); |
|
412 |
CharBuffer db = this.duplicate(); |
|
413 |
db.position(start); |
|
414 |
db.limit(end); |
|
415 |
cb.put(db); |
|
416 |
return new String(ca); |
|
417 |
} catch (StringIndexOutOfBoundsException x) { |
|
418 |
throw new IndexOutOfBoundsException(); |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
||
423 |
// --- Methods to support CharSequence --- |
|
424 |
||
1224
8c1bc7c5dd00
6733145: (bf) CharBuffer.subSequence can be updated to take advantage of covariant returns
martin
parents:
1151
diff
changeset
|
425 |
public CharBuffer subSequence(int start, int end) { |
2 | 426 |
int pos = position(); |
427 |
int lim = limit(); |
|
428 |
assert (pos <= lim); |
|
429 |
pos = (pos <= lim ? pos : lim); |
|
430 |
int len = lim - pos; |
|
431 |
||
432 |
if ((start < 0) || (end > len) || (start > end)) |
|
433 |
throw new IndexOutOfBoundsException(); |
|
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
434 |
return new DirectCharBuffer$RW$$BO$(this, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
435 |
-1, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
436 |
pos + start, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
437 |
pos + end, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
438 |
capacity(), |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
439 |
offset); |
2 | 440 |
} |
441 |
||
442 |
#end[char] |
|
443 |
||
444 |
||
445 |
||
446 |
#if[!byte] |
|
447 |
||
448 |
public ByteOrder order() { |
|
449 |
#if[boS] |
|
450 |
return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) |
|
451 |
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); |
|
452 |
#end[boS] |
|
453 |
#if[boU] |
|
454 |
return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN) |
|
455 |
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); |
|
456 |
#end[boU] |
|
457 |
} |
|
458 |
||
459 |
#end[!byte] |
|
460 |
||
461 |
||
462 |
||
463 |
#if[byte] |
|
464 |
||
465 |
byte _get(int i) { // package-private |
|
466 |
return unsafe.getByte(address + i); |
|
467 |
} |
|
468 |
||
469 |
void _put(int i, byte b) { // package-private |
|
470 |
#if[rw] |
|
471 |
unsafe.putByte(address + i, b); |
|
472 |
#else[rw] |
|
473 |
throw new ReadOnlyBufferException(); |
|
474 |
#end[rw] |
|
475 |
} |
|
476 |
||
477 |
// #BIN |
|
478 |
// |
|
479 |
// Binary-data access methods for short, char, int, long, float, |
|
480 |
// and double will be inserted here |
|
481 |
||
482 |
#end[byte] |
|
483 |
||
484 |
} |