Java Code Examples for java.util.zip.DeflaterOutputStream

The following examples show how to use java.util.zip.DeflaterOutputStream. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: netcdf-java   Author: Unidata   File: TimeCompression.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static public void deflateRandom(String filenameOut, boolean buffer) throws IOException {
  FileOutputStream fout = new FileOutputStream(filenameOut);
  OutputStream out = new DeflaterOutputStream(fout);
  if (buffer)
    out = new BufferedOutputStream(out, 1000); // 3X performance by having this !!
  DataOutputStream dout = new DataOutputStream(out);

  Random r = new Random();
  long start = System.currentTimeMillis();
  for (int i = 0; i < (1000 * 1000); i++) {
    dout.writeFloat(r.nextFloat());
  }
  dout.flush();
  fout.close();
  double took = .001 * (System.currentTimeMillis() - start);
  File f = new File(filenameOut);
  long len = f.length();
  double ratio = 4 * 1000.0 * 1000.0 / len;
  System.out.println(" deflateRandom took = " + took + " sec; compress = " + ratio);
}
 
Example #2
Source Project: netcdf-java   Author: Unidata   File: TimeCompression.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static public void main2(String[] args) throws IOException {
  String filenameIn = "C:/temp/tempFile2";
  String filenameOut = "C:/temp/tempFile2.compress2";

  FileInputStream fin = new FileInputStream(filenameIn);
  FileOutputStream fout = new FileOutputStream(filenameOut);
  BufferedOutputStream bout = new BufferedOutputStream(fout, 10 * 1000);
  DeflaterOutputStream out = new DeflaterOutputStream(fout);

  long start = System.currentTimeMillis();
  IO.copyB(fin, out, 10 * 1000);
  double took = .001 * (System.currentTimeMillis() - start);
  System.out.println(" that took = " + took + "sec");

  fin.close();
  fout.close();
}
 
Example #3
Source Project: gcs   Author: richardwilkes   File: PRStream.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Sets the data associated with the stream, either compressed or uncompressed. Note that the
 * data will never be compressed if Document.compress is set to false.
 *
 * @param data raw data, decrypted and uncompressed.
 * @param compress true if you want the stream to be compressed.
 * @param compressionLevel a value between -1 and 9 (ignored if compress == false)
 * @since iText 2.1.3
 */
public void setData(byte[] data, boolean compress, int compressionLevel) {
	remove(PdfName.FILTER);
	offset = -1;
	if (Document.compress && compress) {
		try {
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			Deflater deflater = new Deflater(compressionLevel);
			DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
			zip.write(data);
			zip.close();
			deflater.end();
			bytes = stream.toByteArray();
			this.compressionLevel = compressionLevel;
		} catch (IOException ioe) {
			throw new ExceptionConverter(ioe);
		}
		put(PdfName.FILTER, PdfName.FLATEDECODE);
	} else {
		bytes = data;
	}
	setLength(bytes.length);
}
 
Example #4
Source Project: jlogstash-input-plugin   Author: DTStack   File: CompressedBatchEncoder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected ByteBuf getPayload(ChannelHandlerContext ctx, Batch batch) throws IOException {
    ByteBuf payload = super.getPayload(ctx, batch);

    Deflater deflater = new Deflater();
    ByteBufOutputStream output = new ByteBufOutputStream(ctx.alloc().buffer());
    DeflaterOutputStream outputDeflater = new DeflaterOutputStream(output, deflater);

    byte[] chunk = new byte[payload.readableBytes()];
    payload.readBytes(chunk);
    outputDeflater.write(chunk);
    outputDeflater.close();

    ByteBuf content = ctx.alloc().buffer();
    content.writeByte(batch.getProtocol());
    content.writeByte('C');


    content.writeInt(output.writtenBytes());
    content.writeBytes(output.buffer());

    return content;
}
 
Example #5
Source Project: dubbox   Author: remoting   File: Bytes.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * zip.
 * 
 * @param bytes source.
 * @return compressed byte array.
 * @throws IOException.
 */
