Java Code Examples for java.util.zip.Deflater#needsInput()

The following examples show how to use java.util.zip.Deflater#needsInput() . 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: DeflaterOutputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() {
    // setting up a deflater to be used
    byte byteArray[] = { 1, 3, 4, 7, 8 };
    int x = 0;
    Deflater deflate = new Deflater(1);
    deflate.setInput(byteArray);
    while (!(deflate.needsInput())) {
        x += deflate.deflate(outputBuf, x, outputBuf.length - x);
    }
    deflate.finish();
    while (!(deflate.finished())) {
        x = x + deflate.deflate(outputBuf, x, outputBuf.length - x);
    }
    deflate.end();
}
 
Example 2
Source File: DeflateCompressor.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public int compress(byte[] input, int inputOffset, int inputLength, ICompressor.WrappedArray output, int outputOffset)
{
    Deflater def = deflater.get();
    def.reset();
    def.setInput(input, inputOffset, inputLength);
    def.finish();
    if (def.needsInput())
        return 0;

    int offs = outputOffset;
    while (true)
    {
        offs += def.deflate(output.buffer, offs, output.buffer.length - offs);
        if (def.finished())
        {
            return offs - outputOffset;
        }
        else
        {
            // We're not done, output was too small. Increase it and continue
            byte[] newBuffer = new byte[(output.buffer.length*4)/3 + 1];
            System.arraycopy(output.buffer, 0, newBuffer, 0, offs);
            output.buffer = newBuffer;
        }
    }
}
 
Example 3
Source File: ByteBuffer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Write the entire content into the given file using Flate compression (see
 * RFC1951) then return the number of bytes written.
 */
public long dumpFlate(RandomAccessFile os) throws IOException {
    Deflater zip = new Deflater(Deflater.BEST_COMPRESSION);
    byte[] output = new byte[8192];
    Iterator<byte[]> it = list.iterator(); // when null, that means we have
                                          // told the Deflater that no
                                          // more input would be coming
    long ans = 0; // the number of bytes written out so far
    while (true) {
        if (it != null && zip.needsInput() && it.hasNext()) {
            byte[] in = it.next();
            if (in == list.getLast()) {
                zip.setInput(in, 0, n);
                it = null;
                zip.finish();
            } else {
                zip.setInput(in, 0, SIZE);
            }
        }
        if (it == null && zip.finished())
            break;
        int count = zip.deflate(output);
        if (count > 0) {
            ans = ans + count;
            if (ans < 0)
                throw new IOException("Data too large to be written to the output file.");
            os.write(output, 0, count);
        }
    }
    return ans;
}
 
Example 4
Source File: CompressionCodecZLib.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PulsarByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    Deflater deflater = this.deflater.get();
    deflater.reset();
    deflater.setInput(array, offset, length);
    while (!deflater.needsInput()) {
        deflate(deflater, compressed);
    }

    return compressed;
}
 
Example 5
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.zip.Deflater#needsInput()
 */
public void test_needsInput() {
    Deflater defl = new Deflater();
    assertTrue(
            "needsInput give the wrong boolean value as a result of no input buffer",
            defl.needsInput());
    byte byteArray[] = { 1, 2, 3 };
    defl.setInput(byteArray);
    assertFalse(
            "needsInput give wrong boolean value as a result of a full input buffer",
            defl.needsInput());
    byte[] outPutBuf = new byte[50];
    while (!defl.needsInput()) {
        defl.deflate(outPutBuf);
    }
    byte emptyByteArray[] = new byte[0];
    defl.setInput(emptyByteArray);
    assertTrue(
            "needsInput give wrong boolean value as a result of an empty input buffer",
            defl.needsInput());
    defl.setInput(byteArray);
    defl.finish();
    while (!defl.finished()) {
        defl.deflate(outPutBuf);
    }
    // needsInput should NOT return true after finish() has been
    // called.
    if (System.getProperty("java.vendor").startsWith("IBM")) {
        assertFalse(
                "needsInput gave wrong boolean value as a result of finish() being called",
                defl.needsInput());
    }
    defl.end();
}
 
Example 6
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.zip.Deflater#Deflater()
 */
public void test_Constructor() throws Exception {
    byte[] byteArray = new byte[100];
    InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt");
    inFile.read(byteArray);
    inFile.close();

    Deflater defl = new Deflater();
    byte[] outPutBuf = new byte[500];
    defl.setInput(byteArray);
    while (!defl.needsInput()) {
        defl.deflate(outPutBuf);
    }
    defl.finish();
    while (!defl.finished()) {
        defl.deflate(outPutBuf);
    }
    int totalOut = defl.getTotalOut();
    defl.end();

    // creating a Deflater using the DEFAULT_COMPRESSION as the int
    MyDeflater mdefl = new MyDeflater();
    mdefl.end();

    mdefl = new MyDeflater(mdefl.getDefCompression());
    outPutBuf = new byte[500];
    mdefl.setInput(byteArray);
    while (!mdefl.needsInput()) {
        mdefl.deflate(outPutBuf);
    }
    mdefl.finish();
    while (!mdefl.finished()) {
        mdefl.deflate(outPutBuf);
    }
    assertEquals(totalOut, mdefl.getTotalOut());
    mdefl.end();
}
 
Example 7
Source File: OldAndroidDeflateTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void compress(Deflater deflater, byte[] inBuf, byte[] outBuf) {
    int inCount = inBuf.length;        // use all
    int inPosn;
    int outPosn;

    inPosn = outPosn = 0;

    //System.out.println("### starting compress");

    while (!deflater.finished()) {
        int want = -1, got;

        // only read if the input buffer is empty
        if (deflater.needsInput() && inCount != 0) {
            want = (inCount < LOCAL_BUF_SIZE) ? inCount : LOCAL_BUF_SIZE;

            deflater.setInput(inBuf, inPosn, want);

            inCount -= want;
            inPosn += want;
            if (inCount == 0) {
                deflater.finish();
            }
        }

        // deflate to current position in output buffer
        int compCount;

        compCount = deflater.deflate(outBuf, outPosn, LOCAL_BUF_SIZE);
        outPosn += compCount;

        //System.out.println("Compressed " + want + ", output " + compCount);
    }
}