|
| 1 | +/* |
| 2 | + * This is free and unencumbered software released into the public domain. |
| 3 | + * |
| 4 | + * Anyone is free to copy, modify, publish, use, compile, sell, or |
| 5 | + * distribute this software, either in source code form or as a compiled |
| 6 | + * binary, for any purpose, commercial or non-commercial, and by any |
| 7 | + * means. |
| 8 | + * |
| 9 | + * In jurisdictions that recognize copyright laws, the author or authors |
| 10 | + * of this software dedicate any and all copyright interest in the |
| 11 | + * software to the public domain. We make this dedication for the benefit |
| 12 | + * of the public at large and to the detriment of our heirs and |
| 13 | + * successors. We intend this dedication to be an overt act of |
| 14 | + * relinquishment in perpetuity of all present and future rights to this |
| 15 | + * software under copyright law. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + * |
| 25 | + * For more information, please refer to <http://unlicense.org/> |
| 26 | + */ |
| 27 | +package java.nio; |
| 28 | + |
| 29 | +/** |
| 30 | + * Extensible heap-based {@link ByteBuffer} |
| 31 | + * <p> |
| 32 | + * This class is to serve as the base class for customization on the top of {@link ByteBuffer} and |
| 33 | + * {@link HeapByteBuffer}. |
| 34 | + * </p> |
| 35 | + * |
| 36 | + * @author aqd |
| 37 | + */ |
| 38 | +public class ExtensibleHeapByteBuffer extends HeapByteBuffer { |
| 39 | + |
| 40 | + /** |
| 41 | + * Create a new {@link ByteBuffer} wrapper |
| 42 | + * |
| 43 | + * @param buf {@link Buffer#array} |
| 44 | + * @param mark the value of <i>mark</i> |
| 45 | + * @param pos {@link Buffer#position} |
| 46 | + * @param lim {@link Buffer#limit} |
| 47 | + * @param cap {@link Buffer#capacity} |
| 48 | + * @param off {@link Buffer#arrayOffset} |
| 49 | + */ |
| 50 | + public ExtensibleHeapByteBuffer(byte[] buf, |
| 51 | + int mark, int pos, int lim, int cap, |
| 52 | + int off) { |
| 53 | + super(buf, mark, pos, lim, cap, off); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a new {@link ByteBuffer} wrapper from parameters of a given underlyingBuffer |
| 58 | + * <p> |
| 59 | + * The newly-created {@link ExtensibleHeapByteBuffer} would be independent from {@code underlyingBuffer}. |
| 60 | + * </p> |
| 61 | + * |
| 62 | + * @param underlyingBuffer the underlying buffer to be wrapped |
| 63 | + */ |
| 64 | + public ExtensibleHeapByteBuffer(ByteBuffer underlyingBuffer) { |
| 65 | + this((HeapByteBuffer) underlyingBuffer); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a new {@link ByteBuffer} wrapper from parameters of a given underlyingBuffer |
| 70 | + * <p> |
| 71 | + * The newly-created {@link ExtensibleHeapByteBuffer} would be independent from {@code underlyingBuffer}. |
| 72 | + * </p> |
| 73 | + * |
| 74 | + * @param underlyingBuffer the underlying buffer to be wrapped |
| 75 | + */ |
| 76 | + ExtensibleHeapByteBuffer(HeapByteBuffer underlyingBuffer) { |
| 77 | + super(underlyingBuffer.hb, |
| 78 | + underlyingBuffer.markValue(), |
| 79 | + underlyingBuffer.position(), |
| 80 | + underlyingBuffer.limit(), |
| 81 | + underlyingBuffer.capacity(), |
| 82 | + underlyingBuffer.offset); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the position of the <i>mark</i> |
| 87 | + * |
| 88 | + * @return the <i>mark</i> |
| 89 | + */ |
| 90 | + protected final int markValue2() { |
| 91 | + return this.markValue(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public ExtensibleHeapByteBuffer slice() { |
| 96 | + return new ExtensibleHeapByteBuffer(this.hb, |
| 97 | + -1, |
| 98 | + 0, |
| 99 | + this.remaining(), |
| 100 | + this.remaining(), |
| 101 | + this.position() + this.offset); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public ExtensibleHeapByteBuffer duplicate() { |
| 106 | + return new ExtensibleHeapByteBuffer(this.hb, |
| 107 | + this.markValue(), |
| 108 | + this.position(), |
| 109 | + this.limit(), |
| 110 | + this.capacity(), |
| 111 | + this.offset); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public ExtensibleHeapByteBufferR asReadOnlyBuffer() { |
| 116 | + return new ExtensibleHeapByteBufferR(this.hb, |
| 117 | + this.markValue(), |
| 118 | + this.position(), |
| 119 | + this.limit(), |
| 120 | + this.capacity(), |
| 121 | + this.offset); |
| 122 | + |
| 123 | + } |
| 124 | +} |
0 commit comments