Package java.util.zip

Examples of java.util.zip.Deflater


            oleHead[3] = (byte) ((oledata.length >>> 24) & 0xFF);
            xOutput.writeBytes(oleHead);

            // Compress the bytes
            byte[] output = new byte[oledata.length];
            Deflater compresser = new Deflater();
            compresser.setInput(oledata);
            compresser.finish();
            int compressedDataLength = compresser.deflate(output);
            //realloc the data length
            byte[] compressedBytes = new byte[compressedDataLength];
            for (int i = 0; i < compressedDataLength; i++) {
                compressedBytes[i] = output[i];
            }
View Full Code Here


    // creating the decompressed data
    byte outPutBuf[] = new byte[500];
    byte byteArray[] = { 1, 3, 4, 7, 8 };
    byte outPutInf[] = new byte[500];
    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);
    }

    Inflater inflate = new Inflater();
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuf);
        }

        inflate.inflate(outPutInf);
      }
    } catch (DataFormatException e) {
      fail("Input to inflate is invalid or corrupted - getTotalIn");
    }
    // System.out.print(deflate.getTotalOut() + " " + inflate.getTotalIn());
    assertTrue(
        "the total byte in outPutBuf did not equal the byte returned in getTotalIn",
        inflate.getTotalIn() == deflate.getTotalOut());

    Inflater inflate2 = new Inflater();
    int offSet = 0;// seems only can start as 0
    int length = 4;
    try {
View Full Code Here

    // creating the decompressed data
    byte outPutBuf[] = new byte[500];
    byte byteArray[] = { 1, 3, 4, 7, 8 };
    int y = 0;
    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);
    }

    Inflater inflate = new Inflater();
    byte outPutInf[] = new byte[500];
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuf);
        }

        y += inflate.inflate(outPutInf);
      }
    } catch (DataFormatException e) {
      fail("Input to inflate is invalid or corrupted - getTotalIn");
    }

    assertTrue(
        "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
        y == inflate.getTotalOut());
    assertTrue(
        "the total number of bytes to be compressed does not equal the total bytes decompressed",
        inflate.getTotalOut() == deflate.getTotalIn());

    // testing inflate(byte,int,int)
    inflate.reset();
    y = 0;
    int offSet = 0;// seems only can start as 0
    int length = 4;
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuf);
        }

        y += inflate.inflate(outPutInf, offSet, length);
      }
    } catch (DataFormatException e) {
      System.out
          .println("Input to inflate is invalid or corrupted - getTotalIn");
    }
    assertTrue(
        "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
        y == inflate.getTotalOut());
    assertTrue(
        "the total number of bytes to be compressed does not equal the total bytes decompressed",
        inflate.getTotalOut() == deflate.getTotalIn());
  }
View Full Code Here

        0, outPutInf[byteArray.length]);
    // testing for an empty input array
    byte outPutBuf[] = new byte[500];
    byte emptyArray[] = new byte[11];
    int x = 0;
    Deflater defEmpty = new Deflater(3);
    defEmpty.setInput(emptyArray);
    while (!(defEmpty.needsInput())) {
      x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
    }
    defEmpty.finish();
    while (!(defEmpty.finished())) {
      x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
    }
    assertTrue(
        "the total number of byte from deflate did not equal getTotalOut - inflate(byte)",
        x == defEmpty.getTotalOut());
    assertTrue(
        "the number of input byte from the array did not correspond with getTotalIn - inflate(byte)",
        defEmpty.getTotalIn() == emptyArray.length);
    Inflater infEmpty = new Inflater();
    try {
      while (!(infEmpty.finished())) {
        if (infEmpty.needsInput()) {
          infEmpty.setInput(outPutBuf);
View Full Code Here

     * @tests java.util.zip.Deflater#getBytesRead()
     */
    public void test_getBytesRead() throws DataFormatException,
            UnsupportedEncodingException {
        // Regression test for HARMONY-158
        Deflater def = new Deflater();
        Inflater inf = new Inflater();
        assertEquals(0, def.getTotalIn());
        assertEquals(0, def.getTotalOut());
        assertEquals(0, def.getBytesRead());
        // Encode a String into bytes
        String inputString = "blahblahblah??";
        byte[] input = inputString.getBytes("UTF-8");

        // Compress the bytes
        byte[] output = new byte[100];
        def.setInput(input);
        def.finish();
        def.deflate(output);
        inf.setInput(output);
        int compressedDataLength =inf.inflate(input);
        assertEquals(16, inf.getTotalIn());
        assertEquals(compressedDataLength, inf.getTotalOut());
        assertEquals(16, inf.getBytesRead());
View Full Code Here

    /**
     * @tests java.util.zip.Deflater#getBytesRead()
     */
    public void test_getBytesWritten() throws DataFormatException, UnsupportedEncodingException {
        // Regression test for HARMONY-158
        Deflater def = new Deflater();
        Inflater inf = new Inflater();
        assertEquals(0, def.getTotalIn());
        assertEquals(0, def.getTotalOut());
        assertEquals(0, def.getBytesWritten());
        // Encode a String into bytes
        String inputString = "blahblahblah??";
        byte[] input = inputString.getBytes("UTF-8");

        // Compress the bytes
        byte[] output = new byte[100];
        def.setInput(input);
        def.finish();
        def.deflate(output);
        inf.setInput(output);
        int compressedDataLength =inf.inflate(input);
        assertEquals(16, inf.getTotalIn());
        assertEquals(compressedDataLength, inf.getTotalOut());
        assertEquals(14, inf.getBytesWritten());
View Full Code Here

    public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
        // ask for a mandatory type conversion to avoid a possible NPE beforehand as we do copy from the InputStream
        InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph);

        DeflaterOutputStream zipOutput = new DeflaterOutputStream(stream, new Deflater(compressionLevel));
        try {
            IOHelper.copy(is, zipOutput);
        } finally {
            IOHelper.close(is, zipOutput);
        }
View Full Code Here

    // The given byte array is compressed onto the specified stream.
    // The method does not close the stream. The caller will have to do it.
    public static void compressToStream(byte[] input, ByteArrayOutputStream bos) throws IOException
    {
       // Create the compressor with highest level of compression
        Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.BEST_COMPRESSION);

        // Give the compressor the data to compress
        compressor.setInput(input);
        compressor.finish();

        // Write the compressed data to the stream
        byte[] buf = new byte[1024];
        while (!compressor.finished())
        {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
    }
View Full Code Here

        File tempFile = null;

        int nextIFDOffset = 0;
        boolean skipByte = false;

        Deflater deflater = null;
        int deflateLevel = Deflater.DEFAULT_COMPRESSION;

        boolean jpegRGBToYCbCr = false;

        if(compression == COMP_NONE) {
            // Determine the number of bytes of padding necessary between
            // the end of the IFD and the first data segment such that the
            // alignment of the data conforms to the specification (required
            // for uncompressed data only).
            int numBytesPadding = 0;
            if(sampleSize[0] == 16 && tileOffsets[0] % 2 != 0) {
                numBytesPadding = 1;
                tileOffsets[0]++;
            } else if(sampleSize[0] == 32 && tileOffsets[0] % 4 != 0) {
                numBytesPadding = (int)(4 - tileOffsets[0] % 4);
                tileOffsets[0] += numBytesPadding;
            }

            // Update the data offsets (which TIFFField stores by reference).
            for (int i = 1; i < numTiles; i++) {
                tileOffsets[i] = tileOffsets[i-1] + tileByteCounts[i-1];
            }

            if(!isLast) {
                // Determine the offset of the next IFD.
                nextIFDOffset = (int)(tileOffsets[0] + totalBytesOfData);

                // IFD offsets must be on a word boundary.
                if(nextIFDOffset % 2 != 0) {
                    nextIFDOffset++;
                    skipByte = true;
                }
            }

            // Write the IFD and field overflow before the image data.
            writeDirectory(ifdOffset, fields, nextIFDOffset);

            // Write any padding bytes needed between the end of the IFD
            // and the start of the actual image data.
            if(numBytesPadding != 0) {
                for(int padding = 0; padding < numBytesPadding; padding++) {
                    output.write((byte)0);
                }
            }
        } else {
            // If compressing, the cannot be written yet as the size of the
            // data segments is unknown.

            if((output instanceof SeekableOutputStream)) {
                // Simply seek to the first data segment position.
                ((SeekableOutputStream)output).seek(tileOffsets[0]);
            } else {
                // Cache the original OutputStream.
                outCache = output;

                try {
                    // Attempt to create a temporary file.
                    tempFile = File.createTempFile("jai-SOS-", ".tmp");
                    tempFile.deleteOnExit();
                    RandomAccessFile raFile =
                        new RandomAccessFile(tempFile, "rw");
                    output = new SeekableOutputStream(raFile);
                   
                    // this method is exited!
                } catch(Exception e) {
                    // Allocate memory for the entire image data (!).
                    output = new ByteArrayOutputStream((int)totalBytesOfData);
                }
            }

            int bufSize = 0;
            switch(compression) {
            case COMP_PACKBITS:
                bufSize = (int)(bytesPerTile +
                                ((bytesPerRow+127)/128)*tileHeight);
                break;
            case COMP_JPEG_TTN2:
                bufSize = 0;

                // Set color conversion flag.
                if(imageType == TIFF_YCBCR &&
                   colorModel != null &&
                   colorModel.getColorSpace().getType() ==
                   ColorSpace.TYPE_RGB) {
                    jpegRGBToYCbCr = true;
                }
            case COMP_DEFLATE:
                bufSize = (int)bytesPerTile;
                deflater = new Deflater(encodeParam.getDeflateLevel());
                break;
            default:
                bufSize = 0;
            }
            if(bufSize != 0) {
View Full Code Here

  public DeflateSerializer (Serializer serializer) {
    this.serializer = serializer;
  }

  public void write (Kryo kryo, Output output, Object object) {
    Deflater deflater = new Deflater(compressionLevel, noHeaders);
    OutputChunked outputChunked = new OutputChunked(output, 256);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(outputChunked, deflater);
    Output deflaterOutput = new Output(deflaterStream, 256);
    kryo.writeObject(deflaterOutput, object, serializer);
    deflaterOutput.flush();
View Full Code Here

TOP

Related Classes of java.util.zip.Deflater

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.