Java Code Examples for java.util.zip.DeflaterOutputStream#write()

The following examples show how to use java.util.zip.DeflaterOutputStream#write() . 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: UtilAll.java    From rocketmq_trans_message 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 2
Source File: PRStream.java    From itext2 with GNU Lesser General Public License v3.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);
    this.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 3
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 4
Source File: MixAll.java    From iot-mqtt 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);
    Deflater deflater = new Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream,deflater);
    try{
        deflaterOutputStream.write(src);
        deflaterOutputStream.flush();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    }catch (IOException e){
        deflater.end();
        throw e;
    }finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }
        deflater.end();
    }
    return result;
}
 
Example 5
Source File: DeflateCompressor.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
public static byte[] compress(byte[] input) throws IOException {
    // Destination where compressed data will be stored.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Create a compressor.
    Deflater deflater = createDeflater();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater);

    // Compress the data.
    //
    // Some other implementations such as Jetty and Tyrus use
    // Deflater.deflate(byte[], int, int, int) with Deflate.SYNC_FLUSH,
    // but this implementation does not do it intentionally because the
    // method and the constant value are not available before Java 7.
    dos.write(input, 0, input.length);
    dos.close();

    // Release the resources held by the compressor.
    deflater.end();

    // Retrieve the compressed data.
    return baos.toByteArray();
}
 
Example 6
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 7
Source File: PRStream.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new PDF stream object that will replace a stream
 * in a existing PDF file.
 * @param	reader	the reader that holds the existing PDF
 * @param	conts	the new content
 * @param	compressionLevel	the compression level for the content
 * @since	2.1.3 (replacing the existing constructor without param compressionLevel)
 */
public PRStream(PdfReader reader, byte[] conts, int compressionLevel) {
    this.reader = reader;
    this.offset = -1;
    if (Document.compress) {
        try {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Deflater deflater = new Deflater(compressionLevel);
            DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
            zip.write(conts);
            zip.close();
            deflater.end();
            bytes = stream.toByteArray();
        }
        catch (IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
        put(PdfName.FILTER, PdfName.FLATEDECODE);
    }
    else
        bytes = conts;
    setLength(bytes.length);
}
 
Example 8
Source File: StandardEncrypter.java    From fastods with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Deflate as LibreOffice.
 *
 * @param data the data to DEFLATE
 * @return the DEFLATEd data
 * @throws IOException if a I/O error occurs
 */
public byte[] compress(final byte[] data) throws IOException {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
    final DeflaterOutputStream dos = new DeflaterOutputStream(os, deflater);

    final InputStream is = new ByteArrayInputStream(data);
    final byte[] buffer = new byte[BUFFER_SIZE];
    int count = is.read(buffer);
    while (count != -1) {
        dos.write(buffer, 0, count);
        count = is.read(buffer);
    }
    dos.close();
    return os.toByteArray();
}
 
Example 9
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 10
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 11
Source File: PNGImageWriter.java    From jdk8u-jdk 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();
}
 
Example 12
Source File: TestZlibCompressorDecompressor.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void compressDecompressLoop(int rawDataSize) throws IOException {
  byte[] rawData = null;
  rawData = generate(rawDataSize);
  ByteArrayOutputStream baos = new ByteArrayOutputStream(rawDataSize+12);
  DeflaterOutputStream dos = new DeflaterOutputStream(baos);
  dos.write(rawData);
  dos.flush();
  dos.close();
  byte[] compressedResult = baos.toByteArray();
  int compressedSize = compressedResult.length;
  ZlibDirectDecompressor decompressor = new ZlibDirectDecompressor();
 
  ByteBuffer inBuf = ByteBuffer.allocateDirect(compressedSize);
  ByteBuffer outBuf = ByteBuffer.allocateDirect(rawDataSize);

  inBuf.put(compressedResult, 0, compressedSize);
  inBuf.flip();    

  ByteBuffer expected = ByteBuffer.wrap(rawData);
  
  outBuf.clear();
  while(!decompressor.finished()) {
    decompressor.decompress(inBuf, outBuf);
    if (outBuf.remaining() == 0) {
      outBuf.flip();
      while (outBuf.remaining() > 0) {        
        assertEquals(expected.get(), outBuf.get());
      }
      outBuf.clear();
    }
  }
  outBuf.flip();
  while (outBuf.remaining() > 0) {        
    assertEquals(expected.get(), outBuf.get());
  }
  outBuf.clear();
  
  assertEquals(0, expected.remaining());
}
 