public static byte[] zip(byte[] bytes) throws IOException
{
	UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
	OutputStream os = new DeflaterOutputStream(bos);
	try
	{
		os.write(bytes);
	}
	finally
	{
		os.close();
		bos.close();
	}
	return bos.toByteArray();
}
 
Example #6
Source Project: rocketmq-read   Author: chenhaiyangs   File: UtilAll.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 压缩
 * @param src ;
 * @param level ;
 * @return ;
 * @throws IOException ;
 */
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example #7
Source Project: barchomat   Author: sir-barchable   File: MessageOutputStream.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void writeZipString(String s) throws IOException {
    byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream zipStream = new DeflaterOutputStream(out);
    zipStream.write(bytes);
    zipStream.close();
    byte[] zipped = out.toByteArray();
    // write total length
    writeInt(zipped.length + 5);
    // write unzipped length in little endian order
    writeInt(Bits.swapEndian(bytes.length));
    // write zipped utf-8 encoded string
    write(zipped);
    // null terminator
    write(0);
}
 
Example #8
Source Project: codewind-eclipse   Author: eclipse   File: FileChangeEventBatchUtil.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private final static byte[] compressString(String str) {

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_SPEED));

	int uncompressedSize;
	try {
		byte[] strBytes = str.getBytes();
		dos.write(strBytes);
		dos.close();
		baos.close();
	} catch (IOException e) {
		log.logSevere("Unable to compress string contents", e, null);
		throw new RuntimeException(e);
	}

	byte[] result = baos.toByteArray();

	return result;
}
 
Example #9
Source Project: apm-agent-java   Author: elastic   File: AbstractIntakeApiHandler.java    License: Apache License 2.0 6 votes vote down vote up
protected HttpURLConnection startRequest(String endpoint) throws IOException {
    final HttpURLConnection connection = apmServerClient.startRequest(endpoint);
    if (logger.isDebugEnabled()) {
        logger.debug("Starting new request to {}", connection.getURL());
    }
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(DslJsonSerializer.BUFFER_SIZE);
    connection.setRequestProperty("Content-Encoding", "deflate");
    connection.setRequestProperty("Content-Type", "application/x-ndjson");
    connection.setUseCaches(false);
    connection.connect();
    os = new DeflaterOutputStream(connection.getOutputStream(), deflater);
    os.write(metaData);
    return connection;
}
 
Example #10
Source Project: rocketmq   Author: apache   File: UtilAll.java    License: Apache License 2.0 6 votes vote down vote up
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example #11
Source Project: dubbox   Author: hutai123   File: BenchmarkRunner.java    License: Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example #12
Source Project: rogue-cloud   Author: codewind-resources   File: CompressionUtils.java    License: Apache License 2.0 6 votes vote down vote up
private final static byte[] compressString(String str) {

		// If compression is disabled, then just return a byte array of a UTF-8 string
		if(!RCRuntime.ENABLE_DEFLATE_COMPRESSION) {
			return str.getBytes();
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_SPEED) );
		
		try {
			dos.write(str.getBytes());
			dos.close();
			baos.close();
		} catch (IOException e) {
			e.printStackTrace();
			log.severe("Exception on compress", e, null);
			throw new RuntimeException(e);
		}
		
		return baos.toByteArray();
	}
 
Example #13
Source Project: DDMQ   Author: didi   File: UtilAll.java    License: Apache License 2.0 6 votes vote down vote up
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example #14
Source Project: dubbox-hystrix   Author: yunhaibin   File: Bytes.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * zip.
 * 
 * @param bytes source.
 * @return compressed byte array.
 * @throws IOException.
 */
public static byte[] zip(byte[] bytes) throws IOException
{
	UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
	OutputStream os = new DeflaterOutputStream(bos);
	try
	{
		os.write(bytes);
	}
	finally
	{
		os.close();
		bos.close();
	}
	return bos.toByteArray();
}
 
