1 /* ByteBufferImpl.java --
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package java.nio;
40
41 /**
42 * This is a Heap memory implementation
43 */
44 final class ByteBufferImpl extends ByteBuffer
45 {
46 private boolean readOnly;
47
48 ByteBufferImpl (byte[] buffer, int offset, int capacity, int limit, int position, int mark, boolean readOnly)
49 {
50 super (capacity, limit, position, mark);
51 this.backing_buffer = buffer;
52 this.array_offset = offset;
53 this.readOnly = readOnly;
54 }
55
56 public CharBuffer asCharBuffer ()
57 {
58 return new CharViewBufferImpl (this, remaining() >> 1);
59 }
60
61 public ShortBuffer asShortBuffer ()
62 {
63 return new ShortViewBufferImpl (this, remaining() >> 1);
64 }
65
66 public IntBuffer asIntBuffer ()
67 {
68 return new IntViewBufferImpl (this, remaining() >> 2);
69 }
70
71 public LongBuffer asLongBuffer ()
72 {
73 return new LongViewBufferImpl (this, remaining() >> 3);
74 }
75
76 public FloatBuffer asFloatBuffer ()
77 {
78 return new FloatViewBufferImpl (this, remaining() >> 2);
79 }
80
81 public DoubleBuffer asDoubleBuffer ()
82 {
83 return new DoubleViewBufferImpl (this, remaining() >> 3);
84 }
85
86 public boolean isReadOnly ()
87 {
88 return readOnly;
89 }
90
91 public ByteBuffer slice ()
92 {
93 return new ByteBufferImpl (backing_buffer, array_offset + position (), remaining (), remaining (), 0, -1, isReadOnly ());
94 }
95
96 public ByteBuffer duplicate ()
97 {
98 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, isReadOnly ());
99 }
100
101 public ByteBuffer asReadOnlyBuffer ()
102 {
103 return new ByteBufferImpl (backing_buffer, array_offset, capacity (), limit (), position (), mark, true);
104 }
105
106 void shiftDown (int dst_offset, int src_offset, int count)
107 {
108 System.arraycopy(backing_buffer, array_offset + src_offset,
109 backing_buffer, array_offset + dst_offset,
110 count);
111 }
112
113 public ByteBuffer compact ()
114 {
115 checkIfReadOnly();
116 mark = -1;
117 int pos = position();
118 int n = limit() - pos;
119 if (n > 0)
120 shiftDown(0, pos, n);
121 position(n);
122 limit(capacity());
123 return this;
124 }
125
126 public boolean isDirect ()
127 {
128 return false;
129 }
130
131 /**
132 * Reads the <code>byte</code> at this buffer's current position,
133 * and then increments the position.
134 *
135 * @exception BufferUnderflowException If there are no remaining
136 * <code>bytes</code> in this buffer.
137 */
138 public byte get ()
139 {
140 if (pos >= limit)
141 throw new BufferUnderflowException();
142
143 return backing_buffer [(pos++) + array_offset];
144 }
145
146 /**
147 * Bulk get
148 */
149 public ByteBuffer get (byte[] dst, int offset, int length)
150 {
151 checkArraySize(dst.length, offset, length);
152 if ( (limit - pos) < length) // check for overflow
153 throw new BufferUnderflowException();
154
155 System.arraycopy(backing_buffer, pos + array_offset,
156 dst, offset, length);
157 pos += length;
158
159 return this;
160 }
161
162 /**
163 * Relative bulk put(), overloads the ByteBuffer impl.
164 */
165 public ByteBuffer put (byte[] src, int offset, int length)
166 {
167 if ( (limit - pos) < length) // check for overflow
168 throw new BufferOverflowException();
169 checkArraySize(src.length, offset, length);
170
171 System.arraycopy(src, offset, backing_buffer, pos + array_offset, length);
172 pos += length;
173
174 return this;
175 }
176
177 /**
178 * Relative put method. Writes <code>value</code> to the next position
179 * in the buffer.
180 *
181 * @exception BufferOverflowException If there is no remaining
182 * space in this buffer.
183 * @exception ReadOnlyBufferException If this buffer is read-only.
184 */
185 public ByteBuffer put (byte value)
186 {
187 if (readOnly)
188 throw new ReadOnlyBufferException();
189 if (pos >= limit)
190 throw new BufferOverflowException();
191
192 backing_buffer [(pos++) + array_offset] = value;
193 return this;
194 }
195
196 /**
197 * Absolute get method. Reads the <code>byte</code> at position
198 * <code>index</code>.
199 *
200 * @exception IndexOutOfBoundsException If index is negative or not smaller
201 * than the buffer's limit.
202 */
203 public byte get (int index)
204 {
205 checkIndex(index);
206
207 return backing_buffer [index + array_offset];
208 }
209
210 /**
211 * Absolute put method. Writes <code>value</code> to position
212 * <code>index</code> in the buffer.
213 *
214 * @exception IndexOutOfBoundsException If index is negative or not smaller
215 * than the buffer's limit.
216 * @exception ReadOnlyBufferException If this buffer is read-only.
217 */
218 public ByteBuffer put (int index, byte value)
219 {
220 checkIfReadOnly();
221 checkIndex(index);
222
223 backing_buffer [index + array_offset] = value;
224 return this;
225 }
226
227 public char getChar ()
228 {
229 return ByteBufferHelper.getChar(this, order());
230 }
231
232 public ByteBuffer putChar (char value)
233 {
234 if (readOnly)
235 throw new ReadOnlyBufferException ();
236 if ( (limit-pos) < 2)
237 throw new BufferOverflowException();
238
239 if (endian == ByteOrder.LITTLE_ENDIAN)
240 {
241 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
242 backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
243 }
244 else
245 {
246 backing_buffer [(pos++) + array_offset] = (byte)(value>>8);
247 backing_buffer [(pos++) + array_offset] = (byte)(value&0xFF);
248 }
249 return this;
250 }
251
252 public char getChar (int index)
253 {
254 return ByteBufferHelper.getChar(this, index, order());
255 }
256
257 public ByteBuffer putChar (int index, char value)
258 {
259 ByteBufferHelper.putChar(this, index, value, order());
260 return this;
261 }
262
263 public short getShort ()
264 {
265 return ByteBufferHelper.getShort(this, order());
266 }
267
268 public ByteBuffer putShort (short value)
269 {
270 ByteBufferHelper.putShort(this, value, order());
271 return this;
272 }
273
274 public short getShort (int index)
275 {
276 return ByteBufferHelper.getShort(this, index, order());
277 }
278
279 public ByteBuffer putShort (int index, short value)
280 {
281 ByteBufferHelper.putShort(this, index, value, order());
282 return this;
283 }
284
285 public int getInt ()
286 {
287 return ByteBufferHelper.getInt(this, order());
288 }
289
290 public ByteBuffer putInt (int value)
291 {
292 ByteBufferHelper.putInt(this, value, order());
293 return this;
294 }
295
296 public int getInt (int index)
297 {
298 return ByteBufferHelper.getInt(this, index, order());
299 }
300
301 public ByteBuffer putInt (int index, int value)
302 {
303 ByteBufferHelper.putInt(this, index, value, order());
304 return this;
305 }
306
307 public long getLong ()
308 {
309 return ByteBufferHelper.getLong(this, order());
310 }
311
312 public ByteBuffer putLong (long value)
313 {
314 ByteBufferHelper.putLong (this, value, order());
315 return this;
316 }
317
318 public long getLong (int index)
319 {
320 return ByteBufferHelper.getLong (this, index, order());
321 }
322
323 public ByteBuffer putLong (int index, long value)
324 {
325 ByteBufferHelper.putLong (this, index, value, order());
326 return this;
327 }
328
329 public float getFloat ()
330 {
331 return ByteBufferHelper.getFloat (this, order());
332 }
333
334 public ByteBuffer putFloat (float value)
335 {
336 ByteBufferHelper.putFloat (this, value, order());
337 return this;
338 }
339
340 public float getFloat (int index)
341 {
342 return ByteBufferHelper.getFloat (this, index, order());
343 }
344
345 public ByteBuffer putFloat (int index, float value)
346 {
347 ByteBufferHelper.putFloat (this, index, value, order());
348 return this;
349 }
350
351 public double getDouble ()
352 {
353 return ByteBufferHelper.getDouble (this, order());
354 }
355
356 public ByteBuffer putDouble (double value)
357 {
358 ByteBufferHelper.putDouble (this, value, order());
359 return this;
360 }
361
362 public double getDouble (int index)
363 {
364 return ByteBufferHelper.getDouble (this, index, order());
365 }
366
367 public ByteBuffer putDouble (int index, double value)
368 {
369 ByteBufferHelper.putDouble (this, index, value, order());
370 return this;
371 }
372 }