Java Code Examples for java.util.zip.InflaterInputStream

Following code examples demonstrate how to use java.util.zip.InflaterInputStreamfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.util.zip.InflaterInputStreamand various code implementation of this class.

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to InflaterInputStream.class!",
                   InflaterInputStream.class.isAssignableFrom(sut));
    } 


	public void test_available() throws IOException {
		InputStream is = Support_Resources.getStream("hyts_available.tst");
		InflaterInputStream iis = new InflaterInputStream(is);

        int available;
        for (int i = 0; i < 11; i++) {
            iis.read();
            available = iis.available();
            if (available == 0) {
                assertEquals("Expected no more bytes to read", -1, iis.read());
            } else {
                assertEquals("Bytes Available Should Return 1.", 1, available);
            }
        }

		iis.close();
		try {
			iis.available();
			fail("available after close should throw IOException.");
		} catch (IOException e) {
		}
	} 

	public void test_ConstructorLjava_io_InputStreamLjava_util_zip_Inflater() throws IOException {
		byte byteArray[] = new byte[100];
		Inflater inflate = new Inflater();
		InflaterInputStream inflatIP = new InflaterInputStream(infile,
				inflate);

		inflatIP.close();
	} 

    public void test_markSupported() {
        InputStream is = new ByteArrayInputStream(new byte[10]);
        InflaterInputStream iis = new InflaterInputStream(is);
        assertFalse(iis.markSupported());
        assertTrue(is.markSupported());
    } 

        while (true) {
            result = iis.read(outBuf, 0, 5);
            if (result == -1) {
                break;
            }
        } 

	public void test_close() throws IOException {
		InflaterInputStream iin = new InflaterInputStream(
				new ByteArrayInputStream(new byte[0]));
		iin.close();

		iin.close();
	} 

	public void test_ConstructorLjava_io_InputStreamLjava_util_zip_InflaterI() throws IOException {
		int result = 0;
		int buffer[] = new int[500];
		Inflater inflate = new Inflater();
		InflaterInputStream inflatIP = new InflaterInputStream(infile,
				inflate, 1);

		int i = 0;
		while ((result = inflatIP.read()) != -1) {
			buffer[i] = result;
			i++;
		}
		inflatIP.close();
	} 

    public void test_reset() {
        InputStream is = new ByteArrayInputStream(new byte[10]);
        InflaterInputStream iis = new InflaterInputStream(is);
        try {
            iis.reset();
            fail("Should throw IOException");
        } catch (IOException e) {
        }
    } 

    public void testAvailableEmptySource() throws Exception {
        byte[] deflated = { 120, -100, 3, 0, 0, 0, 0, 1 };
        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
        assertEquals(-1, in.read());
        assertEquals(-1, in.read());
        assertEquals(0, in.available());
    } 

	public void test_skipJ() throws IOException {
		InputStream is = Support_Resources.getStream("hyts_available.tst");
		InflaterInputStream iis = new InflaterInputStream(is);

		try {
			iis.skip(-3);
			fail("IllegalArgumentException not thrown");
		} catch (IllegalArgumentException e) {
		}
		assertEquals("Incorrect Byte Returned.", 5, iis.read());

		try {
			iis.skip(Integer.MIN_VALUE);
			fail("IllegalArgumentException not thrown");
		} catch (IllegalArgumentException e) {
		}
		assertEquals("Incorrect Byte Returned.", 4, iis.read());

		assertEquals("Incorrect Number Of Bytes Skipped.", 3, iis.skip(3));

		assertEquals("Incorrect Byte Returned.", 7, iis.read());

		assertEquals("Incorrect Number Of Bytes Skipped.", 0, iis.skip(0));
		assertEquals("Incorrect Byte Returned.", 0, iis.read());

		assertEquals("Incorrect Number Of Bytes Skipped.", 2, iis.skip(4));
		assertEquals("Incorrect Byte Returned.", -1, iis.read());
		iis.close();
	} 

    public void testAvailableSkip() throws Exception {
        byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
        assertEquals(1, in.available());
        assertEquals(4, in.skip(4));
        assertEquals(0, in.available());
    } 

    public void testAvailableNonEmptySource() throws Exception {
        byte[] deflated = { 72, -119, 99, 100, 102, 97, 3, 0, 0, 31, 0, 15, 0 };
        InputStream in = new InflaterInputStream(new ByteArrayInputStream(deflated));
        assertEquals(1, in.read());
        assertEquals(1, in.available());
        assertEquals(3, in.read());
        assertEquals(1, in.available());
        assertEquals(4, in.read());
        assertEquals(1, in.available());
        assertEquals(6, in.read());
        assertEquals(0, in.available());
        assertEquals(-1, in.read());
        assertEquals(-1, in.read());
    } 

    public void test_markI() {
        InputStream is = new ByteArrayInputStream(new byte[10]);
        InflaterInputStream iis = new InflaterInputStream(is);
        iis.mark(0);
        iis.mark(-1);
        iis.mark(10000000);
    } 

	public void test_skipJ2() throws IOException {
		int result = 0;
		int buffer[] = new int[100];
		byte orgBuffer[] = { 1, 3, 4, 7, 8 };

		InputStream infile = Support_Resources
		Inflater inflate = new Inflater();
		InflaterInputStream inflatIP = new InflaterInputStream(infile,
				inflate, 10);
		long skip;
		try {
			skip = inflatIP.skip(Integer.MIN_VALUE);
			fail("Expected IllegalArgumentException when skip() is called with negative parameter");
		} catch (IllegalArgumentException e) {
		}
		inflatIP.close();

		InputStream infile2 = Support_Resources
		InflaterInputStream inflatIP2 = new InflaterInputStream(infile2);


		skip = inflatIP2.skip(Integer.MAX_VALUE);
		assertEquals("method skip() returned wrong number of bytes skipped",
				5, skip);

		InputStream infile3 = Support_Resources
		InflaterInputStream inflatIP3 = new InflaterInputStream(infile3);
		skip = inflatIP3.skip(2);
		assertEquals("the number of bytes returned by skip did not correspond with its input parameters",
				2, skip);
		int i = 0;
		result = 0;
		while ((result = inflatIP3.read()) != -1) {
			buffer[i] = result;
			i++;
		}
		inflatIP2.close();

		for (int j = 2; j < orgBuffer.length; j++) {
			assertTrue(
				"original compressed data did not equal decompressed data",
				buffer[j - 2] == orgBuffer[j]);
		}
	} 

	public void test_read() throws IOException {
		int result = 0;
		int buffer[] = new int[500];
		byte orgBuffer[] = { 1, 3, 4, 7, 8 };
		InputStream infile = Support_Resources
		Inflater inflate = new Inflater();
		InflaterInputStream inflatIP = new InflaterInputStream(infile,
				inflate);

		int i = 0;
		while ((result = inflatIP.read()) != -1) {
			buffer[i] = result;
			i++;
		}
		inflatIP.close();

		for (int j = 0; j < orgBuffer.length; j++) {
			assertTrue(
				"original compressed data did not equal decompressed data",
				buffer[j] == orgBuffer[j]);
		}
	} 

Advertisement
Javadoc
This class implements a stream filter for uncompressing data in the "deflate" compression format. It

is also used as the basis for other decompression filters, such as GZIPInputStream. @see Inflater @author David Connelly

Read More
Advertisement