Example #15
Source Project: lams   Author: lamsfoundation   File: HTTPRedirectDeflateEncoder.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * DEFLATE (RFC1951) compresses the given SAML message.
 * 
 * @param message SAML message
 * 
 * @return DEFLATE compressed message
 * 
 * @throws MessageEncodingException thrown if there is a problem compressing the message
 */
protected String deflateAndBase64Encode(SAMLObject message) throws MessageEncodingException {
    log.debug("Deflating and Base64 encoding SAML message");
    try {
        String messageStr = XMLHelper.nodeToString(marshallMessage(message));

        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
        deflaterStream.write(messageStr.getBytes("UTF-8"));
        deflaterStream.finish();

        return Base64.encodeBytes(bytesOut.toByteArray(), Base64.DONT_BREAK_LINES);
    } catch (IOException e) {
        throw new MessageEncodingException("Unable to DEFLATE and Base64 encode SAML message", e);
    }
}
 
Example #16
Source Project: lams   Author: lamsfoundation   File: EscherMetafileBlip.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setPictureData(byte[] pictureData) {
	super.setPictureData(pictureData);
    setUncompressedSize(pictureData.length);

    // info of chicago project:
    // "... LZ compression algorithm in the format used by GNU Zip deflate/inflate with a 32k window ..."
    // not sure what to do, when lookup tables exceed 32k ...

    try {
     ByteArrayOutputStream bos = new ByteArrayOutputStream();
     DeflaterOutputStream dos = new DeflaterOutputStream(bos);
     dos.write(pictureData);
     dos.close();
     raw_pictureData = bos.toByteArray();
    } catch (IOException e) {
    	throw new RuntimeException("Can't compress metafile picture data", e);
    }
    
    setCompressedSize(raw_pictureData.length);
    setCompressed(true);
}
 
Example #17
Source Project: gcs   Author: richardwilkes   File: FlateFilter.java    License: Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
        throws IOException
{
    int compressionLevel = getCompressionLevel();
    Deflater deflater = new Deflater(compressionLevel);
    DeflaterOutputStream out = new DeflaterOutputStream(encoded, deflater);
    int amountRead;
    int mayRead = input.available();
    if (mayRead > 0)
    {
        byte[] buffer = new byte[Math.min(mayRead,BUFFER_SIZE)];
        while ((amountRead = input.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1)
        {
            out.write(buffer, 0, amountRead);
        }
    }
    out.close();
    encoded.flush();
    deflater.end();
}
 
Example #18
Source Project: database   Author: blazegraph   File: Stream.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Apply the configured {@link CompressionEnum} for the {@link Stream} to
 * the caller's {@link OutputStream}.
 * 
 * @param out
 *            The {@link OutputStream}.
 * 
 * @return The wrapped {@link OutputStream}.
 * 
 * @throws IOException
 */
protected OutputStream wrapOutputStream(final OutputStream out) {

    final CompressionEnum compressionType = metadata
            .getStreamCompressionType();

    switch (compressionType) {
    case None:
        return out;
    case Zip:
        return new DeflaterOutputStream(out);
    default:
        throw new UnsupportedOperationException("CompressionEnum="
                + compressionType);
    }

}
 
Example #19
Source Project: Elasticsearch   Author: baidu   File: DeflateCompressor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {
        private boolean closed = false;

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed == false) {
                    // important to release native memory
                    deflater.end();
                    closed = true;
                }
            }
        }
    };
}
 
Example #20
Source Project: Decoder-Improved   Author: nccgroup   File: ZlibEncoder.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] modifyBytes(byte[] input) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
        DeflaterOutputStream deflater = new DeflaterOutputStream(bos);
        deflater.write(input, 0, input.length);
        deflater.finish();
        deflater.flush();
        bos.flush();
        deflater.close();
        bos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        return new byte[]{};
    }
}
 
Example #21
Source Project: dubbox   Author: remoting   File: BenchmarkRunner.java    License: Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example #22
Source Project: dubbox   Author: learningtcc   File: BenchmarkRunner.java    License: Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example #23
Source Project: dubbox   Author: learningtcc   File: Bytes.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * zip.
 * 
 * @param bytes source.
 * @return compressed byte array.
 * @throws IOException.
 */
