Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arhimondr/f2ada22d2a0b47ccf718eb50945121d3 to your computer and use it in GitHub Desktop.
Save arhimondr/f2ada22d2a0b47ccf718eb50945121d3 to your computer and use it in GitHub Desktop.
public enum Transform
{
ZLIB_TRANSFORM(0x01) {
private static final int ZLIB_COMPRESSION_BUFFER_SIZE = 512;
@Override
public ByteBuf transform(ByteBuf data)
{
ByteBuf output = Unpooled.buffer(ZLIB_COMPRESSION_BUFFER_SIZE);
try (DeflaterOutputStream outputStream = new DeflaterOutputStream(new ByteBufOutputStream(output))) {
data.readBytes(outputStream, data.readableBytes());
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
finally {
data.release();
}
return output;
}
@Override
public ByteBuf untransform(ByteBuf data)
{
try (InflaterInputStream inputStream = new InflaterInputStream(new ByteBufInputStream(data))) {
ByteBuf output = Unpooled.buffer(ZLIB_COMPRESSION_BUFFER_SIZE);
while (inputStream.available() > 0) {
output.writeBytes(inputStream, ZLIB_COMPRESSION_BUFFER_SIZE);
}
return output;
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
finally {
data.release();
}
}
};
private final int id;
Transform(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public abstract ByteBuf transform(ByteBuf data);
public abstract ByteBuf untransform(ByteBuf data);
public static Transform valueOf(int id)
{
for (Transform transform : values()) {
if (transform.getId() == id) {
return transform;
}
}
throw new IllegalArgumentException(format("Unknown transform %s during receive", id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment