1 |
/* CharBufferImpl.java -- |
/* CharBufferImpl.java -- |
2 |
Copyright (C) 2002 Free Software Foundation, Inc. |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package gnu.java.nio; |
package gnu.java.nio; |
40 |
|
|
41 |
import java.nio.ByteBuffer; |
import java.nio.ByteBuffer; |
50 |
{ |
{ |
51 |
private boolean readOnly; |
private boolean readOnly; |
52 |
|
|
53 |
public CharBufferImpl(int cap, int off, int lim) |
public CharBufferImpl (int capacity) |
54 |
{ |
{ |
55 |
super (cap, lim, off, 0); |
this (new char [capacity], 0, capacity, capacity, 0, -1, false); |
|
this.backing_buffer = new char [cap]; |
|
|
readOnly = false; |
|
56 |
} |
} |
57 |
|
|
58 |
public CharBufferImpl(char[] array, int offset, int length) |
public CharBufferImpl (char[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly) |
59 |
{ |
{ |
60 |
super (array.length, length, offset, 0); |
super (buffer, offset, capacity, limit, position, mark); |
61 |
this.backing_buffer = array; |
this.readOnly = readOnly; |
|
readOnly = false; |
|
62 |
} |
} |
63 |
|
|
64 |
public CharBufferImpl (CharBufferImpl copy) |
public CharBufferImpl (CharBufferImpl copy) |
68 |
readOnly = copy.isReadOnly (); |
readOnly = copy.isReadOnly (); |
69 |
} |
} |
70 |
|
|
71 |
private static native char[] nio_cast (byte[] copy); |
public boolean isReadOnly () |
|
|
|
|
CharBufferImpl (byte[] copy) |
|
|
{ |
|
|
super (copy.length / 2, copy.length / 2, 0, 0); |
|
|
this.backing_buffer = (copy != null ? nio_cast (copy) : null); |
|
|
readOnly = false; |
|
|
} |
|
|
|
|
|
private static native byte nio_get_Byte (CharBufferImpl b, int index, int limit); |
|
|
|
|
|
private static native void nio_put_Byte (CharBufferImpl b, int index, int limit, byte value); |
|
|
|
|
|
public ByteBuffer asByteBuffer () |
|
|
{ |
|
|
ByteBufferImpl res = new ByteBufferImpl (backing_buffer); |
|
|
res.limit ((limit () * 1) / 2); |
|
|
return res; |
|
|
} |
|
|
|
|
|
|
|
|
public boolean isReadOnly() |
|
72 |
{ |
{ |
73 |
return readOnly; |
return readOnly; |
74 |
} |
} |
75 |
|
|
76 |
public CharBuffer slice() |
public CharBuffer slice () |
77 |
{ |
{ |
78 |
return new CharBufferImpl (backing_buffer, arrayOffset () + position (), |
return new CharBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ()); |
|
remaining ()); |
|
79 |
} |
} |
80 |
|
|
81 |
public CharBuffer duplicate() |
public CharBuffer duplicate () |
82 |
{ |
{ |
83 |
return new CharBufferImpl(this); |
return new CharBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ()); |
84 |
} |
} |
85 |
|
|
86 |
public CharBuffer asReadOnlyBuffer() |
public CharBuffer asReadOnlyBuffer () |
87 |
{ |
{ |
88 |
CharBufferImpl result = new CharBufferImpl (this); |
return new CharBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true); |
|
result.readOnly = true; |
|
|
return result; |
|
89 |
} |
} |
90 |
|
|
91 |
public CharBuffer compact() |
public CharBuffer compact () |
92 |
{ |
{ |
93 |
|
int copied = 0; |
94 |
|
|
95 |
|
while (remaining () > 0) |
96 |
|
{ |
97 |
|
put (copied, get ()); |
98 |
|
copied++; |
99 |
|
} |
100 |
|
|
101 |
|
position (copied); |
102 |
return this; |
return this; |
103 |
} |
} |
104 |
|
|
105 |
public boolean isDirect() |
public boolean isDirect () |
106 |
{ |
{ |
107 |
return false; |
return false; |
108 |
} |
} |
115 |
|| end > length ()) |
|| end > length ()) |
116 |
throw new IndexOutOfBoundsException (); |
throw new IndexOutOfBoundsException (); |
117 |
|
|
118 |
// No support for direct buffers yet. |
return new CharBufferImpl (backing_buffer, array_offset, capacity (), position () + end, position () + start, -1, isReadOnly ()); |
|
// assert array () != null; |
|
|
return new CharBufferImpl (array (), position () + start, |
|
|
position () + end); |
|
119 |
} |
} |
120 |
|
|
121 |
/** |
/** |
122 |
* Relative get method. Reads the next character from the buffer. |
* Relative get method. Reads the next <code>char</code> from the buffer. |
123 |
*/ |
*/ |
124 |
final public char get() |
final public char get () |
125 |
{ |
{ |
126 |
char e = backing_buffer[position()]; |
char result = backing_buffer [position ()]; |
127 |
position(position()+1); |
position (position () + 1); |
128 |
return e; |
return result; |
129 |
} |
} |
130 |
|
|
131 |
/** |
/** |
134 |
* |
* |
135 |
* @exception ReadOnlyBufferException If this buffer is read-only. |
* @exception ReadOnlyBufferException If this buffer is read-only. |
136 |
*/ |
*/ |
137 |
final public CharBuffer put(char b) |
final public CharBuffer put (char value) |
138 |
{ |
{ |
139 |
if (readOnly) |
if (readOnly) |
140 |
throw new ReadOnlyBufferException (); |
throw new ReadOnlyBufferException (); |
141 |
|
|
142 |
backing_buffer[position()] = b; |
backing_buffer [position ()] = value; |
143 |
position(position()+1); |
position (position () + 1); |
144 |
return this; |
return this; |
145 |
} |
} |
146 |
|
|
147 |
/** |
/** |
148 |
* Absolute get method. Reads the character at position <code>index</code>. |
* Absolute get method. Reads the <code>char</code> at position |
149 |
|
* <code>index</code>. |
150 |
* |
* |
151 |
* @exception IndexOutOfBoundsException If index is negative or not smaller |
* @exception IndexOutOfBoundsException If index is negative or not smaller |
152 |
* than the buffer's limit. |
* than the buffer's limit. |
153 |
*/ |
*/ |
154 |
final public char get(int index) |
final public char get (int index) |
155 |
{ |
{ |
156 |
if (index < 0 |
if (index < 0 |
157 |
|| index >= limit ()) |
|| index >= limit ()) |
158 |
throw new IndexOutOfBoundsException (); |
throw new IndexOutOfBoundsException (); |
159 |
|
|
160 |
return backing_buffer[index]; |
return backing_buffer [index]; |
161 |
} |
} |
162 |
|
|
163 |
/** |
/** |
168 |
* than the buffer's limit. |
* than the buffer's limit. |
169 |
* @exception ReadOnlyBufferException If this buffer is read-only. |
* @exception ReadOnlyBufferException If this buffer is read-only. |
170 |
*/ |
*/ |
171 |
final public CharBuffer put(int index, char b) |
final public CharBuffer put (int index, char value) |
172 |
{ |
{ |
173 |
if (index < 0 |
if (index < 0 |
174 |
|| index >= limit ()) |
|| index >= limit ()) |
176 |
|
|
177 |
if (readOnly) |
if (readOnly) |
178 |
throw new ReadOnlyBufferException (); |
throw new ReadOnlyBufferException (); |
179 |
|
|
180 |
backing_buffer[index] = b; |
backing_buffer [index] = value; |
181 |
return this; |
return this; |
182 |
} |
} |
183 |
|
|
184 |
|
final public ByteOrder order () |
|
public final ByteOrder order() |
|
185 |
{ |
{ |
186 |
return ByteOrder.BIG_ENDIAN; |
return ByteOrder.nativeOrder (); |
187 |
} |
} |
188 |
} |
} |