Search in sources :

Example 66 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project robovm by robovm.

the class GZIPInputStreamTest method gunzip.

public static byte[] gunzip(byte[] bytes) throws IOException {
    InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int count;
    while ((count = in.read(buffer)) != -1) {
        out.write(buffer, 0, count);
    }
    in.close();
    return out.toByteArray();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 67 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project robovm by robovm.

the class GZIPInputStreamTest method testSkip.

/** http://b/3042574 GzipInputStream.skip() causing CRC failures */
public void testSkip() throws IOException {
    byte[] data = new byte[1024 * 1024];
    byte[] gzipped = GZIPOutputStreamTest.gzip(data);
    GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(gzipped));
    long totalSkipped = 0;
    long count;
    do {
        count = in.skip(Long.MAX_VALUE);
        totalSkipped += count;
    } while (count > 0);
    assertEquals(data.length, totalSkipped);
    in.close();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 68 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project robovm by robovm.

the class OldAndroidGZIPStreamTest method scanGZIP.

static void scanGZIP(ByteArrayInputStream bytesIn) throws IOException {
    GZIPInputStream in = new GZIPInputStream(bytesIn);
    try {
        ByteArrayOutputStream contents = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int len, totalLen = 0;
        while ((len = in.read(buf)) > 0) {
            contents.write(buf, 0, len);
            totalLen += len;
        }
        assertEquals(totalLen, 128 * 1024);
    } finally {
        in.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 69 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project spring-boot by spring-projects.

the class SampleTomcatApplicationTests method testCompression.

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
    ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, requestEntity, byte[].class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {
        assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 70 with GZIPInputStream

use of java.util.zip.GZIPInputStream in project spring-boot by spring-projects.

the class SampleJettyApplicationTests method testCompression.

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
    ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, requestEntity, byte[].class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {
        assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15