12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package com.sdyc.ndmp.protobuf.serializer;
- import org.apache.commons.compress.compressors.CompressorException;
- import org.apache.commons.compress.compressors.CompressorInputStream;
- import org.apache.commons.compress.compressors.CompressorOutputStream;
- import org.apache.commons.compress.compressors.CompressorStreamFactory;
- import org.apache.commons.io.IOUtils;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- /**
- * <pre>
- * Created with IntelliJ IDEA.
- * User: lwj
- * Date: 2015/2/27
- * Time: 10:48
- * To change this template use File | Settings | File Templates.
- * </pre>
- *
- * @author lwj
- */
- public class GZipSerializer extends CompressSerializer {
- public GZipSerializer() {
- }
- @Override
- public byte[] compress(byte[] bytes) {
- CompressorInputStream inputStream = null;
- try {
- inputStream = new CompressorStreamFactory().createCompressorInputStream(
- CompressorStreamFactory.GZIP, new ByteArrayInputStream(bytes));
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- IOUtils.copy(inputStream, out);
- inputStream.close();
- return out.toByteArray();
- } catch (IOException e) {
- throw new IllegalStateException(e);
- } catch (CompressorException e) {
- throw new IllegalStateException(e);
- }
- }
- @Override
- public byte[] uncompress(byte[] bytes) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- try {
- CompressorOutputStream outputStream = new CompressorStreamFactory().createCompressorOutputStream(
- CompressorStreamFactory.GZIP, out);
- IOUtils.copy(new ByteArrayInputStream(bytes), outputStream);
- outputStream.close();
- return out.toByteArray();
- } catch (IOException e) {
- throw new IllegalStateException(e);
- } catch (CompressorException e) {
- throw new IllegalStateException(e);
- }
- }
- }
|