List of usage examples for java.util.zip GZIPOutputStream write
public synchronized void write(byte[] buf, int off, int len) throws IOException
From source file:org.loadosophia.client.LoadosophiaAPIClient.java
private File gzipFile(File src) throws IOException { // Never try to make it stream-like on the fly, because content-length // still required // Create the GZIP output stream String outFilename = src.getAbsolutePath() + ".gz"; notifier.notifyAbout("Gzipping " + src.getAbsolutePath()); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outFilename)); // Open the input file FileInputStream in = new FileInputStream(src); // Transfer bytes from the input file to the GZIP output stream byte[] buf = new byte[1024]; int len;/*from ww w . j av a 2s . co m*/ while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); // Complete the GZIP file out.finish(); out.close(); src.delete(); return new File(outFilename); }
From source file:org.kairosdb.datastore.remote.RemoteDatastore.java
/** Compresses the given file and removes the uncompressed file @param file/* w ww . ja v a 2 s. c o m*/ @return Size of the zip file */ private long zipFile(String file) throws IOException { String zipFile = file + ".gz"; FileInputStream is = new FileInputStream(file); GZIPOutputStream gout = new GZIPOutputStream(new FileOutputStream(zipFile)); byte[] buffer = new byte[1024]; int readSize = 0; 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()); }
From source file:com.hypersocket.netty.HttpRequestDispatcherHandler.java
private InputStream processContent(HttpRequestServletWrapper servletRequest, HttpResponseServletWrapper servletResponse, String acceptEncodings) { InputStream writer = (InputStream) servletRequest.getAttribute(CONTENT_INPUTSTREAM); if (writer != null) { if (log.isDebugEnabled()) { log.debug("Response for " + servletRequest.getRequestURI() + " will be chunked"); }//from w ww.j av a 2s . c o m servletResponse.setChunked(true); servletResponse.removeHeader(HttpHeaders.CONTENT_LENGTH); servletResponse.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked"); return writer; } if (servletResponse.getContent().readableBytes() > 0) { ChannelBuffer buffer = servletResponse.getContent(); boolean doGzip = false; if (servletResponse.getNettyResponse().getHeader("Content-Encoding") == null) { if (acceptEncodings != null) { doGzip = acceptEncodings.indexOf("gzip") > -1; } if (doGzip) { try { ByteArrayOutputStream gzipped = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(gzipped); gzip.write(buffer.array(), 0, buffer.readableBytes()); gzip.finish(); buffer = ChannelBuffers.wrappedBuffer(gzipped.toByteArray()); servletResponse.setHeader("Content-Encoding", "gzip"); } catch (IOException e) { log.error("Failed to gzip response", e); } } } servletResponse.getNettyResponse().setContent(buffer); servletResponse.setHeader("Content-Length", String.valueOf(buffer.readableBytes())); } else { servletResponse.setHeader("Content-Length", "0"); } return null; }
From source file:com.l2jfree.security.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>/* ww w . j a va2s .co m*/ * Valid options: * * <pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: * <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * * @param source * The data to convert * @param off * Offset in array where conversion should begin * @param len * Length of data to convert * @param options * Specified options * @return String * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { IOUtils.closeQuietly(gzos); IOUtils.closeQuietly(b64os); IOUtils.closeQuietly(baos); } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch // end else: don't compress }
From source file:edu.umn.cs.spatialHadoop.nasa.HDFRasterLayer.java
@Override public void write(DataOutput out) throws IOException { super.write(out); out.writeLong(timestamp);// w w w. ja va 2 s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); ByteBuffer bbuffer = ByteBuffer.allocate(getHeight() * 2 * 8 + 8); bbuffer.putInt(getWidth()); bbuffer.putInt(getHeight()); gzos.write(bbuffer.array(), 0, bbuffer.position()); for (int x = 0; x < getWidth(); x++) { bbuffer.clear(); for (int y = 0; y < getHeight(); y++) { bbuffer.putLong(sum[x][y]); bbuffer.putLong(count[x][y]); } gzos.write(bbuffer.array(), 0, bbuffer.position()); } gzos.close(); byte[] serializedData = baos.toByteArray(); out.writeInt(serializedData.length); out.write(serializedData); }
From source file:com.chenshu.compress.CompressOldTest.java
@Benchmark public int jdkGzip2Compress() { ByteArrayInputStream bin = null; ByteArrayOutputStream bout = null; GZIPOutputStream gzout = null; try {//from w w w . j a va 2s . co m bin = new ByteArrayInputStream(data); bout = new ByteArrayOutputStream(data.length); gzout = new GZIPOutputStream(bout) { { def.setLevel(level); } }; int count; byte ret[] = new byte[1024]; while ((count = bin.read(ret, 0, 1024)) != -1) { gzout.write(ret, 0, count); } gzout.finish(); gzout.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (gzout != null) { try { gzout.close(); } catch (IOException e) { e.printStackTrace(); } } if (bout != null) { try { bout.close(); } catch (IOException e) { e.printStackTrace(); } } } byte[] bs = bout.toByteArray(); return bs.length; }
From source file:org.nuxeo.client.internals.util.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from w ww .j a v a 2 s . com * Valid options: * * <pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @param options Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { IOUtils.closeQuietly(gzos); IOUtils.closeQuietly(b64os); IOUtils.closeQuietly(baos); } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:org.nuxeo.ecm.automation.client.jaxrs.util.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>// w w w. j ava 2 s . c o m * Valid options: * * <pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @param options Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { IOUtils.closeQuietly(gzos); IOUtils.closeQuietly(b64os); IOUtils.closeQuietly(baos); } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:org.mule.util.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>/*from w w w .jav a 2 s . c om*/ * Valid options: * * <pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: * <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @param options Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) throws IOException { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { ByteArrayOutputStream baos = null; GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new ByteArrayOutputStream(4096); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.finish(); gzos.close(); } // end try catch (IOException e) { throw e; } // end catch finally { IOUtils.closeQuietly(gzos); IOUtils.closeQuietly(b64os); IOUtils.closeQuietly(baos); } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for // padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New // lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from w w w . j a v a 2 s .c om * Valid options:<pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> * * * @param source The data to convert * @param off Offset in array where conversion should begin * @param len Length of data to convert * @param options Specified options * @see Base64#GZIP * @see Base64#DONT_BREAK_LINES * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { try { gzos.close(); } catch (Exception e) { } try { b64os.close(); } catch (Exception e) { } try { baos.close(); } catch (Exception e) { } } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(source, d + off, 3, outBuff, e); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }