/hazelcast/src/main/java/com/hazelcast/nio/PipedZipBufferFactory.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 203 lines · 147 code · 41 blank · 15 comment · 0 complexity · d6975966740f9a1de0dfc1cf175ffa3d MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.nio;
  17. import java.io.*;
  18. import java.nio.ByteBuffer;
  19. import java.util.zip.DataFormatException;
  20. import java.util.zip.Deflater;
  21. import java.util.zip.Inflater;
  22. import static com.hazelcast.nio.IOUtil.closeResource;
  23. public final class PipedZipBufferFactory {
  24. public static DeflatingPipedBuffer createDeflatingBuffer(int compressedDataSize) {
  25. return createDeflatingBuffer(compressedDataSize, Deflater.DEFAULT_COMPRESSION);
  26. }
  27. public static DeflatingPipedBuffer createDeflatingBuffer(int compressedDataSize, int compressionLevel) {
  28. return new DeflatingPipedBufferImpl(compressedDataSize, compressionLevel);
  29. }
  30. public static InflatingPipedBuffer createInflatingBuffer(int compressedDataSize) {
  31. return new InflatingPipedBufferImpl(compressedDataSize);
  32. }
  33. public interface DeflatingPipedBuffer extends PipedZipBufferCommons {
  34. DataOutput getDataOutput();
  35. int deflate();
  36. }
  37. public interface InflatingPipedBuffer extends PipedZipBufferCommons {
  38. DataInput getDataInput();
  39. int inflate() throws DataFormatException;
  40. int inflate(int length) throws DataFormatException;
  41. }
  42. private interface PipedZipBufferCommons {
  43. ByteBuffer getInputBuffer();
  44. ByteBuffer getOutputBuffer();
  45. OutputStream getOutputStream();
  46. InputStream getInputStream();
  47. void reset();
  48. void destroy();
  49. }
  50. private static class DeflatingPipedBufferImpl extends PipedZipBufferSupport implements DeflatingPipedBuffer {
  51. private final Deflater deflater;
  52. private DataOutputStream dataOutput;
  53. private DeflatingPipedBufferImpl(int compressedDataSize, int compressionLevel) {
  54. super(compressedDataSize);
  55. deflater = new Deflater(compressionLevel);
  56. dataOutput = new DataOutputStream(getOutputStream());
  57. }
  58. public int deflate() {
  59. try {
  60. deflater.setInput(uncompressedBuffer.array(), 0, uncompressedBuffer.position());
  61. deflater.finish();
  62. final int count = deflater.deflate(compressedBuffer.array());
  63. return count;
  64. } finally {
  65. deflater.reset();
  66. }
  67. }
  68. @Override
  69. public void reset() {
  70. super.reset();
  71. deflater.reset();
  72. }
  73. public ByteBuffer getInputBuffer() {
  74. return uncompressedBuffer;
  75. }
  76. public ByteBuffer getOutputBuffer() {
  77. return compressedBuffer;
  78. }
  79. public DataOutput getDataOutput() {
  80. return dataOutput;
  81. }
  82. public void destroy() {
  83. closeResource(dataOutput);
  84. dataOutput = null;
  85. deflater.end();
  86. super.destroy();
  87. }
  88. }
  89. private static class InflatingPipedBufferImpl extends PipedZipBufferSupport implements InflatingPipedBuffer {
  90. private final Inflater inflater = new Inflater();
  91. private DataInputStream dataInput;
  92. private InflatingPipedBufferImpl(int compressedDataSize) {
  93. super(compressedDataSize);
  94. dataInput = new DataInputStream(getInputStream());
  95. }
  96. public int inflate() throws DataFormatException {
  97. return inflate(compressedBuffer.capacity());
  98. }
  99. public int inflate(int length) throws DataFormatException {
  100. try {
  101. inflater.setInput(compressedBuffer.array(), 0, length);
  102. final int count = inflater.inflate(uncompressedBuffer.array());
  103. uncompressedBuffer.limit(count);
  104. uncompressedBuffer.position(0);
  105. return count;
  106. } finally {
  107. inflater.reset();
  108. }
  109. }
  110. @Override
  111. public void reset() {
  112. inflater.reset();
  113. super.reset();
  114. }
  115. public ByteBuffer getInputBuffer() {
  116. return compressedBuffer;
  117. }
  118. public ByteBuffer getOutputBuffer() {
  119. return uncompressedBuffer;
  120. }
  121. public DataInput getDataInput() {
  122. return dataInput;
  123. }
  124. public void destroy() {
  125. closeResource(dataInput);
  126. dataInput = null;
  127. inflater.end();
  128. super.destroy();
  129. }
  130. }
  131. private static abstract class PipedZipBufferSupport implements PipedZipBufferCommons {
  132. protected ByteBuffer compressedBuffer;
  133. protected ByteBuffer uncompressedBuffer;
  134. protected InputStream inputStream;
  135. protected OutputStream outputStream;
  136. private PipedZipBufferSupport(int compressedDataSize) {
  137. super();
  138. compressedBuffer = ByteBuffer.allocate(compressedDataSize);
  139. uncompressedBuffer = ByteBuffer.allocate(compressedDataSize * 10);
  140. outputStream = IOUtil.newOutputStream(getInputBuffer());
  141. inputStream = IOUtil.newInputStream(getOutputBuffer());
  142. }
  143. public final OutputStream getOutputStream() {
  144. return outputStream;
  145. }
  146. public final InputStream getInputStream() {
  147. return inputStream;
  148. }
  149. public void reset() {
  150. uncompressedBuffer.clear();
  151. compressedBuffer.clear();
  152. }
  153. public void destroy() {
  154. closeResource(inputStream);
  155. closeResource(outputStream);
  156. compressedBuffer = null;
  157. uncompressedBuffer = null;
  158. inputStream = null;
  159. outputStream = null;
  160. }
  161. }
  162. }