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");
}
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());
}
}
}
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) {
}
}
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();
}
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();
}
}
Aggregations