List of usage examples for java.util.zip GZIPOutputStream close
public void close() throws IOException
From source file:org.pentaho.amazon.client.impl.S3ClientImplTest.java
private void createGzArchive() throws Exception { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; GZIPOutputStream gzipOutputStream = null; try {//w w w.j ava 2 s. c o m fileInputStream = new FileInputStream(logFileName); fileOutputStream = new FileOutputStream(gzArchName); gzipOutputStream = new GZIPOutputStream(fileOutputStream); byte[] buffer = new byte[1024]; int len; while ((len = fileInputStream.read(buffer)) != -1) { gzipOutputStream.write(buffer, 0, len); } } finally { gzipOutputStream.close(); fileOutputStream.close(); fileInputStream.close(); } }
From source file:org.kairosdb.datastore.remote.RemoteDatastore.java
/** Compresses the given file and removes the uncompressed file @param file//from w ww .jav a2s. co 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: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 a v a 2 s. c om 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:com.kloudtek.buildmagic.tools.createinstaller.deb.CreateDebTask.java
private void prepare() throws IOException { // validate parameters if (name == null || name.trim().length() == 0) { throw new BuildException("name attribute is missing or invalid"); }/*from www . j ava 2s . c o m*/ if (version == null || version.trim().length() == 0) { throw new BuildException("version attribute is missing or invalid"); } if (arch == null || arch.trim().length() == 0) { throw new BuildException("arch attribute is missing or invalid"); } if (maintName == null || maintName.trim().length() == 0) { throw new BuildException("maintName attribute is missing"); } if (maintEmail == null || maintEmail.trim().length() == 0) { throw new BuildException("maintEmail attribute is missing"); } if (priority != null && !PRIORITIES.contains(priority.toLowerCase())) { log("Priority '" + priority + "' isn't recognized as a valid value", Project.MSG_WARN); } if (control != null) { for (final TemplateEntry entry : control.getTemplateEntries()) { if (entry.getPackageName() == null) { entry.setPackageName(name); } templateEntries.add(entry); } } // generate copyright file if (copyrights.isEmpty()) { if (license == null) { throw new BuildException("No license has been specified"); } if (licenseFile == null) { licenseFile = new File("license.txt"); } copyrights.add(new Copyright(license, licenseFile)); } copyrights.validate(); copyright = dataBufferManager.createBuffer(); copyrights.write(copyright, this); // generate changelog changelog = new Changelog(name); Changelog.Entry entry = changelog.createEntry(); changelog.setDistributions("main"); // TODO entry.setMaintName(maintName); entry.setMaintEmail(maintEmail); entry.setChanges("Released"); entry.setVersion(version); entry.setDate(new Date()); changelog.validate(); changelogData = changelog.export(Changelog.Type.DEBIAN); ByteArrayOutputStream changelogBuf = new ByteArrayOutputStream(); GZIPOutputStream changelogGzipBuf = new GZIPOutputStream(changelogBuf); changelogGzipBuf.write(changelogData.getBytes()); changelogGzipBuf.finish(); changelogGzipBuf.close(); changelogDataGzipped = changelogBuf.toByteArray(); }
From source file:org.nuxeo.client.internals.util.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from ww w.jav a 2s . 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 (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.apache.hadoop.io.compress.TestCodec.java
public void testGzipCompatibility() throws IOException { Random r = new Random(); long seed = r.nextLong(); r.setSeed(seed);//from www . j a v a 2 s . co m LOG.info("seed: " + seed); DataOutputBuffer dflbuf = new DataOutputBuffer(); GZIPOutputStream gzout = new GZIPOutputStream(dflbuf); byte[] b = new byte[r.nextInt(128 * 1024 + 1)]; r.nextBytes(b); gzout.write(b); gzout.close(); DataInputBuffer gzbuf = new DataInputBuffer(); gzbuf.reset(dflbuf.getData(), dflbuf.getLength()); Configuration conf = new Configuration(); conf.setBoolean("hadoop.native.lib", false); CompressionCodec codec = ReflectionUtils.newInstance(GzipCodec.class, conf); Decompressor decom = codec.createDecompressor(); assertNotNull(decom); assertEquals(BuiltInGzipDecompressor.class, decom.getClass()); InputStream gzin = codec.createInputStream(gzbuf, decom); dflbuf.reset(); IOUtils.copyBytes(gzin, dflbuf, 4096); final byte[] dflchk = Arrays.copyOf(dflbuf.getData(), dflbuf.getLength()); assertTrue(java.util.Arrays.equals(b, dflchk)); }
From source file:org.nuxeo.ecm.automation.client.jaxrs.util.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>/*from www .ja va 2s. 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 { 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.apache.sling.discovery.impl.topology.connector.TopologyConnectorClient.java
/** ping the server and pass the announcements between the two **/ void ping(final boolean force) { if (autoStopped) { // then we suppress any further pings! logger.debug("ping: autoStopped=true, hence suppressing any further pings."); return;/* w w w .j a v a2 s . c om*/ } if (force) { backoffPeriodEnd = -1; } else if (backoffPeriodEnd > 0) { if (System.currentTimeMillis() < backoffPeriodEnd) { logger.debug("ping: not issueing a heartbeat due to backoff instruction from peer."); return; } else { logger.debug("ping: backoff period ended, issuing another ping now."); } } final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json"; if (logger.isDebugEnabled()) { logger.debug("ping: connectorUrl=" + connectorUrl + ", complete uri=" + uri); } HttpClient httpClient = new HttpClient(); final PutMethod method = new PutMethod(uri); Announcement resultingAnnouncement = null; try { String userInfo = connectorUrl.getUserInfo(); if (userInfo != null) { Credentials c = new UsernamePasswordCredentials(userInfo); httpClient.getState() .setCredentials(new AuthScope(method.getURI().getHost(), method.getURI().getPort()), c); } Announcement topologyAnnouncement = new Announcement(clusterViewService.getSlingId()); topologyAnnouncement.setServerInfo(serverInfo); final ClusterView clusterView = clusterViewService.getClusterView(); topologyAnnouncement.setLocalCluster(clusterView); if (force) { logger.debug("ping: sending a resetBackoff"); topologyAnnouncement.setResetBackoff(true); } announcementRegistry.addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() { public boolean accept(final String receivingSlingId, final Announcement announcement) { // filter out announcements that are of old cluster instances // which I dont really have in my cluster view at the moment final Iterator<InstanceDescription> it = clusterViewService.getClusterView().getInstances() .iterator(); while (it.hasNext()) { final InstanceDescription instance = it.next(); if (instance.getSlingId().equals(receivingSlingId)) { // then I have the receiving instance in my cluster view // all fine then return true; } } // looks like I dont have the receiving instance in my cluster view // then I should also not propagate that announcement anywhere return false; } }); final String p = requestValidator.encodeMessage(topologyAnnouncement.asJSON()); if (logger.isDebugEnabled()) { logger.debug("ping: topologyAnnouncement json is: " + p); } requestValidator.trustMessage(method, p); if (config.isGzipConnectorRequestsEnabled()) { // tell the server that the content is gzipped: method.addRequestHeader("Content-Encoding", "gzip"); // and gzip the body: final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final GZIPOutputStream gzipOut = new GZIPOutputStream(baos); gzipOut.write(p.getBytes("UTF-8")); gzipOut.close(); final byte[] gzippedEncodedJson = baos.toByteArray(); method.setRequestEntity(new ByteArrayRequestEntity(gzippedEncodedJson, "application/json")); lastRequestEncoding = "gzip"; } else { // otherwise plaintext: method.setRequestEntity(new StringRequestEntity(p, "application/json", "UTF-8")); lastRequestEncoding = "plaintext"; } // independent of request-gzipping, we do accept the response to be gzipped, // so indicate this to the server: method.addRequestHeader("Accept-Encoding", "gzip"); DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(0, false); httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); httpClient.getHttpConnectionManager().getParams() .setConnectionTimeout(1000 * config.getConnectionTimeout()); httpClient.getHttpConnectionManager().getParams().setSoTimeout(1000 * config.getSoTimeout()); method.getParams().setSoTimeout(1000 * config.getSoTimeout()); httpClient.executeMethod(method); if (logger.isDebugEnabled()) { logger.debug("ping: done. code=" + method.getStatusCode() + " - " + method.getStatusText()); } lastStatusCode = method.getStatusCode(); lastResponseEncoding = null; if (method.getStatusCode() == HttpServletResponse.SC_OK) { final Header contentEncoding = method.getResponseHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) { lastResponseEncoding = "gzip"; } else { lastResponseEncoding = "plaintext"; } String responseBody = requestValidator.decodeMessage(method); // limiting to 16MB, should be way enough if (logger.isDebugEnabled()) { logger.debug("ping: response body=" + responseBody); } if (responseBody != null && responseBody.length() > 0) { Announcement inheritedAnnouncement = Announcement.fromJSON(responseBody); final long backoffInterval = inheritedAnnouncement.getBackoffInterval(); if (backoffInterval > 0) { // then reset the backoffPeriodEnd: /* minus 1 sec to avoid slipping the interval by a few millis */ this.backoffPeriodEnd = System.currentTimeMillis() + (1000 * backoffInterval) - 1000; logger.debug("ping: servlet instructed to backoff: backoffInterval=" + backoffInterval + ", resulting in period end of " + new Date(backoffPeriodEnd)); } else { logger.debug("ping: servlet did not instruct any backoff-ing at this stage"); this.backoffPeriodEnd = -1; } if (inheritedAnnouncement.isLoop()) { if (logger.isDebugEnabled()) { logger.debug( "ping: connector response indicated a loop detected. not registering this announcement from " + inheritedAnnouncement.getOwnerId()); } if (inheritedAnnouncement.getOwnerId().equals(clusterViewService.getSlingId())) { // SLING-3316 : local-loop detected. Check config to see if we should stop this connector if (config.isAutoStopLocalLoopEnabled()) { inheritedAnnouncement = null; // results in connected -> false and representsloop -> true autoStopped = true; // results in isAutoStopped -> true } } } else { inheritedAnnouncement.setInherited(true); if (announcementRegistry.registerAnnouncement(inheritedAnnouncement) == -1) { if (logger.isDebugEnabled()) { logger.debug( "ping: connector response is from an instance which I already see in my topology" + inheritedAnnouncement); } statusDetails = "receiving side is seeing me via another path (connector or cluster) already (loop)"; return; } } resultingAnnouncement = inheritedAnnouncement; statusDetails = null; } else { statusDetails = "no response body received"; } } else { statusDetails = "got HTTP Status-Code: " + lastStatusCode; } // SLING-2882 : reset suppressPingWarnings_ flag in success case suppressPingWarnings_ = false; } catch (URIException e) { logger.warn("ping: Got URIException: " + e + ", uri=" + uri); statusDetails = e.toString(); } catch (IOException e) { // SLING-2882 : set/check the suppressPingWarnings_ flag if (suppressPingWarnings_) { if (logger.isDebugEnabled()) { logger.debug("ping: got IOException: " + e + ", uri=" + uri); } } else { suppressPingWarnings_ = true; logger.warn("ping: got IOException [suppressing further warns]: " + e + ", uri=" + uri); } statusDetails = e.toString(); } catch (JSONException e) { logger.warn("ping: got JSONException: " + e); statusDetails = e.toString(); } catch (RuntimeException re) { logger.warn("ping: got RuntimeException: " + re, re); statusDetails = re.toString(); } finally { method.releaseConnection(); lastInheritedAnnouncement = resultingAnnouncement; lastPingedAt = System.currentTimeMillis(); } }
From source file:com.rackspacecloud.blueflood.inputs.handlers.HttpHandlerIntegrationTest.java
@Test public void testCompressedRequests() throws Exception { URIBuilder builder = getMetricsURIBuilder().setPath("/v2.0/acTEST/ingest"); HttpPost post = new HttpPost(builder.build()); String content = generateJSONMetricsData(); ByteArrayOutputStream baos = new ByteArrayOutputStream(content.length()); GZIPOutputStream gzipOut = new GZIPOutputStream(baos); gzipOut.write(content.getBytes());//from w ww . ja v a 2 s . c o m gzipOut.close(); ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray()); //Setting the content encoding to gzip entity.setContentEncoding("gzip"); baos.close(); post.setEntity(entity); HttpResponse response = client.execute(post); try { assertEquals(200, response.getStatusLine().getStatusCode()); } finally { EntityUtils.consume(response.getEntity()); // Releases connection apparently } }
From source file:rapture.common.client.BaseHttpApi.java
private byte[] getCompressedParams(String request) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = null; try {/*from w ww . j av a 2 s . c o m*/ gzos = new GZIPOutputStream(baos); gzos.write(request.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { return null; } catch (IOException e) { return null; } finally { if (gzos != null) { try { gzos.close(); } catch (IOException ignore) { } } } return baos.toByteArray(); }