类java.util.zip.DeflaterOutputStream源码实例Demo

下面列出了怎么用java.util.zip.DeflaterOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: netcdf-java   文件: TimeCompression.java
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);
}
 
源代码2 项目: netcdf-java   文件: TimeCompression.java
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();
}
 
源代码3 项目: gcs   文件: PRStream.java
/**
 * 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);
}
 
@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;
}
 
源代码5 项目: dubbox   文件: Bytes.java
/**
 * 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();
}
 
源代码6 项目: rocketmq-read   文件: UtilAll.java
/**
 * 压缩
 * @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;
}
 
源代码7 项目: barchomat   文件: MessageOutputStream.java
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);
}
 
@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;
}
 
源代码9 项目: apm-agent-java   文件: AbstractIntakeApiHandler.java
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;
}
 
源代码10 项目: rocketmq   文件: UtilAll.java
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;
}
 
源代码11 项目: dubbox   文件: BenchmarkRunner.java
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;
	}
}
 
源代码12 项目: rogue-cloud   文件: CompressionUtils.java
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();
	}
 
源代码13 项目: DDMQ   文件: UtilAll.java
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;
}
 
源代码14 项目: dubbox-hystrix   文件: Bytes.java
/**
 * 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();
}
 
源代码15 项目: lams   文件: HTTPRedirectDeflateEncoder.java
/**
 * 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);
    }
}
 
源代码16 项目: lams   文件: EscherMetafileBlip.java
@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);
}
 
源代码17 项目: gcs   文件: FlateFilter.java
@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();
}
 
源代码18 项目: database   文件: Stream.java
/**
 * 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);
    }

}
 
源代码19 项目: Elasticsearch   文件: DeflateCompressor.java
@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;
                }
            }
        }
    };
}
 
源代码20 项目: Decoder-Improved   文件: ZlibEncoder.java
@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[]{};
    }
}
 
源代码21 项目: dubbox   文件: BenchmarkRunner.java
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;
	}
}
 
源代码22 项目: dubbox   文件: BenchmarkRunner.java
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;
	}
}
 
源代码23 项目: dubbox   文件: Bytes.java
/**
 * 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();
}
 
源代码24 项目: gama   文件: DocumentationNode.java
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();
}
 
源代码25 项目: netcdf-java   文件: NcStreamIosp.java
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();
  }
}
 
源代码26 项目: openjdk-8   文件: PNGImageWriter.java
private byte[] deflate(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    dos.write(b);
    dos.close();
    return baos.toByteArray();
}
 
源代码27 项目: dubbo-2.6.5   文件: Bytes.java
/**
 * 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();
}
 
源代码28 项目: hottub   文件: AstSerializer.java
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();
}
 
源代码29 项目: armeria   文件: HttpEncoders.java
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.");
    }
}
 
源代码30 项目: QQRobot   文件: ZLibUtils.java
/** 
 * 压缩 
 *  
 * @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();  
    }  
}
 
 类所在包
 同包方法