ByteBuffer.java revision 7e02765b7ada4e85f941f0a6b0fc60a8a76301f3
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
18package java.nio;
19
20import java.util.Arrays;
21import libcore.io.Memory;
22
23/**
24 * A buffer for bytes.
25 * <p>
26 * A byte buffer can be created in either one of the following ways:
27 * <ul>
28 * <li>{@link #allocate(int) Allocate} a new byte array and create a buffer
29 * based on it;</li>
30 * <li>{@link #allocateDirect(int) Allocate} a memory block and create a direct
31 * buffer based on it;</li>
32 * <li>{@link #wrap(byte[]) Wrap} an existing byte array to create a new
33 * buffer.</li>
34 * </ul>
35 *
36 */
37public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer> {
38    /**
39     * The byte order of this buffer, default is {@code BIG_ENDIAN}.
40     */
41    ByteOrder order = ByteOrder.BIG_ENDIAN;
42
43    /**
44     * Creates a byte buffer based on a newly allocated byte array.
45     *
46     * @param capacity
47     *            the capacity of the new buffer
48     * @return the created byte buffer.
49     * @throws IllegalArgumentException
50     *             if {@code capacity < 0}.
51     */
52    public static ByteBuffer allocate(int capacity) {
53        if (capacity < 0) {
54            throw new IllegalArgumentException("capacity < 0: " + capacity);
55        }
56        return new ByteArrayBuffer(new byte[capacity]);
57    }
58
59    /**
60     * Creates a direct byte buffer based on a newly allocated memory block.
61     *
62     * @param capacity
63     *            the capacity of the new buffer
64     * @return the created byte buffer.
65     * @throws IllegalArgumentException
66     *             if {@code capacity < 0}.
67     */
68    public static ByteBuffer allocateDirect(int capacity) {
69        if (capacity < 0) {
70            throw new IllegalArgumentException("capacity < 0: " + capacity);
71        }
72        return new DirectByteBuffer(MemoryBlock.allocate(capacity), capacity, 0, false, null);
73    }
74
75    /**
76     * Creates a new byte buffer by wrapping the given byte array.
77     * <p>
78     * Calling this method has the same effect as
79     * {@code wrap(array, 0, array.length)}.
80     *
81     * @param array
82     *            the byte array which the new buffer will be based on
83     * @return the created byte buffer.
84     */
85    public static ByteBuffer wrap(byte[] array) {
86        return new ByteArrayBuffer(array);
87    }
88
89    /**
90     * Creates a new byte buffer by wrapping the given byte array.
91     * <p>
92     * The new buffer's position will be {@code start}, limit will be
93     * {@code start + byteCount}, capacity will be the length of the array.
94     *
95     * @param array
96     *            the byte array which the new buffer will be based on.
97     * @param start
98     *            the start index, must not be negative and not greater than
99     *            {@code array.length}.
100     * @param byteCount
101     *            the length, must not be negative and not greater than
102     *            {@code array.length - start}.
103     * @return the created byte buffer.
104     * @throws IndexOutOfBoundsException
105     *                if either {@code start} or {@code byteCount} is invalid.
106     */
107    public static ByteBuffer wrap(byte[] array, int start, int byteCount) {
108        Arrays.checkOffsetAndCount(array.length, start, byteCount);
109        ByteBuffer buf = new ByteArrayBuffer(array);
110        buf.position = start;
111        buf.limit = start + byteCount;
112        return buf;
113    }
114
115    ByteBuffer(int capacity, long effectiveDirectAddress) {
116        super(0, capacity, effectiveDirectAddress);
117    }
118
119    /**
120     * Returns the byte array which this buffer is based on, if there is one.
121     *
122     * @return the byte array which this buffer is based on.
123     * @throws ReadOnlyBufferException
124     *                if this buffer is based on a read-only array.
125     * @throws UnsupportedOperationException
126     *                if this buffer is not based on an array.
127     */
128    @Override public final byte[] array() {
129        return protectedArray();
130    }
131
132    /**
133     * Returns the offset of the byte array which this buffer is based on, if
134     * there is one.
135     * <p>
136     * The offset is the index of the array which corresponds to the zero
137     * position of the buffer.
138     *
139     * @return the offset of the byte array which this buffer is based on.
140     * @throws ReadOnlyBufferException
141     *                if this buffer is based on a read-only array.
142     * @throws UnsupportedOperationException
143     *                if this buffer is not based on an array.
144     */
145    @Override public final int arrayOffset() {
146        return protectedArrayOffset();
147    }
148
149    /**
150     * Returns a char buffer which is based on the remaining content of this
151     * byte buffer.
152     * <p>
153     * The new buffer's position is zero, its limit and capacity is the number
154     * of remaining bytes divided by two, and its mark is not set. The new
155     * buffer's read-only property and byte order are the same as this buffer's.
156     * The new buffer is direct if this byte buffer is direct.
157     * <p>
158     * The new buffer shares its content with this buffer, which means either
159     * buffer's change of content will be visible to the other. The two buffers'
160     * position, limit and mark are independent.
161     */
162    public abstract CharBuffer asCharBuffer();
163
164    /**
165     * Returns a double buffer which is based on the remaining content of this
166     * byte buffer.
167     * <p>
168     * The new buffer's position is zero, its limit and capacity is the number
169     * of remaining bytes divided by eight, and its mark is not set. The new
170     * buffer's read-only property and byte order are the same as this buffer's.
171     * The new buffer is direct if this byte buffer is direct.
172     * <p>
173     * The new buffer shares its content with this buffer, which means either
174     * buffer's change of content will be visible to the other. The two buffers'
175     * position, limit and mark are independent.
176     */
177    public abstract DoubleBuffer asDoubleBuffer();
178
179    /**
180     * Returns a float buffer which is based on the remaining content of this
181     * byte buffer.
182     * <p>
183     * The new buffer's position is zero, its limit and capacity is the number
184     * of remaining bytes divided by four, and its mark is not set. The new
185     * buffer's read-only property and byte order are the same as this buffer's.
186     * The new buffer is direct if this byte buffer is direct.
187     * <p>
188     * The new buffer shares its content with this buffer, which means either
189     * buffer's change of content will be visible to the other. The two buffers'
190     * position, limit and mark are independent.
191     */
192    public abstract FloatBuffer asFloatBuffer();
193
194    /**
195     * Returns a int buffer which is based on the remaining content of this byte
196     * buffer.
197     * <p>
198     * The new buffer's position is zero, its limit and capacity is the number
199     * of remaining bytes divided by four, and its mark is not set. The new
200     * buffer's read-only property and byte order are the same as this buffer's.
201     * The new buffer is direct if this byte buffer is direct.
202     * <p>
203     * The new buffer shares its content with this buffer, which means either
204     * buffer's change of content will be visible to the other. The two buffers'
205     * position, limit and mark are independent.
206     */
207    public abstract IntBuffer asIntBuffer();
208
209    /**
210     * Returns a long buffer which is based on the remaining content of this
211     * byte buffer.
212     * <p>
213     * The new buffer's position is zero, its limit and capacity is the number
214     * of remaining bytes divided by eight, and its mark is not set. The new
215     * buffer's read-only property and byte order are the same as this buffer's.
216     * The new buffer is direct if this byte buffer is direct.
217     * <p>
218     * The new buffer shares its content with this buffer, which means either
219     * buffer's change of content will be visible to the other. The two buffers'
220     * position, limit and mark are independent.
221     */
222    public abstract LongBuffer asLongBuffer();
223
224    /**
225     * Returns a read-only buffer that shares its content with this buffer.
226     * <p>
227     * The returned buffer is guaranteed to be a new instance, even if this
228     * buffer is read-only itself. The new buffer's position, limit, capacity
229     * and mark are the same as this buffer.
230     * <p>
231     * The new buffer shares its content with this buffer, which means this
232     * buffer's change of content will be visible to the new buffer. The two
233     * buffer's position, limit and mark are independent.
234     *
235     * @return a read-only version of this buffer.
236     */
237    public abstract ByteBuffer asReadOnlyBuffer();
238
239    /**
240     * Returns a short buffer which is based on the remaining content of this
241     * byte buffer.
242     * <p>
243     * The new buffer's position is zero, its limit and capacity is the number
244     * of remaining bytes divided by two, and its mark is not set. The new
245     * buffer's read-only property and byte order are the same as this buffer's.
246     * The new buffer is direct if this byte buffer is direct.
247     * <p>
248     * The new buffer shares its content with this buffer, which means either
249     * buffer's change of content will be visible to the other. The two buffers'
250     * position, limit and mark are independent.
251     */
252    public abstract ShortBuffer asShortBuffer();
253
254    /**
255     * Compacts this byte buffer.
256     * <p>
257     * The remaining bytes will be moved to the head of the
258     * buffer, starting from position zero. Then the position is set to
259     * {@code remaining()}; the limit is set to capacity; the mark is
260     * cleared.
261     *
262     * @return {@code this}
263     * @throws ReadOnlyBufferException
264     *                if no changes may be made to the contents of this buffer.
265     */
266    public abstract ByteBuffer compact();
267
268    /**
269     * Compares the remaining bytes of this buffer to another byte buffer's
270     * remaining bytes.
271     *
272     * @param otherBuffer
273     *            another byte buffer.
274     * @return a negative value if this is less than {@code other}; 0 if this
275     *         equals to {@code other}; a positive value if this is greater
276     *         than {@code other}.
277     * @throws ClassCastException
278     *                if {@code other} is not a byte buffer.
279     */
280    @Override public int compareTo(ByteBuffer otherBuffer) {
281        int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
282                : otherBuffer.remaining();
283        int thisPos = position;
284        int otherPos = otherBuffer.position;
285        byte thisByte, otherByte;
286        while (compareRemaining > 0) {
287            thisByte = get(thisPos);
288            otherByte = otherBuffer.get(otherPos);
289            if (thisByte != otherByte) {
290                return thisByte < otherByte ? -1 : 1;
291            }
292            thisPos++;
293            otherPos++;
294            compareRemaining--;
295        }
296        return remaining() - otherBuffer.remaining();
297    }
298
299    /**
300     * Returns a duplicated buffer that shares its content with this buffer.
301     * <p>
302     * The duplicated buffer's position, limit, capacity and mark are the same
303     * as this buffer's. The duplicated buffer's read-only property is the same
304     * as this buffer's.
305     *
306     * <p>Note that <i>in contrast to all non-{@code byte} buffers</i>,
307     * byte order is not preserved in the duplicate, and is instead set to
308     * big-endian.
309     *
310     * <p>The new buffer shares its content with this buffer, which means either
311     * buffer's change of content will be visible to the other. The two buffers'
312     * position, limit and mark are independent.
313     */
314    public abstract ByteBuffer duplicate();
315
316    /**
317     * Checks whether this byte buffer is equal to another object.
318     * <p>
319     * If {@code other} is not a byte buffer then {@code false} is returned. Two
320     * byte buffers are equal if and only if their remaining bytes are exactly
321     * the same. Position, limit, capacity and mark are not considered.
322     *
323     * @param other
324     *            the object to compare with this byte buffer.
325     * @return {@code true} if this byte buffer is equal to {@code other},
326     *         {@code false} otherwise.
327     */
328    @Override
329    public boolean equals(Object other) {
330        if (!(other instanceof ByteBuffer)) {
331            return false;
332        }
333        ByteBuffer otherBuffer = (ByteBuffer) other;
334
335        if (remaining() != otherBuffer.remaining()) {
336            return false;
337        }
338
339        int myPosition = position;
340        int otherPosition = otherBuffer.position;
341        boolean equalSoFar = true;
342        while (equalSoFar && (myPosition < limit)) {
343            equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
344        }
345
346        return equalSoFar;
347    }
348
349    /**
350     * Returns the byte at the current position and increases the position by 1.
351     *
352     * @return the byte at the current position.
353     * @throws BufferUnderflowException
354     *                if the position is equal or greater than limit.
355     */
356    public abstract byte get();
357
358    /**
359     * Reads bytes from the current position into the specified byte array and
360     * increases the position by the number of bytes read.
361     * <p>
362     * Calling this method has the same effect as
363     * {@code get(dst, 0, dst.length)}.
364     *
365     * @param dst
366     *            the destination byte array.
367     * @return {@code this}
368     * @throws BufferUnderflowException
369     *                if {@code dst.length} is greater than {@code remaining()}.
370     */
371    public ByteBuffer get(byte[] dst) {
372        return get(dst, 0, dst.length);
373    }
374
375    /**
376     * Reads bytes from the current position into the specified byte array,
377     * starting at the specified offset, and increases the position by the
378     * number of bytes read.
379     *
380     * @param dst
381     *            the target byte array.
382     * @param dstOffset
383     *            the offset of the byte array, must not be negative and
384     *            not greater than {@code dst.length}.
385     * @param byteCount
386     *            the number of bytes to read, must not be negative and not
387     *            greater than {@code dst.length - dstOffset}
388     * @return {@code this}
389     * @throws IndexOutOfBoundsException if {@code dstOffset < 0 ||  byteCount < 0}
390     * @throws BufferUnderflowException if {@code byteCount > remaining()}
391     */
392    public ByteBuffer get(byte[] dst, int dstOffset, int byteCount) {
393        Arrays.checkOffsetAndCount(dst.length, dstOffset, byteCount);
394        if (byteCount > remaining()) {
395            throw new BufferUnderflowException();
396        }
397        for (int i = dstOffset; i < dstOffset + byteCount; ++i) {
398            dst[i] = get();
399        }
400        return this;
401    }
402
403    /**
404     * Returns the byte at the specified index and does not change the position.
405     *
406     * @param index
407     *            the index, must not be negative and less than limit.
408     * @return the byte at the specified index.
409     * @throws IndexOutOfBoundsException
410     *                if index is invalid.
411     */
412    public abstract byte get(int index);
413
414    /**
415     * Returns the char at the current position and increases the position by 2.
416     * <p>
417     * The 2 bytes starting at the current position are composed into a char
418     * according to the current byte order and returned.
419     *
420     * @return the char at the current position.
421     * @throws BufferUnderflowException
422     *                if the position is greater than {@code limit - 2}.
423     */
424    public abstract char getChar();
425
426    /**
427     * Returns the char at the specified index.
428     * <p>
429     * The 2 bytes starting from the specified index are composed into a char
430     * according to the current byte order and returned. The position is not
431     * changed.
432     *
433     * @param index
434     *            the index, must not be negative and equal or less than
435     *            {@code limit - 2}.
436     * @return the char at the specified index.
437     * @throws IndexOutOfBoundsException
438     *                if {@code index} is invalid.
439     */
440    public abstract char getChar(int index);
441
442    /**
443     * Returns the double at the current position and increases the position by
444     * 8.
445     * <p>
446     * The 8 bytes starting from the current position are composed into a double
447     * according to the current byte order and returned.
448     *
449     * @return the double at the current position.
450     * @throws BufferUnderflowException
451     *                if the position is greater than {@code limit - 8}.
452     */
453    public abstract double getDouble();
454
455    /**
456     * Returns the double at the specified index.
457     * <p>
458     * The 8 bytes starting at the specified index are composed into a double
459     * according to the current byte order and returned. The position is not
460     * changed.
461     *
462     * @param index
463     *            the index, must not be negative and equal or less than
464     *            {@code limit - 8}.
465     * @return the double at the specified index.
466     * @throws IndexOutOfBoundsException
467     *                if {@code index} is invalid.
468     */
469    public abstract double getDouble(int index);
470
471    /**
472     * Returns the float at the current position and increases the position by
473     * 4.
474     * <p>
475     * The 4 bytes starting at the current position are composed into a float
476     * according to the current byte order and returned.
477     *
478     * @return the float at the current position.
479     * @throws BufferUnderflowException
480     *                if the position is greater than {@code limit - 4}.
481     */
482    public abstract float getFloat();
483
484    /**
485     * Returns the float at the specified index.
486     * <p>
487     * The 4 bytes starting at the specified index are composed into a float
488     * according to the current byte order and returned. The position is not
489     * changed.
490     *
491     * @param index
492     *            the index, must not be negative and equal or less than
493     *            {@code limit - 4}.
494     * @return the float at the specified index.
495     * @throws IndexOutOfBoundsException
496     *                if {@code index} is invalid.
497     */
498    public abstract float getFloat(int index);
499
500    /**
501     * Returns the int at the current position and increases the position by 4.
502     * <p>
503     * The 4 bytes starting at the current position are composed into a int
504     * according to the current byte order and returned.
505     *
506     * @return the int at the current position.
507     * @throws BufferUnderflowException
508     *                if the position is greater than {@code limit - 4}.
509     */
510    public abstract int getInt();
511
512    /**
513     * Returns the int at the specified index.
514     * <p>
515     * The 4 bytes starting at the specified index are composed into a int
516     * according to the current byte order and returned. The position is not
517     * changed.
518     *
519     * @param index
520     *            the index, must not be negative and equal or less than
521     *            {@code limit - 4}.
522     * @return the int at the specified index.
523     * @throws IndexOutOfBoundsException
524     *                if {@code index} is invalid.
525     */
526    public abstract int getInt(int index);
527
528    /**
529     * Returns the long at the current position and increases the position by 8.
530     * <p>
531     * The 8 bytes starting at the current position are composed into a long
532     * according to the current byte order and returned.
533     *
534     * @return the long at the current position.
535     * @throws BufferUnderflowException
536     *                if the position is greater than {@code limit - 8}.
537     */
538    public abstract long getLong();
539
540    /**
541     * Returns the long at the specified index.
542     * <p>
543     * The 8 bytes starting at the specified index are composed into a long
544     * according to the current byte order and returned. The position is not
545     * changed.
546     *
547     * @param index
548     *            the index, must not be negative and equal or less than
549     *            {@code limit - 8}.
550     * @return the long at the specified index.
551     * @throws IndexOutOfBoundsException
552     *                if {@code index} is invalid.
553     */
554    public abstract long getLong(int index);
555
556    /**
557     * Returns the short at the current position and increases the position by 2.
558     * <p>
559     * The 2 bytes starting at the current position are composed into a short
560     * according to the current byte order and returned.
561     *
562     * @return the short at the current position.
563     * @throws BufferUnderflowException
564     *                if the position is greater than {@code limit - 2}.
565     */
566    public abstract short getShort();
567
568    /**
569     * Returns the short at the specified index.
570     * <p>
571     * The 2 bytes starting at the specified index are composed into a short
572     * according to the current byte order and returned. The position is not
573     * changed.
574     *
575     * @param index
576     *            the index, must not be negative and equal or less than
577     *            {@code limit - 2}.
578     * @return the short at the specified index.
579     * @throws IndexOutOfBoundsException
580     *                if {@code index} is invalid.
581     */
582    public abstract short getShort(int index);
583
584    @Override public final boolean hasArray() {
585        return protectedHasArray();
586    }
587
588    /**
589     * Calculates this buffer's hash code from the remaining chars. The
590     * position, limit, capacity and mark don't affect the hash code.
591     *
592     * @return the hash code calculated from the remaining bytes.
593     */
594    @Override
595    public int hashCode() {
596        int myPosition = position;
597        int hash = 0;
598        while (myPosition < limit) {
599            hash = hash + get(myPosition++);
600        }
601        return hash;
602    }
603
604    /**
605     * Indicates whether this buffer is direct.
606     *
607     * @return {@code true} if this buffer is direct, {@code false} otherwise.
608     */
609    @Override public abstract boolean isDirect();
610
611    /**
612     * Indicates whether this buffer is still accessible.
613     *
614     * @return {@code true} if this buffer is accessible, {@code false} if the
615     *         buffer was made inaccessible (e.g. freed) and should not be used.
616     * @hide
617     */
618    public boolean isAccessible() {
619        return true;
620    }
621
622    /**
623     * Returns the byte order used by this buffer when converting bytes from/to
624     * other primitive types.
625     * <p>
626     * The default byte order of byte buffer is always
627     * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
628     *
629     * @return the byte order used by this buffer when converting bytes from/to
630     *         other primitive types.
631     */
632    public final ByteOrder order() {
633        return order;
634    }
635
636    /**
637     * Sets the byte order of this buffer.
638     *
639     * @param byteOrder
640     *            the byte order to set. If {@code null} then the order
641     *            will be {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}.
642     * @return {@code this}
643     * @see ByteOrder
644     */
645    public final ByteBuffer order(ByteOrder byteOrder) {
646        if (byteOrder == null) {
647            byteOrder = ByteOrder.LITTLE_ENDIAN;
648        }
649        order = byteOrder;
650        return this;
651    }
652
653    /**
654     * Child class implements this method to realize {@code array()}.
655     *
656     * @see #array()
657     */
658    abstract byte[] protectedArray();
659
660    /**
661     * Child class implements this method to realize {@code arrayOffset()}.
662     *
663     * @see #arrayOffset()
664     */
665    abstract int protectedArrayOffset();
666
667    /**
668     * Child class implements this method to realize {@code hasArray()}.
669     *
670     * @see #hasArray()
671     */
672    abstract boolean protectedHasArray();
673
674    /**
675     * Writes the given byte to the current position and increases the position
676     * by 1.
677     *
678     * @param b
679     *            the byte to write.
680     * @return {@code this}
681     * @throws BufferOverflowException
682     *                if position is equal or greater than limit.
683     * @throws ReadOnlyBufferException
684     *                if no changes may be made to the contents of this buffer.
685     */
686    public abstract ByteBuffer put(byte b);
687
688    /**
689     * Writes bytes in the given byte array to the current position and
690     * increases the position by the number of bytes written.
691     * <p>
692     * Calling this method has the same effect as
693     * {@code put(src, 0, src.length)}.
694     *
695     * @param src
696     *            the source byte array.
697     * @return {@code this}
698     * @throws BufferOverflowException
699     *                if {@code remaining()} is less than {@code src.length}.
700     * @throws ReadOnlyBufferException
701     *                if no changes may be made to the contents of this buffer.
702     */
703    public final ByteBuffer put(byte[] src) {
704        return put(src, 0, src.length);
705    }
706
707    /**
708     * Writes bytes in the given byte array, starting from the specified offset,
709     * to the current position and increases the position by the number of bytes
710     * written.
711     *
712     * @param src
713     *            the source byte array.
714     * @param srcOffset
715     *            the offset of byte array, must not be negative and not greater
716     *            than {@code src.length}.
717     * @param byteCount
718     *            the number of bytes to write, must not be negative and not
719     *            greater than {@code src.length - srcOffset}.
720     * @return {@code this}
721     * @throws BufferOverflowException
722     *                if {@code remaining()} is less than {@code byteCount}.
723     * @throws IndexOutOfBoundsException
724     *                if either {@code srcOffset} or {@code byteCount} is invalid.
725     * @throws ReadOnlyBufferException
726     *                if no changes may be made to the contents of this buffer.
727     */
728    public ByteBuffer put(byte[] src, int srcOffset, int byteCount) {
729        Arrays.checkOffsetAndCount(src.length, srcOffset, byteCount);
730        if (byteCount > remaining()) {
731            throw new BufferOverflowException();
732        }
733        for (int i = srcOffset; i < srcOffset + byteCount; ++i) {
734            put(src[i]);
735        }
736        return this;
737    }
738
739    /**
740     * Writes all the remaining bytes of the {@code src} byte buffer to this
741     * buffer's current position, and increases both buffers' position by the
742     * number of bytes copied.
743     *
744     * @param src
745     *            the source byte buffer.
746     * @return {@code this}
747     * @throws BufferOverflowException
748     *                if {@code src.remaining()} is greater than this buffer's
749     *                {@code remaining()}.
750     * @throws IllegalArgumentException
751     *                if {@code src} is this buffer.
752     * @throws ReadOnlyBufferException
753     *                if no changes may be made to the contents of this buffer.
754     */
755    public ByteBuffer put(ByteBuffer src) {
756        if (!src.isAccessible() || !isAccessible()) {
757            throw new IllegalStateException("buffer is inaccessible");
758        }
759
760        if (isReadOnly()) {
761            throw new ReadOnlyBufferException();
762        }
763        if (src == this) {
764            throw new IllegalArgumentException("src == this");
765        }
766        int srcByteCount = src.remaining();
767        if (srcByteCount > remaining()) {
768            throw new BufferOverflowException();
769        }
770
771        Object srcObject = src.isDirect() ? src : NioUtils.unsafeArray(src);
772        int srcOffset = src.position();
773        if (!src.isDirect()) {
774            srcOffset += NioUtils.unsafeArrayOffset(src);
775        }
776
777        ByteBuffer dst = this;
778        Object dstObject = dst.isDirect() ? dst : NioUtils.unsafeArray(dst);
779        int dstOffset = dst.position();
780        if (!dst.isDirect()) {
781            dstOffset += NioUtils.unsafeArrayOffset(dst);
782        }
783
784        Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, srcByteCount);
785        src.position(src.limit());
786        dst.position(dst.position() + srcByteCount);
787
788        return this;
789    }
790
791    /**
792     * Write a byte to the specified index of this buffer without changing the
793     * position.
794     *
795     * @param index
796     *            the index, must not be negative and less than the limit.
797     * @param b
798     *            the byte to write.
799     * @return {@code this}
800     * @throws IndexOutOfBoundsException
801     *                if {@code index} is invalid.
802     * @throws ReadOnlyBufferException
803     *                if no changes may be made to the contents of this buffer.
804     */
805    public abstract ByteBuffer put(int index, byte b);
806
807    /**
808     * Writes the given char to the current position and increases the position
809     * by 2.
810     * <p>
811     * The char is converted to bytes using the current byte order.
812     *
813     * @param value
814     *            the char to write.
815     * @return {@code this}
816     * @throws BufferOverflowException
817     *                if position is greater than {@code limit - 2}.
818     * @throws ReadOnlyBufferException
819     *                if no changes may be made to the contents of this buffer.
820     */
821    public abstract ByteBuffer putChar(char value);
822
823    /**
824     * Writes the given char to the specified index of this buffer.
825     * <p>
826     * The char is converted to bytes using the current byte order. The position
827     * is not changed.
828     *
829     * @param index
830     *            the index, must not be negative and equal or less than
831     *            {@code limit - 2}.
832     * @param value
833     *            the char to write.
834     * @return {@code this}
835     * @throws IndexOutOfBoundsException
836     *                if {@code index} is invalid.
837     * @throws ReadOnlyBufferException
838     *                if no changes may be made to the contents of this buffer.
839     */
840    public abstract ByteBuffer putChar(int index, char value);
841
842    /**
843     * Writes the given double to the current position and increases the position
844     * by 8.
845     * <p>
846     * The double is converted to bytes using the current byte order.
847     *
848     * @param value
849     *            the double to write.
850     * @return {@code this}
851     * @throws BufferOverflowException
852     *                if position is greater than {@code limit - 8}.
853     * @throws ReadOnlyBufferException
854     *                if no changes may be made to the contents of this buffer.
855     */
856    public abstract ByteBuffer putDouble(double value);
857
858    /**
859     * Writes the given double to the specified index of this buffer.
860     * <p>
861     * The double is converted to bytes using the current byte order. The
862     * position is not changed.
863     *
864     * @param index
865     *            the index, must not be negative and equal or less than
866     *            {@code limit - 8}.
867     * @param value
868     *            the double to write.
869     * @return {@code this}
870     * @throws IndexOutOfBoundsException
871     *                if {@code index} is invalid.
872     * @throws ReadOnlyBufferException
873     *                if no changes may be made to the contents of this buffer.
874     */
875    public abstract ByteBuffer putDouble(int index, double value);
876
877    /**
878     * Writes the given float to the current position and increases the position
879     * by 4.
880     * <p>
881     * The float is converted to bytes using the current byte order.
882     *
883     * @param value
884     *            the float to write.
885     * @return {@code this}
886     * @throws BufferOverflowException
887     *                if position is greater than {@code limit - 4}.
888     * @throws ReadOnlyBufferException
889     *                if no changes may be made to the contents of this buffer.
890     */
891    public abstract ByteBuffer putFloat(float value);
892
893    /**
894     * Writes the given float to the specified index of this buffer.
895     * <p>
896     * The float is converted to bytes using the current byte order. The
897     * position is not changed.
898     *
899     * @param index
900     *            the index, must not be negative and equal or less than
901     *            {@code limit - 4}.
902     * @param value
903     *            the float to write.
904     * @return {@code this}
905     * @throws IndexOutOfBoundsException
906     *                if {@code index} is invalid.
907     * @throws ReadOnlyBufferException
908     *                if no changes may be made to the contents of this buffer.
909     */
910    public abstract ByteBuffer putFloat(int index, float value);
911
912    /**
913     * Writes the given int to the current position and increases the position by
914     * 4.
915     * <p>
916     * The int is converted to bytes using the current byte order.
917     *
918     * @param value
919     *            the int to write.
920     * @return {@code this}
921     * @throws BufferOverflowException
922     *                if position is greater than {@code limit - 4}.
923     * @throws ReadOnlyBufferException
924     *                if no changes may be made to the contents of this buffer.
925     */
926    public abstract ByteBuffer putInt(int value);
927
928    /**
929     * Writes the given int to the specified index of this buffer.
930     * <p>
931     * The int is converted to bytes using the current byte order. The position
932     * is not changed.
933     *
934     * @param index
935     *            the index, must not be negative and equal or less than
936     *            {@code limit - 4}.
937     * @param value
938     *            the int to write.
939     * @return {@code this}
940     * @throws IndexOutOfBoundsException
941     *                if {@code index} is invalid.
942     * @throws ReadOnlyBufferException
943     *                if no changes may be made to the contents of this buffer.
944     */
945    public abstract ByteBuffer putInt(int index, int value);
946
947    /**
948     * Writes the given long to the current position and increases the position
949     * by 8.
950     * <p>
951     * The long is converted to bytes using the current byte order.
952     *
953     * @param value
954     *            the long to write.
955     * @return {@code this}
956     * @throws BufferOverflowException
957     *                if position is greater than {@code limit - 8}.
958     * @throws ReadOnlyBufferException
959     *                if no changes may be made to the contents of this buffer.
960     */
961    public abstract ByteBuffer putLong(long value);
962
963    /**
964     * Writes the given long to the specified index of this buffer.
965     * <p>
966     * The long is converted to bytes using the current byte order. The position
967     * is not changed.
968     *
969     * @param index
970     *            the index, must not be negative and equal or less than
971     *            {@code limit - 8}.
972     * @param value
973     *            the long to write.
974     * @return {@code this}
975     * @throws IndexOutOfBoundsException
976     *                if {@code index} is invalid.
977     * @throws ReadOnlyBufferException
978     *                if no changes may be made to the contents of this buffer.
979     */
980    public abstract ByteBuffer putLong(int index, long value);
981
982    /**
983     * Writes the given short to the current position and increases the position
984     * by 2.
985     * <p>
986     * The short is converted to bytes using the current byte order.
987     *
988     * @param value
989     *            the short to write.
990     * @return {@code this}
991     * @throws BufferOverflowException
992     *                if position is greater than {@code limit - 2}.
993     * @throws ReadOnlyBufferException
994     *                if no changes may be made to the contents of this buffer.
995     */
996    public abstract ByteBuffer putShort(short value);
997
998    /**
999     * Writes the given short to the specified index of this buffer.
1000     * <p>
1001     * The short is converted to bytes using the current byte order. The
1002     * position is not changed.
1003     *
1004     * @param index
1005     *            the index, must not be negative and equal or less than
1006     *            {@code limit - 2}.
1007     * @param value
1008     *            the short to write.
1009     * @return {@code this}
1010     * @throws IndexOutOfBoundsException
1011     *                if {@code index} is invalid.
1012     * @throws ReadOnlyBufferException
1013     *                if no changes may be made to the contents of this buffer.
1014     */
1015    public abstract ByteBuffer putShort(int index, short value);
1016
1017    /**
1018     * Returns a sliced buffer that shares its content with this buffer.
1019     * <p>
1020     * The sliced buffer's capacity will be this buffer's
1021     * {@code remaining()}, and it's zero position will correspond to
1022     * this buffer's current position. The new buffer's position will be 0,
1023     * limit will be its capacity, and its mark is cleared. The new buffer's
1024     * read-only property and byte order are the same as this buffer's.
1025     * <p>
1026     * The new buffer shares its content with this buffer, which means either
1027     * buffer's change of content will be visible to the other. The two buffers'
1028     * position, limit and mark are independent.
1029     */
1030    public abstract ByteBuffer slice();
1031}
1032