PageRenderTime 39ms CodeModel.GetById 21ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 17package com.hazelcast.nio;
 18
 19import java.io.*;
 20import java.nio.ByteBuffer;
 21import java.util.zip.DataFormatException;
 22import java.util.zip.Deflater;
 23import java.util.zip.Inflater;
 24
 25import static com.hazelcast.nio.IOUtil.closeResource;
 26
 27public final class PipedZipBufferFactory {
 28
 29    public static DeflatingPipedBuffer createDeflatingBuffer(int compressedDataSize) {
 30        return createDeflatingBuffer(compressedDataSize, Deflater.DEFAULT_COMPRESSION);
 31    }
 32
 33    public static DeflatingPipedBuffer createDeflatingBuffer(int compressedDataSize, int compressionLevel) {
 34        return new DeflatingPipedBufferImpl(compressedDataSize, compressionLevel);
 35    }
 36
 37    public static InflatingPipedBuffer createInflatingBuffer(int compressedDataSize) {
 38        return new InflatingPipedBufferImpl(compressedDataSize);
 39    }
 40
 41    public interface DeflatingPipedBuffer extends PipedZipBufferCommons {
 42        DataOutput getDataOutput();
 43
 44        int deflate();
 45    }
 46
 47    public interface InflatingPipedBuffer extends PipedZipBufferCommons {
 48        DataInput getDataInput();
 49
 50        int inflate() throws DataFormatException;
 51
 52        int inflate(int length) throws DataFormatException;
 53    }
 54
 55    private interface PipedZipBufferCommons {
 56        ByteBuffer getInputBuffer();
 57
 58        ByteBuffer getOutputBuffer();
 59
 60        OutputStream getOutputStream();
 61
 62        InputStream getInputStream();
 63
 64        void reset();
 65
 66        void destroy();
 67    }
 68
 69    private static class DeflatingPipedBufferImpl extends PipedZipBufferSupport implements DeflatingPipedBuffer {
 70        private final Deflater deflater;
 71        private DataOutputStream dataOutput;
 72
 73        private DeflatingPipedBufferImpl(int compressedDataSize, int compressionLevel) {
 74            super(compressedDataSize);
 75            deflater = new Deflater(compressionLevel);
 76            dataOutput = new DataOutputStream(getOutputStream());
 77        }
 78
 79        public int deflate() {
 80            try {
 81                deflater.setInput(uncompressedBuffer.array(), 0, uncompressedBuffer.position());
 82                deflater.finish();
 83                final int count = deflater.deflate(compressedBuffer.array());
 84                return count;
 85            } finally {
 86                deflater.reset();
 87            }
 88        }
 89
 90        @Override
 91        public void reset() {
 92            super.reset();
 93            deflater.reset();
 94        }
 95
 96        public ByteBuffer getInputBuffer() {
 97            return uncompressedBuffer;
 98        }
 99
100        public ByteBuffer getOutputBuffer() {
101            return compressedBuffer;
102        }
103
104        public DataOutput getDataOutput() {
105            return dataOutput;
106        }
107
108        public void destroy() {
109            closeResource(dataOutput);
110            dataOutput = null;
111            deflater.end();
112            super.destroy();
113        }
114    }
115
116    private static class InflatingPipedBufferImpl extends PipedZipBufferSupport implements InflatingPipedBuffer {
117        private final Inflater inflater = new Inflater();
118        private DataInputStream dataInput;
119
120        private InflatingPipedBufferImpl(int compressedDataSize) {
121            super(compressedDataSize);
122            dataInput = new DataInputStream(getInputStream());
123        }
124
125        public int inflate() throws DataFormatException {
126            return inflate(compressedBuffer.capacity());
127        }
128
129        public int inflate(int length) throws DataFormatException {
130            try {
131                inflater.setInput(compressedBuffer.array(), 0, length);
132                final int count = inflater.inflate(uncompressedBuffer.array());
133                uncompressedBuffer.limit(count);
134                uncompressedBuffer.position(0);
135                return count;
136            } finally {
137                inflater.reset();
138            }
139        }
140
141        @Override
142        public void reset() {
143            inflater.reset();
144            super.reset();
145        }
146
147        public ByteBuffer getInputBuffer() {
148            return compressedBuffer;
149        }
150
151        public ByteBuffer getOutputBuffer() {
152            return uncompressedBuffer;
153        }
154
155        public DataInput getDataInput() {
156            return dataInput;
157        }
158
159        public void destroy() {
160            closeResource(dataInput);
161            dataInput = null;
162            inflater.end();
163            super.destroy();
164        }
165    }
166
167    private static abstract class PipedZipBufferSupport implements PipedZipBufferCommons {
168        protected ByteBuffer compressedBuffer;
169        protected ByteBuffer uncompressedBuffer;
170        protected InputStream inputStream;
171        protected OutputStream outputStream;
172
173        private PipedZipBufferSupport(int compressedDataSize) {
174            super();
175            compressedBuffer = ByteBuffer.allocate(compressedDataSize);
176            uncompressedBuffer = ByteBuffer.allocate(compressedDataSize * 10);
177            outputStream = IOUtil.newOutputStream(getInputBuffer());
178            inputStream = IOUtil.newInputStream(getOutputBuffer());
179        }
180
181        public final OutputStream getOutputStream() {
182            return outputStream;
183        }
184
185        public final InputStream getInputStream() {
186            return inputStream;
187        }
188
189        public void reset() {
190            uncompressedBuffer.clear();
191            compressedBuffer.clear();
192        }
193
194        public void destroy() {
195            closeResource(inputStream);
196            closeResource(outputStream);
197            compressedBuffer = null;
198            uncompressedBuffer = null;
199            inputStream = null;
200            outputStream = null;
201        }
202    }
203}