Example usage for java.util.zip Deflater needsInput

List of usage examples for java.util.zip Deflater needsInput

Introduction

In this page you can find the example usage for java.util.zip Deflater needsInput.

Prototype

public boolean needsInput() 

Source Link

Document

Returns true if no data remains in the input buffer.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);// w w  w.  j a va  2 s .c  o m

    while (!def.needsInput()) {
        int numCompressedBytes = def.deflate(output, 0, output.length);
        if (numCompressedBytes > 0) {
            fout.write(output, 0, numCompressedBytes);
        }
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
}

From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {//  ww  w .j  a v a2 s . com
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Deflater zlib = new Deflater();
    zlib.setInput(input);
    zlib.finish();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        bytesRead = zlib.deflate(temp);

        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }
        output.write(temp, 0, bytesRead);
    }
    zlib.end();

    return output.toByteArray();
}

From source file:org.phpmaven.phar.PharJavaPackager.java

private void packFile(final ByteArrayOutputStream fileEntriesBaos,
        final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath)
        throws IOException {
    if (DEBUG) {/*w ww. jav  a  2  s  .co m*/
        System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes.");
    }

    final byte[] fileBytes = filePath.getBytes("UTF-8");
    writeIntLE(fileEntriesBaos, fileBytes.length);
    fileEntriesBaos.write(fileBytes);
    // TODO Complain with files larger than 4 bytes file length
    writeIntLE(fileEntriesBaos, (int) fileToPack.length());
    writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000));

    final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack);
    if (DEBUG) {
        System.out.println("read " + uncompressed.length + " bytes from file.");
    }
    final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
    //        final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream);
    //        gzipStream.write(uncompressed);
    //        gzipStream.flush();
    final CRC32 checksum = new CRC32();
    checksum.update(uncompressed);
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(uncompressed);
    deflater.finish();
    final byte[] buf = new byte[Short.MAX_VALUE];
    while (!deflater.needsInput()) {
        final int bytesRead = deflater.deflate(buf);
        compressedStream.write(buf, 0, bytesRead);
    }

    final byte[] compressed = compressedStream.toByteArray();
    if (DEBUG) {
        System.out.println("compressed to " + compressed.length + " bytes.");
    }

    //        final Inflater decompresser = new Inflater();
    //        decompresser.setInput(compressed);
    //        byte[] result = new byte[5000];
    //        try {
    //            int resultLength = decompresser.inflate(result);
    //            final String str = new String(result, 0, resultLength);
    //            int i = 42;
    //        } catch (DataFormatException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        decompresser.end();

    compressedFilesBaos.write(compressed);
    writeIntLE(fileEntriesBaos, compressed.length);

    writeIntLE(fileEntriesBaos, checksum.getValue());

    // bits: 0x00001000, gzip
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0x10);
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0);

    // 0 bytes manifest
    writeIntLE(fileEntriesBaos, 0);
}