Skip to content

Instantly share code, notes, and snippets.

@astei
Created September 22, 2019 05:46
Show Gist options
  • Save astei/71e6fc63394eca684954ac5eaa85345b to your computer and use it in GitHub Desktop.
Save astei/71e6fc63394eca684954ac5eaa85345b to your computer and use it in GitHub Desktop.
package com.velocitypowered.proxy.protocol.util;
import com.google.common.io.ByteArrayDataInput;
import io.netty.buffer.ByteBuf;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
/**
* A wrapper around {@link io.netty.buffer.ByteBuf} that implements the exception-free
* {@link ByteArrayDataInput} interface from Guava.
*/
public class ByteBufDataInput implements ByteArrayDataInput, DataInput {
private final ByteBuf in;
/**
* Creates a new ByteBufDataInput instance. The ByteBufDataInput simply "borrows" the ByteBuf
* while it is in use.
*
* @param buf the buffer to read from
*/
public ByteBufDataInput(ByteBuf buf) {
this.in = buf;
}
@Override
public void readFully(byte[] b) {
in.readBytes(b);
}
@Override
public void readFully(byte[] b, int off, int len) {
in.readBytes(b, off, len);
}
@Override
public int skipBytes(int n) {
in.skipBytes(n);
}
@Override
public boolean readBoolean() {
return in.readBoolean();
}
@Override
public byte readByte() {
return in.readByte();
}
@Override
public int readUnsignedByte() {
return in.readUnsignedByte() & 0xFF;
}
@Override
public short readShort() {
return in.readShort();
}
@Override
public int readUnsignedShort() {
return in.readUnsignedShort();
}
@Override
public char readChar() {
return in.readChar();
}
@Override
public int readInt() {
return in.readInt();
}
@Override
public long readLong() {
return in.readLong();
}
@Override
public float readFloat() {
return in.readFloat();
}
@Override
public double readDouble() {
return in.readDouble();
}
@Override
public String readLine() {
throw new UnsupportedOperationException();
}
@Override
public String readUTF() {
try {
return DataInputStream.readUTF(this);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment