/** * Sets the specified byte at the specified absolute {@code index} in this * buffer. The 24 high-order bits of the specified value are ignored. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 1} is greater than {@code this.capacity} */ public void setByte(int index, int value) { Preconditions.checkPositionIndexes(index, index + 1, this.length); index += offset; data[index] = (byte) value; }
/** * Gets a byte at the specified absolute {@code index} in this buffer. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 1} is greater than {@code this.capacity} */ public byte getByte(int index) { Preconditions.checkPositionIndexes(index, index + 1, this.length); index += offset; return data[index]; }
/** * Gets a 16-bit short integer at the specified absolute {@code index} in * this slice. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 2} is greater than {@code this.capacity} */ public short getShort(int index) { Preconditions.checkPositionIndexes(index, index + Shorts.BYTES, this.length); index += offset; return (short) ((data[index] & 0xFF) | (data[index + 1] << 8)); }
/** * Sets the specified 16-bit short integer at the specified absolute * {@code index} in this buffer. The 16 high-order bits of the specified * value are ignored. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 2} is greater than {@code this.capacity} */ public void setShort(int index, int value) { Preconditions.checkPositionIndexes(index, index + Shorts.BYTES, this.length); index += offset; data[index] = (byte) (value); data[index + 1] = (byte) (value >>> 8); }
/** * Sets the specified 32-bit integer at the specified absolute * {@code index} in this buffer. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 4} is greater than {@code this.capacity} */ public void setInt(int index, int value) { Preconditions.checkPositionIndexes(index, index + Ints.BYTES, this.length); index += offset; data[index] = (byte) (value); data[index + 1] = (byte) (value >>> 8); data[index + 2] = (byte) (value >>> 16); data[index + 3] = (byte) (value >>> 24); }
/** * Gets a 32-bit integer at the specified absolute {@code index} in * this buffer. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 4} is greater than {@code this.capacity} */ public int getInt(int index) { Preconditions.checkPositionIndexes(index, index + Ints.BYTES, this.length); index += offset; return (data[index] & 0xff) | (data[index + 1] & 0xff) << 8 | (data[index + 2] & 0xff) << 16 | (data[index + 3] & 0xff) << 24; }
/** * Gets a 64-bit long integer at the specified absolute {@code index} in * this buffer. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 8} is greater than {@code this.capacity} */ public long getLong(int index) { Preconditions.checkPositionIndexes(index, index + Longs.BYTES, this.length); index += offset; return ((long) data[index] & 0xff) | ((long) data[index + 1] & 0xff) << 8 | ((long) data[index + 2] & 0xff) << 16 | ((long) data[index + 3] & 0xff) << 24 | ((long) data[index + 4] & 0xff) << 32 | ((long) data[index + 5] & 0xff) << 40 | ((long) data[index + 6] & 0xff) << 48 | ((long) data[index + 7] & 0xff) << 56; }
/** * Sets the specified 64-bit long integer at the specified absolute * {@code index} in this buffer. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * {@code index + 8} is greater than {@code this.capacity} */ public void setLong(int index, long value) { Preconditions.checkPositionIndexes(index, index + Longs.BYTES, this.length); index += offset; data[index] = (byte) (value); data[index + 1] = (byte) (value >>> 8); data[index + 2] = (byte) (value >>> 16); data[index + 3] = (byte) (value >>> 24); data[index + 4] = (byte) (value >>> 32); data[index + 5] = (byte) (value >>> 40); data[index + 6] = (byte) (value >>> 48); data[index + 7] = (byte) (value >>> 56); }
/** * Transfers the specified source array's data to this buffer starting at * the specified absolute {@code index}. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, * if the specified {@code srcIndex} is less than {@code 0}, * if {@code index + length} is greater than * {@code this.capacity}, or * if {@code srcIndex + length} is greater than {@code src.length} */ public void setBytes(int index, byte[] source, int sourceIndex, int length) { Preconditions.checkPositionIndexes(index, index + length, this.length); Preconditions.checkPositionIndexes(sourceIndex, sourceIndex + length, source.length); index += offset; System.arraycopy(source, sourceIndex, data, index, length); }
/** * Converts this buffer's sub-region into a NIO buffer. The returned * buffer shares the content with this buffer. */ public ByteBuffer toByteBuffer(int index, int length) { Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; return ByteBuffer.wrap(data, index, length).order(LITTLE_ENDIAN); }
public byte[] copyBytes(int index, int length) { Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; if (index == 0) { return Arrays.copyOf(data, length); } else { byte[] value = new byte[length]; System.arraycopy(data, index, value, 0, length); return value; } }
/** * Transfers this buffer's data to the specified destination starting at * the specified absolute {@code index}. * * @param destinationIndex the first index of the destination * @param length the number of bytes to transfer * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, * if the specified {@code dstIndex} is less than {@code 0}, * if {@code index + length} is greater than * {@code this.capacity}, or * if {@code dstIndex + length} is greater than * {@code dst.length} */ public void getBytes(int index, byte[] destination, int destinationIndex, int length) { Preconditions.checkPositionIndexes(index, index + length, this.length); Preconditions.checkPositionIndexes(destinationIndex, destinationIndex + length, destination.length); index += offset; System.arraycopy(data, index, destination, destinationIndex, length); }
/** * Transfers this buffer's data to the specified stream starting at the * specified absolute {@code index}. * * @param length the number of bytes to transfer * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * if {@code index + length} is greater than * {@code this.capacity} * @throws java.io.IOException if the specified stream threw an exception during I/O */ public void getBytes(int index, OutputStream out, int length) throws IOException { Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; out.write(data, index, length); }
/** * Transfers the specified source buffer's data to this buffer starting at * the specified absolute {@code index} until the source buffer's position * reaches its limit. * * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * if {@code index + src.remaining()} is greater than * {@code this.capacity} */ public void setBytes(int index, ByteBuffer source) { Preconditions.checkPositionIndexes(index, index + source.remaining(), this.length); index += offset; source.get(data, index, source.remaining()); }
/** * Returns a copy of this buffer's sub-region. Modifying the content of * the returned buffer or this buffer does not affect each other at all. */ public Slice copySlice(int index, int length) { Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; byte[] copiedArray = new byte[length]; System.arraycopy(data, index, copiedArray, 0, length); return new Slice(copiedArray); }
Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; int readBytes = 0;
public int setBytes(int index, FileChannel in, int position, int length) throws IOException { Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; ByteBuffer buf = ByteBuffer.wrap(data, index, length); int readBytes = 0; do { int localReadBytes; try { localReadBytes = in.read(buf, position + readBytes); } catch (ClosedChannelException e) { localReadBytes = -1; } if (localReadBytes < 0) { if (readBytes == 0) { return -1; } else { break; } } else if (localReadBytes == 0) { break; } readBytes += localReadBytes; } while (readBytes < length); return readBytes; }
/** * Returns a slice of this buffer's sub-region. Modifying the content of * the returned buffer or this buffer affects each other's content while * they maintain separate indexes and marks. */ public Slice slice(int index, int length) { if (index == 0 && length == this.length) { return this; } Preconditions.checkPositionIndexes(index, index + length, this.length); if (index >= 0 && length == 0) { return Slices.EMPTY_SLICE; } return new Slice(data, offset + index, length); }
/** * Transfers this buffer's data to the specified channel starting at the * specified absolute {@code index}. * * @param length the maximum number of bytes to transfer * @return the actual number of bytes written out to the specified channel * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or * if {@code index + length} is greater than * {@code this.capacity} * @throws java.io.IOException if the specified channel threw an exception during I/O */ public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; return out.write(ByteBuffer.wrap(data, index, length)); }
Preconditions.checkPositionIndexes(index, index + length, this.length); index += offset; ByteBuffer buf = ByteBuffer.wrap(data, index, length);