• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package java.nio;
19 
20 import com.google.gwt.corp.compatibility.Endianness;
21 import com.google.gwt.corp.compatibility.StringToByteBuffer;
22 
23 /** A buffer for bytes.
24  * <p>
25  * A byte buffer can be created in either one of the following ways:
26  * </p>
27  * <ul>
28  * <li>{@link #allocate(int) Allocate} a new byte array and create a buffer based on it;</li>
29  * <li>{@link #allocateDirect(int) Allocate} a memory block and create a direct buffer based on it;</li>
30  * <li>{@link #wrap(byte[]) Wrap} an existing byte array to create a new buffer.</li>
31  * </ul>
32  * @since Android 1.0 */
33 public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer>, StringToByteBuffer {
34 
35 	/** Creates a byte buffer based on a newly allocated byte array.
36 	 *
37 	 * @param capacity the capacity of the new buffer
38 	 * @return the created byte buffer.
39 	 * @throws IllegalArgumentException if {@code capacity < 0}.
40 	 * @since Android 1.0 */
allocate(int capacity)41 	public static ByteBuffer allocate (int capacity) {
42 		if (capacity < 0) {
43 			throw new IllegalArgumentException();
44 		}
45 		return BufferFactory.newByteBuffer(capacity);
46 	}
47 
48 	/** Creates a direct byte buffer based on a newly allocated memory block.
49 	 *
50 	 * @param capacity the capacity of the new buffer
51 	 * @return the created byte buffer.
52 	 * @throws IllegalArgumentException if {@code capacity < 0}.
53 	 * @since Android 1.0 */
allocateDirect(int capacity)54 	public static ByteBuffer allocateDirect (int capacity) {
55 		if (capacity < 0) {
56 			throw new IllegalArgumentException();
57 		}
58 		return BufferFactory.newDirectByteBuffer(capacity);
59 	}
60 
61 	/** Creates a new byte buffer by wrapping the given byte array.
62 	 * <p>
63 	 * Calling this method has the same effect as {@code wrap(array, 0, array.length)}.
64 	 * </p>
65 	 *
66 	 * @param array the byte array which the new buffer will be based on
67 	 * @return the created byte buffer.
68 	 * @since Android 1.0 */
wrap(byte[] array)69 	public static ByteBuffer wrap (byte[] array) {
70 		return BufferFactory.newByteBuffer(array);
71 	}
72 
73 	/** Creates a new byte buffer by wrapping the given byte array.
74 	 * <p>
75 	 * The new buffer's position will be {@code start}, limit will be {@code start + len}, capacity will be the length of the array.
76 	 * </p>
77 	 *
78 	 * @param array the byte array which the new buffer will be based on.
79 	 * @param start the start index, must not be negative and not greater than {@code array.length}.
80 	 * @param len the length, must not be negative and not greater than {@code array.length - start}.
81 	 * @return the created byte buffer.
82 	 * @exception IndexOutOfBoundsException if either {@code start} or {@code len} is invalid.
83 	 * @since Android 1.0 */
wrap(byte[] array, int start, int len)84 	public static ByteBuffer wrap (byte[] array, int start, int len) {
85 		int length = array.length;
86 		if ((start < 0) || (len < 0) || ((long)start + (long)len > length)) {
87 			throw new IndexOutOfBoundsException();
88 		}
89 
90 		ByteBuffer buf = BufferFactory.newByteBuffer(array);
91 		buf.position = start;
92 		buf.limit = start + len;
93 
94 		return buf;
95 	}
96 
97 	/** The byte order of this buffer, default is {@code BIG_ENDIAN}. */
98 	Endianness order = Endianness.BIG_ENDIAN;
99 
100 	/** Constructs a {@code ByteBuffer} with given capacity.
101 	 *
102 	 * @param capacity the capacity of the buffer. */
ByteBuffer(int capacity)103 	ByteBuffer (int capacity) {
104 		super(capacity);
105 	}
106 
107 	/** Returns the byte array which this buffer is based on, if there is one.
108 	 *
109 	 * @return the byte array which this buffer is based on.
110 	 * @exception ReadOnlyBufferException if this buffer is based on a read-only array.
111 	 * @exception UnsupportedOperationException if this buffer is not based on an array.
112 	 * @since Android 1.0 */
array()113 	public final byte[] array () {
114 		return protectedArray();
115 	}
116 
117 	/** Returns the offset of the byte array which this buffer is based on, if there is one.
118 	 * <p>
119 	 * The offset is the index of the array which corresponds to the zero position of the buffer.
120 	 * </p>
121 	 *
122 	 * @return the offset of the byte array which this buffer is based on.
123 	 * @exception ReadOnlyBufferException if this buffer is based on a read-only array.
124 	 * @exception UnsupportedOperationException if this buffer is not based on an array.
125 	 * @since Android 1.0 */
arrayOffset()126 	public final int arrayOffset () {
127 		return protectedArrayOffset();
128 	}
129 
130 	/** Returns a char buffer which is based on the remaining content of this byte buffer.
131 	 * <p>
132 	 * The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by two, and its mark is
133 	 * not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this
134 	 * byte buffer is direct.
135 	 * </p>
136 	 * <p>
137 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
138 	 * other. The two buffer's position, limit and mark are independent.
139 	 * </p>
140 	 *
141 	 * @return a char buffer which is based on the content of this byte buffer.
142 	 * @since Android 1.0 */
asCharBuffer()143 	public abstract CharBuffer asCharBuffer ();
144 
145 	/** Returns a double buffer which is based on the remaining content of this byte buffer.
146 	 * <p>
147 	 * The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by eight, and its mark is
148 	 * not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this
149 	 * byte buffer is direct.
150 	 * </p>
151 	 * <p>
152 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
153 	 * other. The two buffer's position, limit and mark are independent.
154 	 * </p>
155 	 *
156 	 * @return a double buffer which is based on the content of this byte buffer.
157 	 * @since Android 1.0 */
asDoubleBuffer()158 	public abstract DoubleBuffer asDoubleBuffer ();
159 
160 	/** Returns a float buffer which is based on the remaining content of this byte buffer.
161 	 * <p>
162 	 * The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by four, and its mark is
163 	 * not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this
164 	 * byte buffer is direct.
165 	 * </p>
166 	 * <p>
167 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
168 	 * other. The two buffer's position, limit and mark are independent.
169 	 * </p>
170 	 *
171 	 * @return a float buffer which is based on the content of this byte buffer.
172 	 * @since Android 1.0 */
asFloatBuffer()173 	public abstract FloatBuffer asFloatBuffer ();
174 
175 	/** Returns a int buffer which is based on the remaining content of this byte buffer.
176 	 * <p>
177 	 * The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by four, and its mark is
178 	 * not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this
179 	 * byte buffer is direct.
180 	 * </p>
181 	 * <p>
182 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
183 	 * other. The two buffer's position, limit and mark are independent.
184 	 * </p>
185 	 *
186 	 * @return a int buffer which is based on the content of this byte buffer.
187 	 * @since Android 1.0 */
asIntBuffer()188 	public abstract IntBuffer asIntBuffer ();
189 
190 	/** Returns a long buffer which is based on the remaining content of this byte buffer.
191 	 * <p>
192 	 * The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by eight, and its mark is
193 	 * not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this
194 	 * byte buffer is direct.
195 	 * </p>
196 	 * <p>
197 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
198 	 * other. The two buffer's position, limit and mark are independent.
199 	 * </p>
200 	 *
201 	 * @return a long buffer which is based on the content of this byte buffer.
202 	 * @since Android 1.0 */
asLongBuffer()203 	public abstract LongBuffer asLongBuffer ();
204 
205 	/** Returns a read-only buffer that shares its content with this buffer.
206 	 * <p>
207 	 * The returned buffer is guaranteed to be a new instance, even if this buffer is read-only itself. The new buffer's position,
208 	 * limit, capacity and mark are the same as this buffer.
209 	 * </p>
210 	 * <p>
211 	 * The new buffer shares its content with this buffer, which means this buffer's change of content will be visible to the new
212 	 * buffer. The two buffer's position, limit and mark are independent.
213 	 * </p>
214 	 *
215 	 * @return a read-only version of this buffer.
216 	 * @since Android 1.0 */
asReadOnlyBuffer()217 	public abstract ByteBuffer asReadOnlyBuffer ();
218 
219 	/** Returns a short buffer which is based on the remaining content of this byte buffer.
220 	 * <p>
221 	 * The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by two, and its mark is
222 	 * not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this
223 	 * byte buffer is direct.
224 	 * </p>
225 	 * <p>
226 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
227 	 * other. The two buffer's position, limit and mark are independent.
228 	 * </p>
229 	 *
230 	 * @return a short buffer which is based on the content of this byte buffer.
231 	 * @since Android 1.0 */
asShortBuffer()232 	public abstract ShortBuffer asShortBuffer ();
233 
234 	/** Compacts this byte buffer.
235 	 * <p>
236 	 * The remaining bytes will be moved to the head of the buffer, starting from position zero. Then the position is set to
237 	 * {@code remaining()}; the limit is set to capacity; the mark is cleared.
238 	 * </p>
239 	 *
240 	 * @return this buffer.
241 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
242 	 * @since Android 1.0 */
compact()243 	public abstract ByteBuffer compact ();
244 
245 	/** Compares the remaining bytes of this buffer to another byte buffer's remaining bytes.
246 	 *
247 	 * @param otherBuffer another byte buffer.
248 	 * @return a negative value if this is less than {@code other}; 0 if this equals to {@code other}; a positive value if this is
249 	 *         greater than {@code other}.
250 	 * @exception ClassCastException if {@code other} is not a byte buffer.
251 	 * @since Android 1.0 */
compareTo(ByteBuffer otherBuffer)252 	public int compareTo (ByteBuffer otherBuffer) {
253 		int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining() : otherBuffer.remaining();
254 		int thisPos = position;
255 		int otherPos = otherBuffer.position;
256 		byte thisByte, otherByte;
257 		while (compareRemaining > 0) {
258 			thisByte = get(thisPos);
259 			otherByte = otherBuffer.get(otherPos);
260 			if (thisByte != otherByte) {
261 				return thisByte < otherByte ? -1 : 1;
262 			}
263 			thisPos++;
264 			otherPos++;
265 			compareRemaining--;
266 		}
267 		return remaining() - otherBuffer.remaining();
268 	}
269 
270 	/** Returns a duplicated buffer that shares its content with this buffer.
271 	 * <p>
272 	 * The duplicated buffer's position, limit, capacity and mark are the same as this buffer's. The duplicated buffer's read-only
273 	 * property and byte order are the same as this buffer's too.
274 	 * </p>
275 	 * <p>
276 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
277 	 * other. The two buffer's position, limit and mark are independent.
278 	 * </p>
279 	 *
280 	 * @return a duplicated buffer that shares its content with this buffer.
281 	 * @since Android 1.0 */
duplicate()282 	public abstract ByteBuffer duplicate ();
283 
284 	/** Checks whether this byte buffer is equal to another object.
285 	 * <p>
286 	 * If {@code other} is not a byte buffer then {@code false} is returned. Two byte buffers are equal if and only if their
287 	 * remaining bytes are exactly the same. Position, limit, capacity and mark are not considered.
288 	 * </p>
289 	 *
290 	 * @param other the object to compare with this byte buffer.
291 	 * @return {@code true} if this byte buffer is equal to {@code other}, {@code false} otherwise.
292 	 * @since Android 1.0 */
equals(Object other)293 	public boolean equals (Object other) {
294 		if (!(other instanceof ByteBuffer)) {
295 			return false;
296 		}
297 		ByteBuffer otherBuffer = (ByteBuffer)other;
298 
299 		if (remaining() != otherBuffer.remaining()) {
300 			return false;
301 		}
302 
303 		int myPosition = position;
304 		int otherPosition = otherBuffer.position;
305 		boolean equalSoFar = true;
306 		while (equalSoFar && (myPosition < limit)) {
307 			equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
308 		}
309 
310 		return equalSoFar;
311 	}
312 
313 	/** Returns the byte at the current position and increases the position by 1.
314 	 *
315 	 * @return the byte at the current position.
316 	 * @exception BufferUnderflowException if the position is equal or greater than limit.
317 	 * @since Android 1.0 */
get()318 	public abstract byte get ();
319 
320 	/** Reads bytes from the current position into the specified byte array and increases the position by the number of bytes read.
321 	 * <p>
322 	 * Calling this method has the same effect as {@code get(dest, 0, dest.length)}.
323 	 * </p>
324 	 *
325 	 * @param dest the destination byte array.
326 	 * @return this buffer.
327 	 * @exception BufferUnderflowException if {@code dest.length} is greater than {@code remaining()}.
328 	 * @since Android 1.0 */
get(byte[] dest)329 	public ByteBuffer get (byte[] dest) {
330 		return get(dest, 0, dest.length);
331 	}
332 
333 	/** Reads bytes from the current position into the specified byte array, starting at the specified offset, and increases the
334 	 * position by the number of bytes read.
335 	 *
336 	 * @param dest the target byte array.
337 	 * @param off the offset of the byte array, must not be negative and not greater than {@code dest.length}.
338 	 * @param len the number of bytes to read, must not be negative and not greater than {@code dest.length - off}
339 	 * @return this buffer.
340 	 * @exception IndexOutOfBoundsException if either {@code off} or {@code len} is invalid.
341 	 * @exception BufferUnderflowException if {@code len} is greater than {@code remaining()}.
342 	 * @since Android 1.0 */
get(byte[] dest, int off, int len)343 	public ByteBuffer get (byte[] dest, int off, int len) {
344 		int length = dest.length;
345 		if ((off < 0) || (len < 0) || ((long)off + (long)len > length)) {
346 			throw new IndexOutOfBoundsException();
347 		}
348 
349 		if (len > remaining()) {
350 			throw new BufferUnderflowException();
351 		}
352 		for (int i = off; i < off + len; i++) {
353 			dest[i] = get();
354 		}
355 		return this;
356 	}
357 
358 	/** Returns the byte at the specified index and does not change the position.
359 	 *
360 	 * @param index the index, must not be negative and less than limit.
361 	 * @return the byte at the specified index.
362 	 * @exception IndexOutOfBoundsException if index is invalid.
363 	 * @since Android 1.0 */
get(int index)364 	public abstract byte get (int index);
365 
366 	/** Returns the char at the current position and increases the position by 2.
367 	 * <p>
368 	 * The 2 bytes starting at the current position are composed into a char according to the current byte order and returned.
369 	 * </p>
370 	 *
371 	 * @return the char at the current position.
372 	 * @exception BufferUnderflowException if the position is greater than {@code limit - 2}.
373 	 * @since Android 1.0 */
getChar()374 	public abstract char getChar ();
375 
376 	/** Returns the char at the specified index.
377 	 * <p>
378 	 * The 2 bytes starting from the specified index are composed into a char according to the current byte order and returned. The
379 	 * position is not changed.
380 	 * </p>
381 	 *
382 	 * @param index the index, must not be negative and equal or less than {@code limit - 2}.
383 	 * @return the char at the specified index.
384 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
385 	 * @since Android 1.0 */
getChar(int index)386 	public abstract char getChar (int index);
387 
388 	/** Returns the double at the current position and increases the position by 8.
389 	 * <p>
390 	 * The 8 bytes starting from the current position are composed into a double according to the current byte order and returned.
391 	 * </p>
392 	 *
393 	 * @return the double at the current position.
394 	 * @exception BufferUnderflowException if the position is greater than {@code limit - 8}.
395 	 * @since Android 1.0 */
getDouble()396 	public abstract double getDouble ();
397 
398 	/** Returns the double at the specified index.
399 	 * <p>
400 	 * The 8 bytes starting at the specified index are composed into a double according to the current byte order and returned. The
401 	 * position is not changed.
402 	 * </p>
403 	 *
404 	 * @param index the index, must not be negative and equal or less than {@code limit - 8}.
405 	 * @return the double at the specified index.
406 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
407 	 * @since Android 1.0 */
getDouble(int index)408 	public abstract double getDouble (int index);
409 
410 	/** Returns the float at the current position and increases the position by 4.
411 	 * <p>
412 	 * The 4 bytes starting at the current position are composed into a float according to the current byte order and returned.
413 	 * </p>
414 	 *
415 	 * @return the float at the current position.
416 	 * @exception BufferUnderflowException if the position is greater than {@code limit - 4}.
417 	 * @since Android 1.0 */
getFloat()418 	public abstract float getFloat ();
419 
420 	/** Returns the float at the specified index.
421 	 * <p>
422 	 * The 4 bytes starting at the specified index are composed into a float according to the current byte order and returned. The
423 	 * position is not changed.
424 	 * </p>
425 	 *
426 	 * @param index the index, must not be negative and equal or less than {@code limit - 4}.
427 	 * @return the float at the specified index.
428 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
429 	 * @since Android 1.0 */
getFloat(int index)430 	public abstract float getFloat (int index);
431 
432 	/** Returns the int at the current position and increases the position by 4.
433 	 * <p>
434 	 * The 4 bytes starting at the current position are composed into a int according to the current byte order and returned.
435 	 * </p>
436 	 *
437 	 * @return the int at the current position.
438 	 * @exception BufferUnderflowException if the position is greater than {@code limit - 4}.
439 	 * @since Android 1.0 */
getInt()440 	public abstract int getInt ();
441 
442 	/** Returns the int at the specified index.
443 	 * <p>
444 	 * The 4 bytes starting at the specified index are composed into a int according to the current byte order and returned. The
445 	 * position is not changed.
446 	 * </p>
447 	 *
448 	 * @param index the index, must not be negative and equal or less than {@code limit - 4}.
449 	 * @return the int at the specified index.
450 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
451 	 * @since Android 1.0 */
getInt(int index)452 	public abstract int getInt (int index);
453 
454 	/** Returns the long at the current position and increases the position by 8.
455 	 * <p>
456 	 * The 8 bytes starting at the current position are composed into a long according to the current byte order and returned.
457 	 * </p>
458 	 *
459 	 * @return the long at the current position.
460 	 * @exception BufferUnderflowException if the position is greater than {@code limit - 8}.
461 	 * @since Android 1.0 */
getLong()462 	public abstract long getLong ();
463 
464 	/** Returns the long at the specified index.
465 	 * <p>
466 	 * The 8 bytes starting at the specified index are composed into a long according to the current byte order and returned. The
467 	 * position is not changed.
468 	 * </p>
469 	 *
470 	 * @param index the index, must not be negative and equal or less than {@code limit - 8}.
471 	 * @return the long at the specified index.
472 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
473 	 * @since Android 1.0 */
getLong(int index)474 	public abstract long getLong (int index);
475 
476 	/** Returns the short at the current position and increases the position by 2.
477 	 * <p>
478 	 * The 2 bytes starting at the current position are composed into a short according to the current byte order and returned.
479 	 * </p>
480 	 *
481 	 * @return the short at the current position.
482 	 * @exception BufferUnderflowException if the position is greater than {@code limit - 2}.
483 	 * @since Android 1.0 */
getShort()484 	public abstract short getShort ();
485 
486 	/** Returns the short at the specified index.
487 	 * <p>
488 	 * The 2 bytes starting at the specified index are composed into a short according to the current byte order and returned. The
489 	 * position is not changed.
490 	 * </p>
491 	 *
492 	 * @param index the index, must not be negative and equal or less than {@code limit - 2}.
493 	 * @return the short at the specified index.
494 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
495 	 * @since Android 1.0 */
getShort(int index)496 	public abstract short getShort (int index);
497 
498 	/** Indicates whether this buffer is based on a byte array and provides read/write access.
499 	 *
500 	 * @return {@code true} if this buffer is based on a byte array and provides read/write access, {@code false} otherwise.
501 	 * @since Android 1.0 */
hasArray()502 	public final boolean hasArray () {
503 		return protectedHasArray();
504 	}
505 
506 	/** Calculates this buffer's hash code from the remaining chars. The position, limit, capacity and mark don't affect the hash
507 	 * code.
508 	 *
509 	 * @return the hash code calculated from the remaining bytes.
510 	 * @since Android 1.0 */
hashCode()511 	public int hashCode () {
512 		int myPosition = position;
513 		int hash = 0;
514 		while (myPosition < limit) {
515 			hash = hash + get(myPosition++);
516 		}
517 		return hash;
518 	}
519 
520 	/** Indicates whether this buffer is direct.
521 	 *
522 	 * @return {@code true} if this buffer is direct, {@code false} otherwise.
523 	 * @since Android 1.0 */
isDirect()524 	public abstract boolean isDirect ();
525 
526 	/** Returns the byte order used by this buffer when converting bytes from/to other primitive types.
527 	 * <p>
528 	 * The default byte order of byte buffer is always {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
529 	 * </p>
530 	 *
531 	 * @return the byte order used by this buffer when converting bytes from/to other primitive types.
532 	 * @since Android 1.0 */
order()533 	public final ByteOrder order () {
534 		return order == Endianness.BIG_ENDIAN ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
535 	}
536 
537 	/** Sets the byte order of this buffer.
538 	 *
539 	 * @param byteOrder the byte order to set. If {@code null} then the order will be {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}
540 	 *           .
541 	 * @return this buffer.
542 	 * @see ByteOrder
543 	 * @since Android 1.0 */
order(ByteOrder byteOrder)544 	public final ByteBuffer order (ByteOrder byteOrder) {
545 		return orderImpl(byteOrder);
546 	}
547 
orderImpl(ByteOrder byteOrder)548 	ByteBuffer orderImpl (ByteOrder byteOrder) {
549 		order = byteOrder == ByteOrder.BIG_ENDIAN ? Endianness.BIG_ENDIAN : Endianness.LITTLE_ENDIAN;
550 		return this;
551 	}
552 
553 	/** Child class implements this method to realize {@code array()}.
554 	 *
555 	 * @see #array()
556 	 * @since Android 1.0 */
protectedArray()557 	abstract byte[] protectedArray ();
558 
559 	/** Child class implements this method to realize {@code arrayOffset()}.
560 	 *
561 	 * @see #arrayOffset()
562 	 * @since Android 1.0 */
protectedArrayOffset()563 	abstract int protectedArrayOffset ();
564 
565 	/** Child class implements this method to realize {@code hasArray()}.
566 	 *
567 	 * @see #hasArray()
568 	 * @since Android 1.0 */
protectedHasArray()569 	abstract boolean protectedHasArray ();
570 
571 	/** Writes the given byte to the current position and increases the position by 1.
572 	 *
573 	 * @param b the byte to write.
574 	 * @return this buffer.
575 	 * @exception BufferOverflowException if position is equal or greater than limit.
576 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
577 	 * @since Android 1.0 */
put(byte b)578 	public abstract ByteBuffer put (byte b);
579 
580 	/** Writes bytes in the given byte array to the current position and increases the position by the number of bytes written.
581 	 * <p>
582 	 * Calling this method has the same effect as {@code put(src, 0, src.length)}.
583 	 * </p>
584 	 *
585 	 * @param src the source byte array.
586 	 * @return this buffer.
587 	 * @exception BufferOverflowException if {@code remaining()} is less than {@code src.length}.
588 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
589 	 * @since Android 1.0 */
put(byte[] src)590 	public final ByteBuffer put (byte[] src) {
591 		return put(src, 0, src.length);
592 	}
593 
594 	/** Writes bytes in the given byte array, starting from the specified offset, to the current position and increases the position
595 	 * by the number of bytes written.
596 	 *
597 	 * @param src the source byte array.
598 	 * @param off the offset of byte array, must not be negative and not greater than {@code src.length}.
599 	 * @param len the number of bytes to write, must not be negative and not greater than {@code src.length - off}.
600 	 * @return this buffer.
601 	 * @exception BufferOverflowException if {@code remaining()} is less than {@code len}.
602 	 * @exception IndexOutOfBoundsException if either {@code off} or {@code len} is invalid.
603 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
604 	 * @since Android 1.0 */
put(byte[] src, int off, int len)605 	public ByteBuffer put (byte[] src, int off, int len) {
606 		int length = src.length;
607 		if ((off < 0) || (len < 0) || ((long)off + (long)len > length)) {
608 			throw new IndexOutOfBoundsException();
609 		}
610 
611 		if (len > remaining()) {
612 			throw new BufferOverflowException();
613 		}
614 		for (int i = off; i < off + len; i++) {
615 			put(src[i]);
616 		}
617 		return this;
618 	}
619 
620 	/** Writes all the remaining bytes of the {@code src} byte buffer to this buffer's current position, and increases both buffers'
621 	 * position by the number of bytes copied.
622 	 *
623 	 * @param src the source byte buffer.
624 	 * @return this buffer.
625 	 * @exception BufferOverflowException if {@code src.remaining()} is greater than this buffer's {@code remaining()}.
626 	 * @exception IllegalArgumentException if {@code src} is this buffer.
627 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
628 	 * @since Android 1.0 */
put(ByteBuffer src)629 	public ByteBuffer put (ByteBuffer src) {
630 		if (src == this) {
631 			throw new IllegalArgumentException();
632 		}
633 		if (src.remaining() > remaining()) {
634 			throw new BufferOverflowException();
635 		}
636 		byte[] contents = new byte[src.remaining()];
637 		src.get(contents);
638 		put(contents);
639 		return this;
640 	}
641 
642 	/** Write a byte to the specified index of this buffer without changing the position.
643 	 *
644 	 * @param index the index, must not be negative and less than the limit.
645 	 * @param b the byte to write.
646 	 * @return this buffer.
647 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
648 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
649 	 * @since Android 1.0 */
put(int index, byte b)650 	public abstract ByteBuffer put (int index, byte b);
651 
652 	/** Writes the given char to the current position and increases the position by 2.
653 	 * <p>
654 	 * The char is converted to bytes using the current byte order.
655 	 * </p>
656 	 *
657 	 * @param value the char to write.
658 	 * @return this buffer.
659 	 * @exception BufferOverflowException if position is greater than {@code limit - 2}.
660 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
661 	 * @since Android 1.0 */
putChar(char value)662 	public abstract ByteBuffer putChar (char value);
663 
664 	/** Writes the given char to the specified index of this buffer.
665 	 * <p>
666 	 * The char is converted to bytes using the current byte order. The position is not changed.
667 	 * </p>
668 	 *
669 	 * @param index the index, must not be negative and equal or less than {@code limit - 2}.
670 	 * @param value the char to write.
671 	 * @return this buffer.
672 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
673 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
674 	 * @since Android 1.0 */
putChar(int index, char value)675 	public abstract ByteBuffer putChar (int index, char value);
676 
677 	/** Writes the given double to the current position and increases the position by 8.
678 	 * <p>
679 	 * The double is converted to bytes using the current byte order.
680 	 * </p>
681 	 *
682 	 * @param value the double to write.
683 	 * @return this buffer.
684 	 * @exception BufferOverflowException if position is greater than {@code limit - 8}.
685 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
686 	 * @since Android 1.0 */
putDouble(double value)687 	public abstract ByteBuffer putDouble (double value);
688 
689 	/** Writes the given double to the specified index of this buffer.
690 	 * <p>
691 	 * The double is converted to bytes using the current byte order. The position is not changed.
692 	 * </p>
693 	 *
694 	 * @param index the index, must not be negative and equal or less than {@code limit - 8}.
695 	 * @param value the double to write.
696 	 * @return this buffer.
697 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
698 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
699 	 * @since Android 1.0 */
putDouble(int index, double value)700 	public abstract ByteBuffer putDouble (int index, double value);
701 
702 	/** Writes the given float to the current position and increases the position by 4.
703 	 * <p>
704 	 * The float is converted to bytes using the current byte order.
705 	 * </p>
706 	 *
707 	 * @param value the float to write.
708 	 * @return this buffer.
709 	 * @exception BufferOverflowException if position is greater than {@code limit - 4}.
710 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
711 	 * @since Android 1.0 */
putFloat(float value)712 	public abstract ByteBuffer putFloat (float value);
713 
714 	/** Writes the given float to the specified index of this buffer.
715 	 * <p>
716 	 * The float is converted to bytes using the current byte order. The position is not changed.
717 	 * </p>
718 	 *
719 	 * @param index the index, must not be negative and equal or less than {@code limit - 4}.
720 	 * @param value the float to write.
721 	 * @return this buffer.
722 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
723 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
724 	 * @since Android 1.0 */
putFloat(int index, float value)725 	public abstract ByteBuffer putFloat (int index, float value);
726 
727 	/** Writes the given int to the current position and increases the position by 4.
728 	 * <p>
729 	 * The int is converted to bytes using the current byte order.
730 	 * </p>
731 	 *
732 	 * @param value the int to write.
733 	 * @return this buffer.
734 	 * @exception BufferOverflowException if position is greater than {@code limit - 4}.
735 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
736 	 * @since Android 1.0 */
putInt(int value)737 	public abstract ByteBuffer putInt (int value);
738 
739 	/** Writes the given int to the specified index of this buffer.
740 	 * <p>
741 	 * The int is converted to bytes using the current byte order. The position is not changed.
742 	 * </p>
743 	 *
744 	 * @param index the index, must not be negative and equal or less than {@code limit - 4}.
745 	 * @param value the int to write.
746 	 * @return this buffer.
747 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
748 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
749 	 * @since Android 1.0 */
putInt(int index, int value)750 	public abstract ByteBuffer putInt (int index, int value);
751 
752 	/** Writes the given long to the current position and increases the position by 8.
753 	 * <p>
754 	 * The long is converted to bytes using the current byte order.
755 	 * </p>
756 	 *
757 	 * @param value the long to write.
758 	 * @return this buffer.
759 	 * @exception BufferOverflowException if position is greater than {@code limit - 8}.
760 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
761 	 * @since Android 1.0 */
putLong(long value)762 	public abstract ByteBuffer putLong (long value);
763 
764 	/** Writes the given long to the specified index of this buffer.
765 	 * <p>
766 	 * The long is converted to bytes using the current byte order. The position is not changed.
767 	 * </p>
768 	 *
769 	 * @param index the index, must not be negative and equal or less than {@code limit - 8}.
770 	 * @param value the long to write.
771 	 * @return this buffer.
772 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
773 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
774 	 * @since Android 1.0 */
putLong(int index, long value)775 	public abstract ByteBuffer putLong (int index, long value);
776 
777 	/** Writes the given short to the current position and increases the position by 2.
778 	 * <p>
779 	 * The short is converted to bytes using the current byte order.
780 	 * </p>
781 	 *
782 	 * @param value the short to write.
783 	 * @return this buffer.
784 	 * @exception BufferOverflowException if position is greater than {@code limit - 2}.
785 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
786 	 * @since Android 1.0 */
putShort(short value)787 	public abstract ByteBuffer putShort (short value);
788 
789 	/** Writes the given short to the specified index of this buffer.
790 	 * <p>
791 	 * The short is converted to bytes using the current byte order. The position is not changed.
792 	 * </p>
793 	 *
794 	 * @param index the index, must not be negative and equal or less than {@code limit - 2}.
795 	 * @param value the short to write.
796 	 * @return this buffer.
797 	 * @exception IndexOutOfBoundsException if {@code index} is invalid.
798 	 * @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
799 	 * @since Android 1.0 */
putShort(int index, short value)800 	public abstract ByteBuffer putShort (int index, short value);
801 
802 	/** Returns a sliced buffer that shares its content with this buffer.
803 	 * <p>
804 	 * The sliced buffer's capacity will be this buffer's {@code remaining()}, and it's zero position will correspond to this
805 	 * buffer's current position. The new buffer's position will be 0, limit will be its capacity, and its mark is cleared. The new
806 	 * buffer's read-only property and byte order are the same as this buffer's.
807 	 * </p>
808 	 * <p>
809 	 * The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the
810 	 * other. The two buffer's position, limit and mark are independent.
811 	 * </p>
812 	 *
813 	 * @return a sliced buffer that shares its content with this buffer.
814 	 * @since Android 1.0 */
slice()815 	public abstract ByteBuffer slice ();
816 
817 	/** Returns a string representing the state of this byte buffer.
818 	 *
819 	 * @return a string representing the state of this byte buffer.
820 	 * @since Android 1.0 */
toString()821 	public String toString () {
822 		StringBuffer buf = new StringBuffer();
823 		buf.append(getClass().getName());
824 		buf.append(", status: capacity="); //$NON-NLS-1$
825 		buf.append(capacity());
826 		buf.append(" position="); //$NON-NLS-1$
827 		buf.append(position());
828 		buf.append(" limit="); //$NON-NLS-1$
829 		buf.append(limit());
830 		return buf.toString();
831 	}
832 
stringToByteBuffer(String s)833 	public ByteBuffer stringToByteBuffer (String s) {
834 		return new StringByteBuffer(s);
835 	}
836 }
837