public static byte[] zip(byte[] bytes) throws IOException
{
	UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
	OutputStream os = new DeflaterOutputStream(bos);
	try
	{
		os.write(bytes);
	}
	finally
	{
		os.close();
		bos.close();
	}
	return bos.toByteArray();
}
 
Example #24
Source Project: gama   Author: gama-platform   File: DocumentationNode.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static byte[] compress(final String text) throws IOException {
	final ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try (final OutputStream out = new DeflaterOutputStream(baos, true);) {
		out.write(text.getBytes("ISO-8859-1"));
	}
	return baos.toByteArray();
}
 
Example #25
Source Project: netcdf-java   Author: Unidata   File: NcStreamIosp.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String showDeflate() {
  if (!(what instanceof NcStreamIosp.DataStorage))
    return "Must select a NcStreamIosp.DataStorage object";

  Formatter f = new Formatter();
  try {
    NcStreamIosp.DataStorage dataStorage = (NcStreamIosp.DataStorage) what;

    raf.seek(dataStorage.filePos);
    byte[] data = new byte[dataStorage.size];
    raf.readFully(data);

    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DeflaterOutputStream dout = new DeflaterOutputStream(bout);
    IO.copy(bin, dout);
    dout.close();
    int deflatedSize = bout.size();
    float ratio = ((float) data.length) / deflatedSize;
    f.format("Original size = %d bytes, deflated = %d bytes ratio = %f %n", data.length, deflatedSize, ratio);
    return f.toString();

  } catch (IOException e) {
    e.printStackTrace();
    return e.getMessage();
  }
}
 
Example #26
Source Project: openjdk-8   Author: bpupadhyaya   File: PNGImageWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
private byte[] deflate(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    dos.write(b);
    dos.close();
    return baos.toByteArray();
}
 
Example #27
Source Project: dubbo-2.6.5   Author: tianheframe   File: Bytes.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * zip.
 *
 * @param bytes source.
 * @return compressed byte array.
 * @throws IOException
 */
public static byte[] zip(byte[] bytes) throws IOException {
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
    OutputStream os = new DeflaterOutputStream(bos);
    try {
        os.write(bytes);
    } finally {
        os.close();
        bos.close();
    }
    return bos.toByteArray();
}
 
Example #28
Source Project: hottub   Author: dsrg-uoft   File: AstSerializer.java    License: GNU General Public License v2.0 5 votes vote down vote up
static byte[] serialize(final FunctionNode fn) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Deflater deflater = new Deflater(COMPRESSION_LEVEL);
    try (final ObjectOutputStream oout = new ObjectOutputStream(new DeflaterOutputStream(out, deflater))) {
        oout.writeObject(fn);
    } catch (final IOException e) {
        throw new AssertionError("Unexpected exception serializing function", e);
    } finally {
        deflater.end();
    }
    return out.toByteArray();
}
 
Example #29
Source Project: armeria   Author: line   File: HttpEncoders.java    License: Apache License 2.0 5 votes vote down vote up
static DeflaterOutputStream getEncodingOutputStream(HttpEncodingType encodingType, OutputStream out) {
    switch (encodingType) {
        case GZIP:
            try {
                return new GZIPOutputStream(out, true);
            } catch (IOException e) {
                throw new IllegalStateException(
                        "Error writing gzip header. This should not happen with byte arrays.", e);
            }
        case DEFLATE:
            return new DeflaterOutputStream(out, true);
        default:
            throw new IllegalArgumentException("Unexpected zlib type, this is a programming bug.");
    }
}
 
Example #30
Source Project: QQRobot   Author: uebian   File: ZLibUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
/** 
 * 压缩 
 *  
 * @param data 
 *            待压缩数据 
 *  
 * @param os 
 *            输出流 
 */  
public static void compress(byte[] data, OutputStream os) {  
    DeflaterOutputStream dos = new DeflaterOutputStream(os);  

    try {  
        dos.write(data, 0, data.length);  

        dos.finish();  

        dos.flush();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}