java.util.zip.DeflaterOutputStream Java Examples

The following examples show how to use java.util.zip.DeflaterOutputStream. 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 File: Bytes.java    From dubbox with 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 #2
Source File: Bytes.java    From dubbox-hystrix with 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 #3
Source File: HTTPRedirectDeflateEncoder.java    From lams with 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 #4
Source File: MessageOutputStream.java    From barchomat with 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 #5
Source File: EscherMetafileBlip.java    From lams with 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 #6
Source File: FlateFilter.java    From gcs with 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 #7
Source File: UtilAll.java    From DDMQ with 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 #8
Source File: UtilAll.java    From rocketmq-read with 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 #9
Source File: Stream.java    From database with 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 #10
Source File: UtilAll.java    From rocketmq with 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 File: FileChangeEventBatchUtil.java    From codewind-eclipse with 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 #12
Source File: AbstractIntakeApiHandler.java    From apm-agent-java with 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 #13
Source File: BenchmarkRunner.java    From dubbox with 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 #14
Source File: CompressionUtils.java    From rogue-cloud with 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 #15
Source File: Bytes.java    From dubbox with 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 #16
Source File: ZlibEncoder.java    From Decoder-Improved with 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 #17
Source File: CompressedBatchEncoder.java    From jlogstash-input-plugin with 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 #18
Source File: BenchmarkRunner.java    From dubbox with 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 #19
Source File: PRStream.java    From gcs with 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 #20
Source File: DeflateCompressor.java    From Elasticsearch with 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 #21
Source File: BenchmarkRunner.java    From dubbox with 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 File: TimeCompression.java    From netcdf-java with 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 #23
Source File: TimeCompression.java    From netcdf-java with 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 #24
Source File: MCAChunkData.java    From mcaselector with MIT License 5 votes vote down vote up
public int saveData(RandomAccessFile raf) throws Exception {
	DataOutputStream nbtOut;

	ByteArrayOutputStream baos;

	switch (compressionType) {
	case GZIP:
		nbtOut = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(baos = new ByteArrayOutputStream()), sectors * MCAFile.SECTION_SIZE));
		break;
	case ZLIB:
		nbtOut = new DataOutputStream(new BufferedOutputStream(new DeflaterOutputStream(baos = new ByteArrayOutputStream()), sectors * MCAFile.SECTION_SIZE));
		break;
	default:
		return 0;
	}

	new NBTSerializer(false).toStream(new NamedTag(null, data), nbtOut);

	nbtOut.close();

	byte[] rawData = baos.toByteArray();

	raf.writeInt(rawData.length + 1); // length includes the compression type byte
	raf.writeByte(compressionType.getByte());
	raf.write(rawData);

	return rawData.length + 5;
}
 
Example #25
Source File: ByteTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static byte[] deflate(byte[] bytes) {
    try {
        ByteArrayOutputStream a = new ByteArrayOutputStream();
        try ( DeflaterOutputStream out = new DeflaterOutputStream(a)) {
            out.write(bytes);
            out.flush();
        }
        return a.toByteArray();
    } catch (Exception e) {
        return null;
    }
}
 
Example #26
Source File: TestRawTripPayload.java    From hudi with Apache License 2.0 5 votes vote down vote up
private byte[] compressData(String jsonData) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
  try {
    dos.write(jsonData.getBytes());
  } finally {
    dos.flush();
    dos.close();
  }
  return baos.toByteArray();
}
 
Example #27
Source File: NcStreamIosp.java    From netcdf-java with 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 #28
Source File: TextFileBackuper.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Nonnull
private byte[] prepareContent(@Nonnull final String content) {
  final ByteArrayOutputStream bao = new ByteArrayOutputStream(content.length() << 1);
  final byte[] textAsBytes = content.getBytes(StandardCharsets.UTF_8);
  final CRC32 crc32 = new CRC32();
  crc32.update(textAsBytes);
  final long crc32value = crc32.getValue();
  final long timestamp = System.currentTimeMillis();

  try {
    writeLong(timestamp, bao);
    writeLong(crc32value, bao);
    final ByteArrayOutputStream packedDataBuffer = new ByteArrayOutputStream(content.length());
    final DeflaterOutputStream zos = new DeflaterOutputStream(packedDataBuffer, new Deflater(2));
    IOUtils.write(textAsBytes, zos);
    zos.flush();
    zos.finish();
    final byte[] packedContent = packedDataBuffer.toByteArray();
    writeLong(textAsBytes.length, bao);
    writeLong(packedContent.length, bao);
    IOUtils.write(packedContent, bao);
    bao.flush();
    return bao.toByteArray();
  } catch (IOException ex) {
    LOGGER.error("Unexpected situation, can't pack array");
    return new byte[0];
  }
}
 
Example #29
Source File: AstSerializer.java    From openjdk-jdk8u with 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 #30
Source File: PNGImageWriter.java    From openjdk-jdk8u with 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();
}