328 lines
8.7 KiB
Java
328 lines
8.7 KiB
Java
|
/*
|
||
|
* Copyright 2013 The Netty Project
|
||
|
*
|
||
|
* The Netty Project licenses this file to you under the Apache License,
|
||
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
||
|
* with the License. You may obtain a copy of the License at:
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||
|
* License for the specific language governing permissions and limitations
|
||
|
* under the License.
|
||
|
*/
|
||
|
package io.netty.buffer;
|
||
|
|
||
|
import io.netty.util.ResourceLeak;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.OutputStream;
|
||
|
import java.nio.ByteBuffer;
|
||
|
import java.nio.ByteOrder;
|
||
|
import java.nio.ReadOnlyBufferException;
|
||
|
import java.nio.channels.GatheringByteChannel;
|
||
|
import java.nio.channels.ScatteringByteChannel;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Read-only ByteBuf which wraps a read-only direct ByteBuffer.
|
||
|
*/
|
||
|
class ReadOnlyDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||
|
private final ResourceLeak leak = leakDetector.open(this);
|
||
|
|
||
|
protected final ByteBuffer buffer;
|
||
|
private final ByteBufAllocator allocator;
|
||
|
private ByteBuffer tmpNioBuf;
|
||
|
|
||
|
public ReadOnlyDirectByteBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
|
||
|
super(buffer.remaining());
|
||
|
if (!buffer.isDirect()) {
|
||
|
throw new IllegalArgumentException("buffer must be readOnly");
|
||
|
}
|
||
|
this.allocator = allocator;
|
||
|
this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
|
||
|
writerIndex(buffer.limit());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void deallocate() {
|
||
|
leak.close();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public byte getByte(int index) {
|
||
|
ensureAccessible();
|
||
|
return _getByte(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected byte _getByte(int index) {
|
||
|
return buffer.get(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public short getShort(int index) {
|
||
|
ensureAccessible();
|
||
|
return _getShort(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected short _getShort(int index) {
|
||
|
return buffer.getShort(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getUnsignedMedium(int index) {
|
||
|
ensureAccessible();
|
||
|
return _getUnsignedMedium(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected int _getUnsignedMedium(int index) {
|
||
|
return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getInt(int index) {
|
||
|
ensureAccessible();
|
||
|
return _getInt(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected int _getInt(int index) {
|
||
|
return buffer.getInt(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public long getLong(int index) {
|
||
|
ensureAccessible();
|
||
|
return _getLong(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected long _getLong(int index) {
|
||
|
return buffer.getLong(index);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||
|
checkDstIndex(index, length, dstIndex, dst.capacity());
|
||
|
if (dst.hasArray()) {
|
||
|
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
||
|
} else if (dst.nioBufferCount() > 0) {
|
||
|
for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
|
||
|
int bbLen = bb.remaining();
|
||
|
getBytes(index, bb);
|
||
|
index += bbLen;
|
||
|
}
|
||
|
} else {
|
||
|
dst.setBytes(dstIndex, this, index, length);
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||
|
checkDstIndex(index, length, dstIndex, dst.length);
|
||
|
if (dst == null) {
|
||
|
throw new NullPointerException("dst");
|
||
|
}
|
||
|
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
||
|
throw new IndexOutOfBoundsException(String.format(
|
||
|
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
|
||
|
}
|
||
|
|
||
|
ByteBuffer tmpBuf = internalNioBuffer();
|
||
|
tmpBuf.clear().position(index).limit(index + length);
|
||
|
tmpBuf.get(dst, dstIndex, length);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||
|
checkIndex(index);
|
||
|
if (dst == null) {
|
||
|
throw new NullPointerException("dst");
|
||
|
}
|
||
|
|
||
|
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
||
|
ByteBuffer tmpBuf = internalNioBuffer();
|
||
|
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
||
|
dst.put(tmpBuf);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void _setByte(int index, int value) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void _setShort(int index, int value) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void _setMedium(int index, int value) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void _setInt(int index, int value) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void _setLong(int index, long value) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int capacity() {
|
||
|
return maxCapacity();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf capacity(int newCapacity) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBufAllocator alloc() {
|
||
|
return allocator;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteOrder order() {
|
||
|
return ByteOrder.BIG_ENDIAN;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf unwrap() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isDirect() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf setBytes(int index, ByteBuffer src) {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int setBytes(int index, InputStream in, int length) throws IOException {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
||
|
throw new ReadOnlyBufferException();
|
||
|
}
|
||
|
|
||
|
protected final ByteBuffer internalNioBuffer() {
|
||
|
ByteBuffer tmpNioBuf = this.tmpNioBuf;
|
||
|
if (tmpNioBuf == null) {
|
||
|
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
|
||
|
}
|
||
|
return tmpNioBuf;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf copy(int index, int length) {
|
||
|
ensureAccessible();
|
||
|
ByteBuffer src;
|
||
|
try {
|
||
|
src = (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
|
||
|
} catch (IllegalArgumentException e) {
|
||
|
throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length));
|
||
|
}
|
||
|
|
||
|
ByteBuffer dst = ByteBuffer.allocateDirect(length);
|
||
|
dst.put(src);
|
||
|
dst.order(order());
|
||
|
dst.clear();
|
||
|
return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int nioBufferCount() {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuffer nioBuffer(int index, int length) {
|
||
|
ensureAccessible();
|
||
|
if (index == 0 && length == capacity()) {
|
||
|
return buffer.duplicate();
|
||
|
} else {
|
||
|
return ((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length)).slice();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuffer[] nioBuffers(int index, int length) {
|
||
|
return new ByteBuffer[] { nioBuffer(index, length) };
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean hasArray() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public byte[] array() {
|
||
|
throw new UnsupportedOperationException("direct buffer");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int arrayOffset() {
|
||
|
throw new UnsupportedOperationException("direct buffer");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean hasMemoryAddress() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public long memoryAddress() {
|
||
|
throw new UnsupportedOperationException();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf suspendIntermediaryDeallocations() {
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ByteBuf resumeIntermediaryDeallocations() {
|
||
|
return this;
|
||
|
}
|
||
|
}
|