40 |
import java.nio.ByteBuffer; |
import java.nio.ByteBuffer; |
41 |
import java.nio.ByteOrder; |
import java.nio.ByteOrder; |
42 |
import java.nio.CharBuffer; |
import java.nio.CharBuffer; |
43 |
|
import java.nio.ReadOnlyBufferException; |
44 |
|
|
45 |
|
/** |
46 |
|
* This is a Heap memory implementation |
47 |
|
*/ |
48 |
public final class CharBufferImpl extends CharBuffer |
public final class CharBufferImpl extends CharBuffer |
49 |
{ |
{ |
50 |
private boolean ro; |
private boolean readOnly; |
51 |
|
|
52 |
public CharBufferImpl(int cap, int off, int lim) |
public CharBufferImpl(int cap, int off, int lim) |
53 |
{ |
{ |
54 |
this.backing_buffer = new char[cap]; |
super (cap, lim, off, 0); |
55 |
this.cap = cap; |
this.backing_buffer = new char [cap]; |
56 |
this.limit(lim); |
readOnly = false; |
|
this.position(off); |
|
57 |
} |
} |
58 |
|
|
59 |
public CharBufferImpl(char[] array, int off, int lim) |
public CharBufferImpl(char[] array, int offset, int length) |
60 |
{ |
{ |
61 |
|
// FIXME: check if IndexOutOfBoundsException is thrown correctly |
62 |
|
super (array.length, length, offset, 0); |
63 |
this.backing_buffer = array; |
this.backing_buffer = array; |
64 |
this.cap = array.length; |
readOnly = false; |
|
this.limit(lim); |
|
|
this.position(off); |
|
65 |
} |
} |
66 |
|
|
67 |
public CharBufferImpl (CharBufferImpl copy) |
public CharBufferImpl (CharBufferImpl copy) |
68 |
{ |
{ |
69 |
|
super (copy.capacity (), copy.limit (), copy.position (), 0); |
70 |
backing_buffer = copy.backing_buffer; |
backing_buffer = copy.backing_buffer; |
71 |
ro = copy.ro; |
readOnly = copy.isReadOnly (); |
|
limit (copy.limit()); |
|
|
position (copy.position ()); |
|
|
} |
|
|
|
|
|
void inc_pos (int a) |
|
|
{ |
|
|
position (position () + a); |
|
72 |
} |
} |
73 |
|
|
74 |
private static native char[] nio_cast (byte[] copy); |
private static native char[] nio_cast (byte[] copy); |
75 |
|
|
76 |
CharBufferImpl (byte[] copy) |
CharBufferImpl (byte[] copy) |
77 |
{ |
{ |
78 |
this.backing_buffer = copy != null ? nio_cast (copy) : null; |
super (copy.length / 2, copy.length / 2, 0, 0); |
79 |
|
this.backing_buffer = (copy != null ? nio_cast (copy) : null); |
80 |
|
readOnly = false; |
81 |
} |
} |
82 |
|
|
83 |
private static native byte nio_get_Byte (CharBufferImpl b, int index, int limit); |
private static native byte nio_get_Byte (CharBufferImpl b, int index, int limit); |
91 |
return res; |
return res; |
92 |
} |
} |
93 |
|
|
94 |
|
|
95 |
public boolean isReadOnly() |
public boolean isReadOnly() |
96 |
{ |
{ |
97 |
return ro; |
return readOnly; |
98 |
} |
} |
99 |
|
|
100 |
public CharBuffer slice() |
public CharBuffer slice() |
101 |
{ |
{ |
102 |
CharBufferImpl A = new CharBufferImpl(this); |
return new CharBufferImpl (backing_buffer, arrayOffset () + position (), |
103 |
A.array_offset = position(); |
remaining ()); |
|
return A; |
|
104 |
} |
} |
105 |
|
|
106 |
public CharBuffer duplicate() |
public CharBuffer duplicate() |
110 |
|
|
111 |
public CharBuffer asReadOnlyBuffer() |
public CharBuffer asReadOnlyBuffer() |
112 |
{ |
{ |
113 |
CharBufferImpl a = new CharBufferImpl(this); |
CharBufferImpl result = new CharBufferImpl (this); |
114 |
a.ro = true; |
result.readOnly = true; |
115 |
return a; |
return result; |
116 |
} |
} |
117 |
|
|
118 |
public CharBuffer compact() |
public CharBuffer compact() |
122 |
|
|
123 |
public boolean isDirect() |
public boolean isDirect() |
124 |
{ |
{ |
125 |
return backing_buffer != null; |
return false; |
126 |
} |
} |
127 |
|
|
128 |
final public CharSequence subSequence (int start, int end) |
final public CharSequence subSequence (int start, int end) |
129 |
{ |
{ |
130 |
if (start < 0 || |
if (start < 0 |
131 |
end > length () || |
|| start > length () |
132 |
start > end) |
|| end < start |
133 |
|
|| end > length ()) |
134 |
throw new IndexOutOfBoundsException (); |
throw new IndexOutOfBoundsException (); |
135 |
|
|
136 |
// No support for direct buffers yet. |
// No support for direct buffers yet. |
139 |
position () + end); |
position () + end); |
140 |
} |
} |
141 |
|
|
142 |
|
/** |
143 |
|
* Relative get method. Reads the next character from the buffer. |
144 |
|
*/ |
145 |
final public char get() |
final public char get() |
146 |
{ |
{ |
147 |
char e = backing_buffer[position()]; |
char e = backing_buffer[position()]; |
149 |
return e; |
return e; |
150 |
} |
} |
151 |
|
|
152 |
|
/** |
153 |
|
* Relative put method. Writes <code>value</code> to the next position |
154 |
|
* in the buffer. |
155 |
|
* |
156 |
|
* @exception ReadOnlyBufferException If this buffer is read-only. |
157 |
|
*/ |
158 |
final public CharBuffer put(char b) |
final public CharBuffer put(char b) |
159 |
{ |
{ |
160 |
|
if (readOnly) |
161 |
|
throw new ReadOnlyBufferException (); |
162 |
|
|
163 |
backing_buffer[position()] = b; |
backing_buffer[position()] = b; |
164 |
position(position()+1); |
position(position()+1); |
165 |
return this; |
return this; |
166 |
} |
} |
167 |
|
|
168 |
|
/** |
169 |
|
* Absolute get method. Reads the character at position <code>index</code>. |
170 |
|
* |
171 |
|
* @exception IndexOutOfBoundsException If index is negative or not smaller |
172 |
|
* than the buffer's limit. |
173 |
|
*/ |
174 |
final public char get(int index) |
final public char get(int index) |
175 |
{ |
{ |
176 |
|
if (index < 0 |
177 |
|
|| index >= limit ()) |
178 |
|
throw new IndexOutOfBoundsException (); |
179 |
|
|
180 |
return backing_buffer[index]; |
return backing_buffer[index]; |
181 |
} |
} |
182 |
|
|
183 |
|
/** |
184 |
|
* Absolute put method. Writes <code>value</value> to position |
185 |
|
* <code>index</code> in the buffer. |
186 |
|
* |
187 |
|
* @exception IndexOutOfBoundsException If index is negative or not smaller |
188 |
|
* than the buffer's limit. |
189 |
|
* @exception ReadOnlyBufferException If this buffer is read-only. |
190 |
|
*/ |
191 |
final public CharBuffer put(int index, char b) |
final public CharBuffer put(int index, char b) |
192 |
{ |
{ |
193 |
|
if (index < 0 |
194 |
|
|| index >= limit ()) |
195 |
|
throw new IndexOutOfBoundsException (); |
196 |
|
|
197 |
|
if (readOnly) |
198 |
|
throw new ReadOnlyBufferException (); |
199 |
|
|
200 |
backing_buffer[index] = b; |
backing_buffer[index] = b; |
201 |
return this; |
return this; |
202 |
} |
} |