Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. public enum Transform
  2. {
  3. ZLIB_TRANSFORM(0x01) {
  4.  
  5. private static final int ZLIB_COMPRESSION_BUFFER_SIZE = 512;
  6.  
  7. @Override
  8. public ByteBuf transform(ByteBuf data)
  9. {
  10. ByteBuf output = Unpooled.buffer(ZLIB_COMPRESSION_BUFFER_SIZE);
  11. try (DeflaterOutputStream outputStream = new DeflaterOutputStream(new ByteBufOutputStream(output))) {
  12. data.readBytes(outputStream, data.readableBytes());
  13. }
  14. catch (IOException e) {
  15. throw new UncheckedIOException(e);
  16. }
  17. finally {
  18. data.release();
  19. }
  20. return output;
  21. }
  22.  
  23. @Override
  24. public ByteBuf untransform(ByteBuf data)
  25. {
  26. try (InflaterInputStream inputStream = new InflaterInputStream(new ByteBufInputStream(data))) {
  27. ByteBuf output = Unpooled.buffer(ZLIB_COMPRESSION_BUFFER_SIZE);
  28. while (inputStream.available() > 0) {
  29. output.writeBytes(inputStream, ZLIB_COMPRESSION_BUFFER_SIZE);
  30. }
  31. return output;
  32. }
  33. catch (IOException e) {
  34. throw new UncheckedIOException(e);
  35. }
  36. finally {
  37. data.release();
  38. }
  39. }
  40. };
  41.  
  42. private final int id;
  43.  
  44. Transform(int id)
  45. {
  46. this.id = id;
  47. }
  48.  
  49. public int getId()
  50. {
  51. return id;
  52. }
  53.  
  54. public abstract ByteBuf transform(ByteBuf data);
  55.  
  56. public abstract ByteBuf untransform(ByteBuf data);
  57.  
  58. public static Transform valueOf(int id)
  59. {
  60. for (Transform transform : values()) {
  61. if (transform.getId() == id) {
  62. return transform;
  63. }
  64. }
  65. throw new IllegalArgumentException(format("Unknown transform %s during receive", id));
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement