Search in sources :

Example 16 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project druid by druid-io.

the class S3DataSegmentPullerTest method testGZUncompress.

@Test
public void testGZUncompress() throws ServiceException, IOException, SegmentLoadingException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
    final byte[] value = bucket.getBytes("utf8");
    final File tmpFile = temporaryFolder.newFile("gzTest.gz");
    try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
        outputStream.write(value);
    }
    final S3Object object0 = new S3Object();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.setLastModifiedDate(new Date(0));
    object0.setDataInputStream(new FileInputStream(tmpFile));
    final File tmpDir = temporaryFolder.newFolder("gzTestDir");
    EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(null).once();
    EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    EasyMock.expect(s3Client.getObject(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client);
    EasyMock.replay(s3Client);
    FileUtils.FileCopyResult result = puller.getSegmentFiles(new S3DataSegmentPuller.S3Coords(bucket, object0.getKey()), tmpDir);
    EasyMock.verify(s3Client);
    Assert.assertEquals(value.length, result.size());
    File expected = new File(tmpDir, "renames-0");
    Assert.assertTrue(expected.exists());
    Assert.assertEquals(value.length, expected.length());
}
Also used : FileUtils(io.druid.java.util.common.FileUtils) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) File(java.io.File) Test(org.junit.Test)

Example 17 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project crate by crate.

the class FileReadingCollectorTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    Path copy_from = Files.createTempDirectory("copy_from");
    Path copy_from_gz = Files.createTempDirectory("copy_from_gz");
    Path copy_from_empty = Files.createTempDirectory("copy_from_empty");
    tmpFileGz = File.createTempFile("fileReadingCollector", ".json.gz", copy_from_gz.toFile());
    tmpFile = File.createTempFile("fileReadingCollector", ".json", copy_from.toFile());
    tmpFileEmptyLine = File.createTempFile("emptyLine", ".json", copy_from_empty.toFile());
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(tmpFileGz)), StandardCharsets.UTF_8))) {
        writer.write("{\"name\": \"Arthur\", \"id\": 4, \"details\": {\"age\": 38}}\n");
        writer.write("{\"id\": 5, \"name\": \"Trillian\", \"details\": {\"age\": 33}}\n");
    }
    try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) {
        writer.write("{\"name\": \"Arthur\", \"id\": 4, \"details\": {\"age\": 38}}\n");
        writer.write("{\"id\": 5, \"name\": \"Trillian\", \"details\": {\"age\": 33}}\n");
    }
    try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tmpFileEmptyLine), StandardCharsets.UTF_8)) {
        writer.write("{\"name\": \"Arthur\", \"id\": 4, \"details\": {\"age\": 38}}\n");
        writer.write("\n");
        writer.write("{\"id\": 5, \"name\": \"Trillian\", \"details\": {\"age\": 33}}\n");
    }
}
Also used : Path(java.nio.file.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) BeforeClass(org.junit.BeforeClass)

Example 18 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.

the class GzipHandlerTest method testGzipRequest.

@Test
public void testGzipRequest() throws Exception {
    String data = "Hello Nice World! ";
    for (int i = 0; i < 10; ++i) data += data;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    // generated and parsed test
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod("POST");
    request.setURI("/ctx/echo");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Content-Type", "text/plain");
    request.setHeader("Content-Encoding", "gzip");
    request.setContent(bytes);
    response = HttpTester.parseResponse(_connector.getResponse(request.generate()));
    assertThat(response.getStatus(), is(200));
    assertThat(response.getContent(), is(data));
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 19 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentSentTwiceInOneWrite.

@Test
public void testGZIPContentSentTwiceInOneWrite() throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] content = Arrays.copyOf(gzipBytes, 2 * gzipBytes.length);
            System.arraycopy(gzipBytes, 0, content, gzipBytes.length, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(content);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    byte[] expected = Arrays.copyOf(data, 2 * data.length);
    System.arraycopy(data, 0, expected, data.length, data.length);
    Assert.assertArrayEquals(expected, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream) Test(org.junit.Test)

Example 20 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentFragmented.

private void testGZIPContentFragmented(final int fragment) throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] chunk1 = Arrays.copyOfRange(gzipBytes, 0, gzipBytes.length - fragment);
            byte[] chunk2 = Arrays.copyOfRange(gzipBytes, gzipBytes.length - fragment, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            sleep(500);
            output.write(chunk2);
            output.flush();
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)322 ByteArrayOutputStream (java.io.ByteArrayOutputStream)135 FileOutputStream (java.io.FileOutputStream)96 IOException (java.io.IOException)93 OutputStream (java.io.OutputStream)66 File (java.io.File)62 Test (org.junit.Test)48 BufferedOutputStream (java.io.BufferedOutputStream)30 FileInputStream (java.io.FileInputStream)28 OutputStreamWriter (java.io.OutputStreamWriter)27 GZIPInputStream (java.util.zip.GZIPInputStream)25 InputStream (java.io.InputStream)24 ByteBuffer (java.nio.ByteBuffer)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 BufferedWriter (java.io.BufferedWriter)18 DropBoxManager (android.os.DropBoxManager)15 BufferedReader (java.io.BufferedReader)11 DataOutputStream (java.io.DataOutputStream)11 FileNotFoundException (java.io.FileNotFoundException)11 InputStreamReader (java.io.InputStreamReader)11