public static byte[] gzip(byte[] data) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream out = new GZIPOutputStream(bos); out.write(data); out.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
public Gzipper() throws RuntimeException { try { baos = new ByteArrayOutputStream(256); gzos = new GZIPOutputStream(baos, true); } catch (IOException e) { throw new RuntimeException("Error finalizing the GzipOutputstream", e); } }
/** * Writes byte array to gzip output stream. Creates new <code>GZIPOutputStream</code> * if not created yet. Also sets the "Content-Encoding" header. */ public void writeToGZip(final byte[] b, final int off, final int len) throws IOException { if (gzipstream == null) { gzipstream = new GZIPOutputStream(output); response.setHeader("Content-Encoding", "gzip"); } gzipstream.write(b, off, len); }
public static byte[] toCompressedJsonConf(Map<String, Object> topoConf) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter out = new OutputStreamWriter(new GZIPOutputStream(bos)); JSONValue.writeJSONString(topoConf, out); out.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
public static String encodeBase64ZippedString( String in ) throws IOException { Charset charset = Charset.forName( Const.XML_ENCODING ); ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 ); try ( Base64OutputStream base64OutputStream = new Base64OutputStream( baos ); GZIPOutputStream gzos = new GZIPOutputStream( base64OutputStream ) ) { gzos.write( in.getBytes( charset ) ); } return baos.toString(); } }
public static void main(String[] args) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); Throwable baosEx = null; try { final GZIPOutputStream gzip = new GZIPOutputStream(baos); Throwable gzipEx = null; try { if (baosEx != null) { try { baos.close(); } catch (Throwable t) { baosEx.addSuppressed(t); baos.close();
private void flushToGZIP() throws IOException { if (stream instanceof ByteArrayOutputStream) { // indication de compression, // on utilise setHeader et non addHeader pour être compatible avec PJL compression filter // en particulier dans le plugin grails vis à vis de l'autre plugin grails UiPerformance response.setHeader("Content-Encoding", "gzip"); response.setHeader("Vary", "Accept-Encoding"); // make new gzip stream using the response output stream (content-encoding is in constructor) final GZIPOutputStream gzipstream = new GZIPOutputStream(response.getOutputStream(), compressionThreshold); // get existing bytes final byte[] bytes = ((ByteArrayOutputStream) stream).toByteArray(); gzipstream.write(bytes); // we are no longer buffering, send content via gzipstream stream = gzipstream; } }
public static String saveSession(String sessionName) throws IOException { StringBuilder builder = new StringBuilder(); String filename = mStoragePath + '/' + sessionName + ".dss", session; builder.append(SESSION_MAGIC + "\n"); // skip the network target synchronized (mTargets) { builder.append(mTargets.size() - 1).append("\n"); for (Target target : mTargets) { if (target.getType() != Target.Type.NETWORK) target.serialize(builder); } } session = builder.toString(); FileOutputStream ostream = new FileOutputStream(filename); GZIPOutputStream gzip = new GZIPOutputStream(ostream); gzip.write(session.getBytes()); gzip.close(); mSessionName = sessionName; return filename; }
private byte[] getPlaintextStorageXml() throws Exception { ByteArrayOutputStream plaintextByteArrayOutputStream = new ByteArrayOutputStream(); DataOutputStream plaintextOutputStream = new DataOutputStream(plaintextByteArrayOutputStream); plaintextOutputStream.writeInt(transferSettings.getType().getBytes().length); plaintextOutputStream.write(transferSettings.getType().getBytes()); GZIPOutputStream plaintextGzipOutputStream = new GZIPOutputStream(plaintextOutputStream); new Persister(new Format(0)).write(transferSettings, plaintextGzipOutputStream); plaintextGzipOutputStream.close(); return plaintextByteArrayOutputStream.toByteArray(); } }
private static byte[] compressIfNecessary(byte[] dataBytes) throws IOException { // enable compression when data is larger than 1KB int maxDataSizeUncompress = 1024; if (dataBytes.length < maxDataSizeUncompress) { return dataBytes; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(dataBytes); gzip.close(); return out.toByteArray(); }
@Override public byte[] compress(String text) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { OutputStream out = new GZIPOutputStream(baos); out.write(text.getBytes("UTF-8")); out.close(); } catch (IOException e) { throw new RuntimeException(e); } return baos.toByteArray(); }
/** * Base64 encodes then zips a byte array into a compressed string * * @param src the source byte array * @return a compressed, base64 encoded string * @throws IOException */ public static String encodeBase64Zipped( byte[] src ) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 ); try ( Base64OutputStream base64OutputStream = new Base64OutputStream( baos ); GZIPOutputStream gzos = new GZIPOutputStream( base64OutputStream ) ) { gzos.write( src ); } return baos.toString(); } }
public Gzipper() throws RuntimeException { try { baos = new ByteArrayOutputStream(256); gzos = new GZIPOutputStream(baos, true); } catch (IOException e) { throw new RuntimeException("Error finalizing the GzipOutputstream", e); } }
/** Compresses the given file and removes the uncompressed file @param file name of the zip file @return Size of the zip file */ private long zipFile(String file) throws IOException { cleanDiskSpace(); if (hasSpace()) { String zipFile = file + ".gz"; FileInputStream is = new FileInputStream(file); GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(zipFile)); byte[] buffer = new byte[1024]; int readSize; while ((readSize = is.read(buffer)) != -1) gout.write(buffer, 0, readSize); is.close(); gout.flush(); gout.close(); //delete uncompressed file new File(file).delete(); return (new File(zipFile).length()); } else { logger.error("No space available to create zip files after attempting clean up"); return 0; } }
public static void compressGZIP(File input, File output) throws IOException { try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) { try (FileInputStream in = new FileInputStream(input)) { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } } }
protected void processDynamic(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { ContentCachingResponseWrapper wrapper = new ContentCachingResponseWrapper(response); chain.doFilter(request, wrapper); FastByteArrayOutputStream baos = new FastByteArrayOutputStream(1024); GZIPOutputStream zip = new GZIPOutputStream(baos); StreamUtils.copy(wrapper.getContentInputStream(), zip); zip.close(); response.addHeader("Content-Encoding", "gzip"); response.setContentLength(baos.size()); StreamUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream()); }
public static byte[] compressGZIPAsString(String content, Charset charset) throws IOException { if (content == null || content.length() == 0) { return null; } GZIPOutputStream gzip; ByteArrayOutputStream out = new ByteArrayOutputStream(); gzip = new GZIPOutputStream(out); gzip.write(content.getBytes(charset)); gzip.close(); return out.toByteArray(); }
@Override public byte[] serialize(Object object) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(gos); oos.writeObject(object); oos.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
public String save(String sessionName) throws IOException { StringBuilder builder = new StringBuilder(); String filename = System.getStoragePath() + '/' + sessionName + ".dhs", buffer = null; builder.append( System.SESSION_MAGIC + "\n"); builder.append(mUserName == null ? "null" : mUserName).append("\n"); builder.append(mHTTPS).append("\n"); builder.append(mAddress).append("\n"); builder.append(mDomain).append("\n"); builder.append(mUserAgent).append("\n"); builder.append(mCookies.size()).append("\n"); for(HttpCookie cookie : mCookies.values()){ builder .append(cookie.getName()).append( "=" ).append(cookie.getValue()) .append( "; domain=").append(cookie.getDomain()) .append( "; path=/" ) .append( mHTTPS ? ";secure" : "" ) .append("\n"); } buffer = builder.toString(); FileOutputStream ostream = new FileOutputStream(filename); GZIPOutputStream gzip = new GZIPOutputStream(ostream); gzip.write(buffer.getBytes()); gzip.close(); return filename; }
public static void compressGZIP(File input, File output) throws IOException { try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) { try (FileInputStream in = new FileInputStream(input)) { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } } }