Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement