Search in sources :

Example 66 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project lucene-solr by apache.

the class TestFastInputStream method testgzip.

@Test
public void testgzip() throws Exception {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    FastOutputStream fos = new FastOutputStream(b);
    GZIPOutputStream gzos = new GZIPOutputStream(fos);
    String ss = "Helloooooooooooooooooooo";
    writeChars(gzos, ss, 0, ss.length());
    gzos.close();
    JavaBinCodec.writeVInt(10, fos);
    fos.flushBuffer();
    GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(b.toByteArray(), 0, b.size()));
    char[] cbuf = new char[ss.length()];
    readChars(gzis, cbuf, 0, ss.length());
    assertEquals(new String(cbuf), ss);
    // System.out.println("passes w/o FastInputStream");
    ByteArrayInputStream bis = new ByteArrayInputStream(b.toByteArray(), 0, b.size());
    gzis = new GZIPInputStream(new FastInputStream(bis));
    cbuf = new char[ss.length()];
    readChars(gzis, cbuf, 0, ss.length());
    assertEquals(new String(cbuf), ss);
// System.out.println("passes w FastInputStream");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Test(org.junit.Test)

Example 67 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project karaf by apache.

the class GogoPlugin method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String encoding = request.getHeader("Accept-Encoding");
    boolean supportsGzip = (encoding != null && encoding.toLowerCase().indexOf("gzip") > -1);
    SessionTerminal st = (SessionTerminal) request.getSession(true).getAttribute("terminal");
    if (st == null || st.isClosed()) {
        st = new SessionTerminal();
        request.getSession().setAttribute("terminal", st);
    }
    String str = request.getParameter("k");
    String f = request.getParameter("f");
    String dump = st.handle(str, f != null && f.length() > 0);
    if (dump != null) {
        if (supportsGzip) {
            response.setHeader("Content-Encoding", "gzip");
            response.setHeader("Content-Type", "text/html");
            try {
                GZIPOutputStream gzos = new GZIPOutputStream(response.getOutputStream());
                gzos.write(dump.getBytes());
                gzos.close();
            } catch (IOException ie) {
                // handle the error here
                ie.printStackTrace();
            }
        } else {
            response.getOutputStream().write(dump.getBytes());
        }
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 68 with GZIPOutputStream

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

the class HeapdumpMvcEndpoint method handle.

/**
	 * Handle the heap dump file and respond. By default this method will return the
	 * response as a GZip stream.
	 * @param heapDumpFile the generated dump file
	 * @param request the HTTP request
	 * @param response the HTTP response
	 * @throws ServletException on servlet error
	 * @throws IOException on IO error
	 */
protected void handle(File heapDumpFile, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + (heapDumpFile.getName() + ".gz") + "\"");
    try {
        InputStream in = new FileInputStream(heapDumpFile);
        try {
            GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream());
            StreamUtils.copy(in, out);
            out.finish();
        } catch (NullPointerException ex) {
        } finally {
            try {
                in.close();
            } catch (Throwable ex) {
            }
        }
    } catch (FileNotFoundException ex) {
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 69 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project spring-framework by spring-projects.

the class GzipResourceResolverTests method createGzFile.

private static void createGzFile(String filePath) throws IOException {
    Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
    Resource fileResource = new FileSystemResource(location.createRelative(filePath).getFile());
    Path gzFilePath = Paths.get(fileResource.getFile().getAbsolutePath() + ".gz");
    Files.deleteIfExists(gzFilePath);
    File gzFile = Files.createFile(gzFilePath).toFile();
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
    FileCopyUtils.copy(fileResource.getInputStream(), out);
    gzFile.deleteOnExit();
}
Also used : Path(java.nio.file.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 70 with GZIPOutputStream

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

the class DeflaterOutputStreamTest method createInflaterStream.

/**
     * Creates an optionally-flushing deflater stream, writes some bytes to it,
     * and flushes it. Returns an inflater stream that reads this deflater's
     * output.
     *
     * <p>These bytes are written on a separate thread so that when the inflater
     * stream is read, that read will fail when no bytes are available. Failing
     * takes 3 seconds, co-ordinated by PipedInputStream's 'broken pipe'
     * timeout. The 3 second delay is unfortunate but seems to be the easiest
     * way demonstrate that data is unavailable. Ie. other techniques will cause
     * the dry read to block indefinitely.
     */
static InputStream createInflaterStream(final Class<?> c, final boolean flushing) throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    final PipedOutputStream pout = new PipedOutputStream();
    PipedInputStream pin = new PipedInputStream(pout);
    executor.submit(new Callable<Void>() {

        public Void call() throws Exception {
            OutputStream out;
            if (c == DeflaterOutputStream.class) {
                out = new DeflaterOutputStream(pout, flushing);
            } else if (c == GZIPOutputStream.class) {
                out = new GZIPOutputStream(pout, flushing);
            } else {
                throw new AssertionError();
            }
            out.write(1);
            out.write(2);
            out.write(3);
            out.flush();
            return null;
        }
    }).get();
    executor.shutdown();
    if (c == DeflaterOutputStream.class) {
        return new InflaterInputStream(pin);
    } else if (c == GZIPOutputStream.class) {
        return new GZIPInputStream(pin);
    } else {
        throw new AssertionError();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InflaterInputStream(java.util.zip.InflaterInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedOutputStream(java.io.PipedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ExecutorService(java.util.concurrent.ExecutorService) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Callable(java.util.concurrent.Callable)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)835 ByteArrayOutputStream (java.io.ByteArrayOutputStream)339 IOException (java.io.IOException)254 FileOutputStream (java.io.FileOutputStream)251 OutputStream (java.io.OutputStream)185 File (java.io.File)179 Test (org.junit.Test)93 BufferedOutputStream (java.io.BufferedOutputStream)84 GZIPInputStream (java.util.zip.GZIPInputStream)77 FileInputStream (java.io.FileInputStream)72 InputStream (java.io.InputStream)64 ByteArrayInputStream (java.io.ByteArrayInputStream)60 OutputStreamWriter (java.io.OutputStreamWriter)53 ObjectOutputStream (java.io.ObjectOutputStream)39 DataOutputStream (java.io.DataOutputStream)38 BufferedWriter (java.io.BufferedWriter)30 ByteBuffer (java.nio.ByteBuffer)28 Path (java.nio.file.Path)28 ArrayList (java.util.ArrayList)28 NodeSettings (org.knime.core.node.NodeSettings)28