Example 13
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 14
Source File: DigestUtils.java    From opencron with Apache License 2.0 5 votes vote down vote up
public static String compressData(String data, String charset) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream zos = new DeflaterOutputStream(bos);
        zos.write(data.getBytes());
        zos.close();
        return new String(getenBASE64inCodec(bos.toByteArray(), charset));
    } catch (Exception ex) {
        ex.printStackTrace();
        return "ZIP_ERR";
    }
}
 
Example 15
Source File: ZLibUtil.java    From MonitorClient with Apache License 2.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(); 
        dos.close();
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}
 
Example 16
Source File: PNGImageWriter.java    From openjdk-8-source 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();
}
 
Example 17
Source File: ZLibUtils.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩
 * 
 * @param data 待压缩的数据
 * @param os 输出流
 * @throws IOException
 */
public static void compress(byte[] data, OutputStream os) throws IOException
{
	DeflaterOutputStream dos = new DeflaterOutputStream(os);

	dos.write(data, 0, data.length);
	dos.finish();
	dos.flush();
}
 
Example 18
Source File: PNGImageWriter.java    From openjdk-jdk9 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();
}
 
Example 19
Source File: PNGImageWriter.java    From jdk8u-jdk 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();
}
 
Example 20
Source File: ReadParams.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception{
    /* initialise stuff */
    File fn = new File("x.ReadBounds");
    FileOutputStream fout = new FileOutputStream(fn);
    for (int i = 0; i < 32; i++) {
        fout.write(i);
    }
    fout.close();

    byte b[] = new byte[64];
    for(int i = 0; i < 64; i++) {
        b[i] = 1;
    }

    /* test all input streams */
    FileInputStream fis = new FileInputStream(fn);
    doTest(fis);
    doTest1(fis);
    fis.close();

    BufferedInputStream bis =
        new BufferedInputStream(new MyInputStream(1024));
    doTest(bis);
    doTest1(bis);
    bis.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    doTest(bais);
    doTest1(bais);
    bais.close();

    FileOutputStream fos = new FileOutputStream(fn);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeInt(12345);
    oos.writeObject("Today");
    oos.writeObject(new Integer(32));
    oos.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fn));
    doTest(ois);
    doTest1(ois);
    ois.close();

    DataInputStream dis = new DataInputStream(new MyInputStream(1024));
    doTest(dis);
    doTest1(dis);
    dis.close();

    LineNumberInputStream lis =
        new LineNumberInputStream(new MyInputStream(1024));
    doTest(lis);
    doTest1(lis);
    lis.close();

    PipedOutputStream pos = new PipedOutputStream();
    PipedInputStream pis = new PipedInputStream();
    pos.connect(pis);
    pos.write(b, 0, 64);
    doTest(pis);
    doTest1(pis);
    pis.close();

    PushbackInputStream pbis =
        new PushbackInputStream(new MyInputStream(1024));
    doTest(pbis);
    doTest1(pbis);
    pbis.close();

    StringBufferInputStream sbis =
        new StringBufferInputStream(new String(b));
    doTest(sbis);
    doTest1(sbis);
    sbis.close();

    SequenceInputStream sis =
        new SequenceInputStream(new MyInputStream(1024),
                                new MyInputStream(1024));
    doTest(sis);
    doTest1(sis);
    sis.close();

    ZipInputStream zis = new ZipInputStream(new FileInputStream(fn));
    doTest(zis);
    doTest1(zis);
    zis.close();

    byte[] data = new byte[256];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(bos);
    dos.write(data, 0, data.length);
    dos.close();
    InflaterInputStream ifs = new InflaterInputStream
        (new ByteArrayInputStream(bos.toByteArray()));
    doTest(ifs);
    doTest1(ifs);
    ifs.close();

    /* cleanup */
    fn.delete();
}