List of usage examples for java.util.zip GZIPOutputStream close
public void close() throws IOException
From source file:com.thoughtworks.go.server.websocket.ConsoleLogSender.java
byte[] maybeGzipIfLargeEnough(byte[] input) { if (input.length < 512) { return input; }//from w ww . java 2 s . c om // To avoid having to re-allocate the internal byte array, allocate an initial buffer assuming a safe 10:1 compression ratio final ByteArrayOutputStream gzipBytes = new ByteArrayOutputStream(input.length / 10); try { final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(gzipBytes, 1024 * 8); gzipOutputStream.write(input); gzipOutputStream.close(); } catch (IOException e) { LOGGER.error("Could not gzip {}", input); } return gzipBytes.toByteArray(); }
From source file:com.parse.ParseHttpClientTest.java
private void doSingleParseHttpClientExecuteWithGzipResponse(int responseCode, String responseStatus, final String responseContent, ParseHttpClient client) throws Exception { MockWebServer server = new MockWebServer(); // Make mock response Buffer buffer = new Buffer(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut); gzipOut.write(responseContent.getBytes()); gzipOut.close(); buffer.write(byteOut.toByteArray()); MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + responseCode + " " + responseStatus) .setBody(buffer).setHeader("Content-Encoding", "gzip"); // Start mock server server.enqueue(mockResponse);// w w w .j ava 2s .c o m server.start(); // We do not need to add Accept-Encoding header manually, httpClient library should do that. String requestUrl = server.getUrl("/").toString(); ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl) .setMethod(ParseHttpRequest.Method.GET).build(); // Execute request ParseHttpResponse parseResponse = client.execute(parseRequest); RecordedRequest recordedRequest = server.takeRequest(); // Verify request method assertEquals(ParseHttpRequest.Method.GET.toString(), recordedRequest.getMethod()); // Verify request headers Headers recordedHeaders = recordedRequest.getHeaders(); assertEquals("gzip", recordedHeaders.get("Accept-Encoding")); // Verify response body byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent()); assertArrayEquals(responseContent.getBytes(), content); // Shutdown mock server server.shutdown(); }
From source file:csiro.pidsvc.servlet.controller.java
protected void returnAttachment(HttpServletResponse response, String filename, String outputFormat, String content) throws IOException { // response.setContentType("application/xml"); response.getWriter().write(content); return; if (outputFormat.equalsIgnoreCase("xml")) { response.setContentType("application/xml"); response.setHeader("Content-Disposition", "attachment; filename=" + filename); response.getOutputStream().write(content.getBytes()); } else {/*from w w w . j ava 2 s .co m*/ response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=" + filename); GZIPOutputStream gos = new GZIPOutputStream(response.getOutputStream()); gos.write(content.getBytes()); gos.close(); } }
From source file:org.spout.vanilla.data.resources.MapPalette.java
/** * Tries to save this Map Palette to file * @param file to save to// w w w . ja v a 2 s. co m * @return True if it was successful, False if not */ public boolean save(File file) { try { GZIPOutputStream stream = new GZIPOutputStream(new FileOutputStream(file)); try { this.write(stream); return true; } finally { stream.close(); } } catch (IOException ex) { ex.printStackTrace(); } return false; }
From source file:NGzipCompressingEntity.java
public void writeTo(final OutputStream outstream) throws IOException { if (outstream == null) { throw new IllegalArgumentException("Output stream may not be null"); }/*ww w.j a va 2 s . co m*/ System.out.println("Writing gzip"); GZIPOutputStream gzip = new GZIPOutputStream(outstream); InputStream in = wrappedEntity.getContent(); byte[] tmp = new byte[2048]; int l; while ((l = in.read(tmp)) != -1) { gzip.write(tmp, 0, l); } gzip.close(); }
From source file:de.undercouch.gradle.tasks.download.CompressionTest.java
@Override protected Handler[] makeHandlers() throws IOException { ContextHandler compressionHandler = new ContextHandler("/" + COMPRESSED) { @Override/*from w w w. j a v a2 s . com*/ public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { String acceptEncoding = request.getHeader("Accept-Encoding"); boolean acceptGzip = "gzip".equals(acceptEncoding); response.setStatus(200); OutputStream os = response.getOutputStream(); if (acceptGzip) { response.setHeader("Content-Encoding", "gzip"); GZIPOutputStream gos = new GZIPOutputStream(os); OutputStreamWriter osw = new OutputStreamWriter(gos); osw.write("Compressed"); osw.close(); gos.flush(); gos.close(); } else { OutputStreamWriter osw = new OutputStreamWriter(os); osw.write("Uncompressed"); osw.close(); } os.close(); } }; return new Handler[] { compressionHandler }; }
From source file:net.sf.ehcache.constructs.web.PageInfo.java
/** * @param ungzipped the bytes to be gzipped * @return gzipped bytes//from w w w . j a v a2s. c o m */ private byte[] gzip(byte[] ungzipped) throws IOException, AlreadyGzippedException { if (isGzipped(ungzipped)) { throw new AlreadyGzippedException("The byte[] is already gzipped. It should not be gzipped again."); } final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes); gzipOutputStream.write(ungzipped); gzipOutputStream.close(); return bytes.toByteArray(); }
From source file:arena.utils.FileUtils.java
public static void gzip(File input, File outFile) { Log log = LogFactory.getLog(FileUtils.class); InputStream inStream = null;// ww w . j a v a 2 s . c o m OutputStream outStream = null; GZIPOutputStream gzip = null; try { long inFileLengthKB = input.length() / 1024L; // Open the out file if (outFile == null) { outFile = new File(input.getParentFile(), input.getName() + ".gz"); } inStream = new FileInputStream(input); outStream = new FileOutputStream(outFile, true); gzip = new GZIPOutputStream(outStream); // Iterate through in buffers and write out to the gzipped output stream byte buffer[] = new byte[102400]; // 100k buffer int read = 0; long readSoFar = 0; while ((read = inStream.read(buffer)) != -1) { readSoFar += read; gzip.write(buffer, 0, read); log.debug("Gzipped " + (readSoFar / 1024L) + "KB / " + inFileLengthKB + "KB of logfile " + input.getName()); } // Close the streams inStream.close(); inStream = null; gzip.close(); gzip = null; outStream.close(); outStream = null; // Delete the old file input.delete(); log.debug("Gzip of logfile " + input.getName() + " complete"); } catch (IOException err) { // Delete the gzip file log.error("Error during gzip of logfile " + input, err); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException err2) { } } if (gzip != null) { try { gzip.close(); } catch (IOException err2) { } } if (outStream != null) { try { outStream.close(); } catch (IOException err2) { } } } }
From source file:com.orange.mmp.api.ws.jsonrpc.MMPJsonRpcServlet.java
/** * Gzip something.//www . j ava 2s . c o m * * @param in * original content * @return size gzipped content */ private byte[] gzip(byte[] in) throws IOException { if (in != null && in.length > 0) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(in); gout.flush(); gout.close(); return bout.toByteArray(); } return new byte[0]; }