Created
July 9, 2019 20:13
-
-
Save arhimondr/f2ada22d2a0b47ccf718eb50945121d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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