下面列出了org.apache.commons.io.output.CloseShieldOutputStream#java.util.zip.DeflaterOutputStream 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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();
}
/**
* 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;
}
/**
* 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();
}
/**
* 压缩
* @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;
}
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;
}
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;
}
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;
}
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;
}
}
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();
}
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;
}
/**
* 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();
}
/**
* 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);
}
}
@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);
}
@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();
}
/**
* 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);
}
}
@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;
}
}
}
};
}
@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[]{};
}
}
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;
}
}
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;
}
}
/**
* 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();
}
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();
}
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();
}
}
private byte[] deflate(byte[] b) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos);
dos.write(b);
dos.close();
return baos.toByteArray();
}
/**
* 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();
}
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();
}
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.");
}
}
/**
* 压缩
*
* @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();
}
}