Skip to content

Commit f0190a2

Browse files
hansonrhansonr
authored andcommitted
java.nio.ByteBuffer, CharBuffer
1 parent ec8dfb8 commit f0190a2

23 files changed

+2029
-36
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package java.nio;
2+
3+
public class BufferOverflowException
4+
extends RuntimeException
5+
{
6+
private static final long serialVersionUID = -5484897634319144535L;
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package java.nio;
2+
3+
public class BufferUnderflowException
4+
extends RuntimeException
5+
{
6+
private static final long serialVersionUID = -1713313658691622206L;
7+
}

sources/net.sf.j2s.java.core/src/java/nio/ByteBuffer.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,19 @@ public abstract class ByteBuffer
262262
// reduce the number of virtual method invocations needed to access these
263263
// values, which is especially costly when coding small buffers.
264264
//
265-
final byte[] hb; // Non-null only for heap buffers
266-
final int offset;
265+
final protected byte[] hb; // Non-null only for heap buffers
266+
final protected int offset;
267267
boolean isReadOnly; // Valid only for heap buffers
268268

269-
// Creates a new buffer with the given mark, position, limit, capacity,
270-
// backing array, and array offset
271-
//
272-
ByteBuffer(int mark, int pos, int lim, int cap, // package-private
273-
byte[] hb, int offset)
274-
{
275-
super(mark, pos, lim, cap);
276-
this.hb = hb;
277-
this.offset = offset;
278-
}
269+
// Creates a new buffer with the given mark, position, limit, capacity,
270+
// backing array, and array offset
271+
//
272+
public ByteBuffer(int mark, int pos, int lim, int cap, // package-private SwingJS -- NOT
273+
byte[] hb, int offset) {
274+
super(mark, pos, lim, cap);
275+
this.hb = hb;
276+
this.offset = offset;
277+
}
279278

280279
// Creates a new buffer with the given mark, position, limit, and capacity
281280
//
@@ -1042,8 +1041,8 @@ public final ByteBuffer order(ByteOrder bo) {
10421041

10431042
// Unchecked accessors, for use by ByteBufferAs-X-Buffer classes
10441043
//
1045-
abstract byte _get(int i); // package-private
1046-
abstract void _put(int i, byte b); // package-private
1044+
public abstract byte _get(int i); // package-private SwingJS -- NOT
1045+
public abstract void _put(int i, byte b); // package-private SwingJS -- NOT
10471046

10481047
// #BIN
10491048
//
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
*
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.
24+
*/
25+
26+
#warn This file is preprocessed before being compiled
27+
28+
package java.nio;
29+
30+
31+
class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private
32+
extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$}
33+
{
34+
35+
#if[rw]
36+
37+
protected final ByteBuffer bb;
38+
protected final int offset;
39+
40+
#end[rw]
41+
42+
ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb) { // package-private
43+
#if[rw]
44+
super(-1, 0,
45+
bb.remaining() >> $LG_BYTES_PER_VALUE$,
46+
bb.remaining() >> $LG_BYTES_PER_VALUE$);
47+
this.bb = bb;
48+
// enforce limit == capacity
49+
int cap = this.capacity();
50+
this.limit(cap);
51+
int pos = this.position();
52+
assert (pos <= cap);
53+
offset = pos;
54+
#else[rw]
55+
super(bb);
56+
#end[rw]
57+
}
58+
59+
ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb,
60+
int mark, int pos, int lim, int cap,
61+
int off)
62+
{
63+
#if[rw]
64+
super(mark, pos, lim, cap);
65+
this.bb = bb;
66+
offset = off;
67+
#else[rw]
68+
super(bb, mark, pos, lim, cap, off);
69+
#end[rw]
70+
}
71+
72+
public $Type$Buffer slice() {
73+
int pos = this.position();
74+
int lim = this.limit();
75+
assert (pos <= lim);
76+
int rem = (pos <= lim ? lim - pos : 0);
77+
int off = (pos << $LG_BYTES_PER_VALUE$) + offset;
78+
assert (off >= 0);
79+
return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, off);
80+
}
81+
82+
public $Type$Buffer duplicate() {
83+
return new ByteBufferAs$Type$Buffer$RW$$BO$(bb,
84+
this.markValue(),
85+
this.position(),
86+
this.limit(),
87+
this.capacity(),
88+
offset);
89+
}
90+
91+
public $Type$Buffer asReadOnlyBuffer() {
92+
#if[rw]
93+
return new ByteBufferAs$Type$BufferR$BO$(bb,
94+
this.markValue(),
95+
this.position(),
96+
this.limit(),
97+
this.capacity(),
98+
offset);
99+
#else[rw]
100+
return duplicate();
101+
#end[rw]
102+
}
103+
104+
#if[rw]
105+
106+
protected int ix(int i) {
107+
return (i << $LG_BYTES_PER_VALUE$) + offset;
108+
}
109+
110+
public $type$ get() {
111+
return Bits.get$Type$$BO$(bb, ix(nextGetIndex()));
112+
}
113+
114+
public $type$ get(int i) {
115+
return Bits.get$Type$$BO$(bb, ix(checkIndex(i)));
116+
}
117+
118+
#if[streamableType]
119+
$type$ getUnchecked(int i) {
120+
return Bits.get$Type$$BO$(bb, ix(i));
121+
}
122+
#end[streamableType]
123+
124+
#end[rw]
125+
126+
public $Type$Buffer put($type$ x) {
127+
#if[rw]
128+
Bits.put$Type$$BO$(bb, ix(nextPutIndex()), x);
129+
return this;
130+
#else[rw]
131+
throw new ReadOnlyBufferException();
132+
#end[rw]
133+
}
134+
135+
public $Type$Buffer put(int i, $type$ x) {
136+
#if[rw]
137+
Bits.put$Type$$BO$(bb, ix(checkIndex(i)), x);
138+
return this;
139+
#else[rw]
140+
throw new ReadOnlyBufferException();
141+
#end[rw]
142+
}
143+
144+
public $Type$Buffer compact() {
145+
#if[rw]
146+
int pos = position();
147+
int lim = limit();
148+
assert (pos <= lim);
149+
int rem = (pos <= lim ? lim - pos : 0);
150+
151+
ByteBuffer db = bb.duplicate();
152+
db.limit(ix(lim));
153+
db.position(ix(0));
154+
ByteBuffer sb = db.slice();
155+
sb.position(pos << $LG_BYTES_PER_VALUE$);
156+
sb.compact();
157+
position(rem);
158+
limit(capacity());
159+
discardMark();
160+
return this;
161+
#else[rw]
162+
throw new ReadOnlyBufferException();
163+
#end[rw]
164+
}
165+
166+
public boolean isDirect() {
167+
return bb.isDirect();
168+
}
169+
170+
public boolean isReadOnly() {
171+
return {#if[rw]?false:true};
172+
}
173+
174+
#if[char]
175+
176+
public String toString(int start, int end) {
177+
if ((end > limit()) || (start > end))
178+
throw new IndexOutOfBoundsException();
179+
try {
180+
int len = end - start;
181+
char[] ca = new char[len];
182+
CharBuffer cb = CharBuffer.wrap(ca);
183+
CharBuffer db = this.duplicate();
184+
db.position(start);
185+
db.limit(end);
186+
cb.put(db);
187+
return new String(ca);
188+
} catch (StringIndexOutOfBoundsException x) {
189+
throw new IndexOutOfBoundsException();
190+
}
191+
}
192+
193+
194+
// --- Methods to support CharSequence ---
195+
196+
public CharBuffer subSequence(int start, int end) {
197+
int pos = position();
198+
int lim = limit();
199+
assert (pos <= lim);
200+
pos = (pos <= lim ? pos : lim);
201+
int len = lim - pos;
202+
203+
if ((start < 0) || (end > len) || (start > end))
204+
throw new IndexOutOfBoundsException();
205+
return new ByteBufferAsCharBuffer$RW$$BO$(bb,
206+
-1,
207+
pos + start,
208+
pos + end,
209+
capacity(),
210+
offset);
211+
}
212+
213+
#end[char]
214+
215+
216+
public ByteOrder order() {
217+
#if[boB]
218+
return ByteOrder.BIG_ENDIAN;
219+
#end[boB]
220+
#if[boL]
221+
return ByteOrder.LITTLE_ENDIAN;
222+
#end[boL]
223+
}
224+
225+
}

0 commit comments

Comments
 (0)