Skip to content

Instantly share code, notes, and snippets.

@ebfhub
Created February 4, 2020 23:08
Show Gist options
  • Save ebfhub/0ea1ab5926696a478508c89b456addd9 to your computer and use it in GitHub Desktop.
Save ebfhub/0ea1ab5926696a478508c89b456addd9 to your computer and use it in GitHub Desktop.
import io.netty.buffer.AbstractByteBuf;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.agrona.DirectBuffer;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.util.function.Consumer;
public class ByteBufWrappingDirectBuffer extends AbstractByteBuf {
private DirectBuffer contents;
private Consumer<ByteBufWrappingDirectBuffer> onRelease;
private int refCount=1;
private ByteOrder bo = ByteOrder.BIG_ENDIAN;
public ByteBufWrappingDirectBuffer(int maxCapacity) {
super(maxCapacity);
}
public void setContents(DirectBuffer c){
this.contents=c;
this.setIndex(0,c.capacity());
}
@Override
protected byte _getByte(int index) {
return contents.getByte(index);
}
@Override
protected short _getShort(int index) {
return contents.getShort(index,bo);
}
@Override
protected short _getShortLE(int index) {
return contents.getShort(index,ByteOrder.LITTLE_ENDIAN);
}
@Override
protected int _getUnsignedMedium(int index) {
throw new UnsupportedOperationException();
}
@Override
protected int _getUnsignedMediumLE(int index) {
throw new UnsupportedOperationException();
}
@Override
protected int _getInt(int index) {
return contents.getInt(index,bo);
}
@Override
protected int _getIntLE(int index) {
return contents.getInt(index,ByteOrder.LITTLE_ENDIAN);
}
@Override
protected long _getLong(int index) {
return contents.getLong(index,bo);
}
@Override
protected long _getLongLE(int index) {
return contents.getLong(index,ByteOrder.LITTLE_ENDIAN);
}
@Override
protected void _setByte(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setShort(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setShortLE(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setMedium(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setMediumLE(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setInt(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setIntLE(int index, int value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setLong(int index, long value) {
throw new UnsupportedOperationException();
}
@Override
protected void _setLongLE(int index, long value) {
throw new UnsupportedOperationException();
}
@Override
public int capacity() {
return contents.capacity();
}
@Override
public ByteBuf capacity(int newCapacity) {
throw new UnsupportedOperationException();
}
@Override
public ByteBufAllocator alloc() {
throw new UnsupportedOperationException();
}
@Override
public ByteOrder order() {
return ByteOrder.nativeOrder();
}
@Override
public ByteBuf unwrap() {
throw new UnsupportedOperationException();
}
@Override
public boolean isDirect() {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf getBytes(int index, ByteBuffer dst) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf getBytes(int index, OutputStream out, int length) {
throw new UnsupportedOperationException();
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) {
throw new UnsupportedOperationException();
}
@Override
public int getBytes(int index, FileChannel out, long position, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf setBytes(int index, ByteBuffer src) {
throw new UnsupportedOperationException();
}
@Override
public int setBytes(int index, InputStream in, int length) {
throw new UnsupportedOperationException();
}
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) {
throw new UnsupportedOperationException();
}
@Override
public int setBytes(int index, FileChannel in, long position, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf copy(int index, int length) {
throw new UnsupportedOperationException();
}
@Override
public int nioBufferCount() {
throw new UnsupportedOperationException();
}
@Override
public ByteBuffer nioBuffer(int index, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuffer[] nioBuffers(int index, int length) {
throw new UnsupportedOperationException();
}
@Override
public boolean hasArray() {
return false;
}
@Override
public byte[] array() {
throw new UnsupportedOperationException();
}
@Override
public int arrayOffset() {
throw new UnsupportedOperationException();
}
@Override
public boolean hasMemoryAddress() {
return false;
}
@Override
public long memoryAddress() {
throw new UnsupportedOperationException();
}
@Override
public int refCnt() {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf touch() {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf touch(Object hint) {
throw new UnsupportedOperationException();
}
@Override
public ByteBuf retain(int increment) {
refCount+=increment;
return this;
}
@Override
public ByteBuf retain() {
return retain(1);
}
@Override
public boolean release() {
return release(1);
}
@Override
public boolean release(int decrement) {
refCount-=decrement;
if(refCount<=0) {
onRelease.accept(this);
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment