Package java.util.zip

Examples of java.util.zip.Inflater


    }

    public static byte[] decompress(byte[] compressedData, int off, int len) throws IOException, DataFormatException
    {
       // Create the decompressor and give it the data to compress
        Inflater decompressor = new Inflater();
        decompressor.setInput(compressedData, off, len);

        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);

        // Decompress the data
        byte[] buf = new byte[1024];
        while (!decompressor.finished())
        {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }
        bos.close();

        // Get the decompressed data
View Full Code Here


            if (oleLength < readbytes) {
                return "oleLength :" + oleLength + " readbytes: " + readbytes;
            }

            // Decompress the bytes
            Inflater decompresser = new Inflater();
            decompresser.setInput(pContents[0], 0, readbytes);
            byte[] result = new byte[oleLength];
            int resultLength = decompresser.inflate(result);
            decompresser.end();

            //return the base64 string of the uncompressed data           
            return Base64.encodeBytes(result);
        } catch (Exception ex) {
            ex.printStackTrace();
View Full Code Here

  public void test_inflate$B() {
    // test method of java.util.zip.inflater.inflate(byte)

    byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    byte outPutInf[] = new byte[500];
    Inflater inflate = new Inflater();
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuff1);
        }
        inflate.inflate(outPutInf);
      }
    } catch (DataFormatException e) {
      fail("Invalid input to be decompressed");
    }
    for (int i = 0; i < byteArray.length; i++) {
      assertTrue(
          "Final decompressed data does not equal the original data",
          byteArray[i] == outPutInf[i]);
    }
    assertEquals("final decompressed data contained more bytes than original - inflateB",
        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);
        }
        infEmpty.inflate(outPutInf);
      }
    } catch (DataFormatException e) {
      fail("Invalid input to be decompressed");
    }
    for (int i = 0; i < emptyArray.length; i++) {
View Full Code Here

    // test method of java.util.zip.inflater.inflate(byte,int,int)

    byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    byte outPutInf[] = new byte[100];
    int y = 0;
    Inflater inflate = new Inflater();
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuff1);
        }
        y += inflate.inflate(outPutInf, y, outPutInf.length - y);
      }
    } catch (DataFormatException e) {
      fail("Invalid input to be decompressed");
    }
    for (int i = 0; i < byteArray.length; i++) {
      assertTrue(
          "Final decompressed data does not equal the original data",
          byteArray[i] == outPutInf[i]);
    }
    assertEquals("final decompressed data contained more bytes than original - inflateB",
        0, outPutInf[byteArray.length]);

    // test boundary checks
    inflate.reset();
    int r = 0;
    int offSet = 0;
    int lengthError = 101;
    try {
      if (inflate.needsInput()) {
        inflate.setInput(outPutBuff1);
      }
      inflate.inflate(outPutInf, offSet, lengthError);

    } catch (DataFormatException e) {
      fail("Invalid input to be decompressed");
    } catch (ArrayIndexOutOfBoundsException e) {
      r = 1;
View Full Code Here

   * @tests java.util.zip.Inflater#Inflater()
   */
  public void test_Constructor() {
    // test method of java.util.zip.inflater.Inflater()
    try {
      Inflater inflate = new Inflater();
      assertNotNull("failed to create the instance of inflater",
          inflate);

    } catch (Exception e) {

View Full Code Here

  public void test_ConstructorZ() {
    // test method of java.util.zip.inflater.Inflater(boolean)
    // note does not throw exception if deflater has a header, but inflater
    // doesn't or vice versa.
    byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    Inflater inflate = new Inflater(true);
    assertNotNull("failed to create the instance of inflater", inflate);
    byte outPutInf[] = new byte[500];
    int r = 0;
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuff1);
        }

        inflate.inflate(outPutInf);
      }
      for (int i = 0; i < byteArray.length; i++) {
        assertEquals("the output array from inflate should contain 0 because the header of inflate and deflate did not match, but this failed",
            0, outPutBuff1[i]);
      }
View Full Code Here

    // test method of java.util.zip.inflater.needsDictionary()
    // note: this flag is set after inflate is called
    byte outPutInf[] = new byte[500];

    // testing with dictionary set.
    Inflater inflateDiction = new Inflater();
    if (inflateDiction.needsInput()) {
      inflateDiction.setInput(outPutDiction);
    }
    try {
      assertEquals("should return 0 because needs dictionary",
          0, inflateDiction.inflate(outPutInf));
    } catch (DataFormatException e) {
      fail("Should not cause exception");
    }
    assertTrue(
        "method needsDictionary returned false when dictionary was used in deflater",
        inflateDiction.needsDictionary());

    // testing without dictionary
    Inflater inflate = new Inflater();
    try {
      inflate.setInput(outPutBuff1);
      inflate.inflate(outPutInf);
      assertFalse(
          "method needsDictionary returned true when dictionary was not used in deflater",
          inflate.needsDictionary());
    } catch (DataFormatException e) {
      fail(
          "Input to inflate is invalid or corrupted - needsDictionary");
    }

        // Regression test for HARMONY-86
        Inflater inf = new Inflater();
        assertFalse(inf.needsDictionary());
        assertEquals(0,inf.getTotalIn());
        assertEquals(0,inf.getTotalOut());
        assertEquals(0,inf.getBytesRead());
        assertEquals(0,inf.getBytesWritten());
  }
View Full Code Here

  /**
   * @tests java.util.zip.Inflater#needsInput()
   */
  public void test_needsInput() {
    // test method of java.util.zip.inflater.needsInput()
    Inflater inflate = new Inflater();
    assertTrue(
        "needsInput give the wrong boolean value as a result of no input buffer",
        inflate.needsInput());

    byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    inflate.setInput(byteArray);
    assertFalse(
        "methodNeedsInput returned true when the input buffer is full",
        inflate.needsInput());

    inflate.reset();
    byte byteArrayEmpty[] = new byte[0];
    inflate.setInput(byteArrayEmpty);
    assertTrue(
        "needsInput give wrong boolean value as a result of an empty input buffer",
        inflate.needsInput());
  }
View Full Code Here

  public void test_reset() {
    // test method of java.util.zip.inflater.reset()
    byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
    byte outPutInf[] = new byte[100];
    int y = 0;
    Inflater inflate = new Inflater();
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuff1);
        }
        y += inflate.inflate(outPutInf, y, outPutInf.length - y);
      }
    } catch (DataFormatException e) {
      fail("Invalid input to be decompressed");
    }
    for (int i = 0; i < byteArray.length; i++) {
      assertTrue(
          "Final decompressed data does not equal the original data",
          byteArray[i] == outPutInf[i]);
    }
    assertEquals("final decompressed data contained more bytes than original - reset",
        0, outPutInf[byteArray.length]);

    // testing that resetting the inflater will also return the correct
    // decompressed data

    inflate.reset();
    try {
      while (!(inflate.finished())) {
        if (inflate.needsInput()) {
          inflate.setInput(outPutBuff1);
        }
        inflate.inflate(outPutInf);
      }
    } catch (DataFormatException e) {
      fail("Invalid input to be decompressed");
    }
    for (int i = 0; i < byteArray.length; i++) {
View Full Code Here

   * @tests java.util.zip.Inflater#setInput(byte[])
   */
  public void test_setInput$B() {
    // test method of java.util.zip.inflater.setInput(byte)
    byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    Inflater inflate = new Inflater();
    inflate.setInput(byteArray);
    assertTrue("setInputB did not deliver any byte to the input buffer",
        inflate.getRemaining() != 0);
  }
View Full Code Here

TOP

Related Classes of java.util.zip.Inflater

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.