List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream
public GZIPOutputStream(OutputStream out) throws IOException
From source file:org.esigate.DriverTest.java
public void testGzipErrorPage() throws Exception { Properties properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost"); HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error"); response.addHeader("Content-type", "Text/html;Charset=UTF-8"); response.addHeader("Content-encoding", "gzip"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); byte[] uncompressedBytes = "".getBytes("UTF-8"); gzos.write(uncompressedBytes, 0, uncompressedBytes.length); gzos.close();// w ww .j a va 2 s .co m byte[] compressedBytes = baos.toByteArray(); ByteArrayEntity httpEntity = new ByteArrayEntity(compressedBytes); httpEntity.setContentType("Text/html;Charset=UTF-8"); httpEntity.setContentEncoding("gzip"); response.setEntity(httpEntity); mockConnectionManager.setResponse(response); Driver driver = createMockDriver(properties, mockConnectionManager); CloseableHttpResponse driverResponse; try { driverResponse = driver.proxy("/", request.build()); fail("We should get an HttpErrorPage"); } catch (HttpErrorPage e) { driverResponse = e.getHttpResponse(); } assertEquals("", HttpResponseUtils.toString(driverResponse)); }
From source file:org.apache.sling.discovery.base.connectors.ping.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;// www. j a va 2 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); } final HttpClientContext clientContext = HttpClientContext.create(); final CloseableHttpClient httpClient = createHttpClient(); final HttpPut putRequest = new HttpPut(uri); // setting the connection timeout (idle connection, configured in seconds) putRequest.setConfig( RequestConfig.custom().setConnectTimeout(1000 * config.getSocketConnectTimeout()).build()); Announcement resultingAnnouncement = null; try { String userInfo = connectorUrl.getUserInfo(); if (userInfo != null) { Credentials c = new UsernamePasswordCredentials(userInfo); clientContext.getCredentialsProvider().setCredentials( new AuthScope(putRequest.getURI().getHost(), putRequest.getURI().getPort()), c); } Announcement topologyAnnouncement = new Announcement(clusterViewService.getSlingId()); topologyAnnouncement.setServerInfo(serverInfo); final ClusterView clusterView; try { clusterView = clusterViewService.getLocalClusterView(); } catch (UndefinedClusterViewException e) { // SLING-5030 : then we cannot ping logger.warn("ping: no clusterView available at the moment, cannot ping others now: " + e); return; } 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 = clusterView.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(putRequest, p); if (config.isGzipConnectorRequestsEnabled()) { // tell the server that the content is gzipped: putRequest.addHeader("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(); putRequest.setEntity(new ByteArrayEntity(gzippedEncodedJson, ContentType.APPLICATION_JSON)); lastRequestEncoding = "gzip"; } else { // otherwise plaintext: final StringEntity plaintext = new StringEntity(p, "UTF-8"); plaintext.setContentType(ContentType.APPLICATION_JSON.getMimeType()); putRequest.setEntity(plaintext); lastRequestEncoding = "plaintext"; } // independent of request-gzipping, we do accept the response to be gzipped, // so indicate this to the server: putRequest.addHeader("Accept-Encoding", "gzip"); final CloseableHttpResponse response = httpClient.execute(putRequest, clientContext); if (logger.isDebugEnabled()) { logger.debug("ping: done. code=" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase()); } lastStatusCode = response.getStatusLine().getStatusCode(); lastResponseEncoding = null; if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) { final Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) { lastResponseEncoding = "gzip"; } else { lastResponseEncoding = "plaintext"; } final String responseBody = requestValidator.decodeMessage(putRequest.getURI().getPath(), response); // 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 (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 { putRequest.releaseConnection(); lastInheritedAnnouncement = resultingAnnouncement; lastPingedAt = System.currentTimeMillis(); try { httpClient.close(); } catch (IOException e) { logger.error("disconnect: could not close httpClient: " + e, e); } } }
From source file:net.sf.jvifm.model.FileModelManager.java
public void tar(String filename, String[] paths, String compressMethod) throws Exception { File tarFile = new File(filename); FileOutputStream fo = new FileOutputStream(tarFile); TarOutputStream to = null;/*from w w w . j a va 2 s . com*/ if (compressMethod == null) { to = new TarOutputStream(fo); } else if (compressMethod.equals("gz")) { to = new TarOutputStream(new GZIPOutputStream(fo)); } else if (compressMethod.equals("bz2")) { fo.write('B'); fo.write('Z'); to = new TarOutputStream(new CBZip2OutputStream(fo)); } for (int i = 0; i < paths.length; i++) { File file = new File(paths[i]).getAbsoluteFile(); prefix = file.getParent(); if (!prefix.endsWith(File.separator)) prefix = prefix + File.separator; doTar(to, file); } to.close(); notifyAddFile(tarFile); }
From source file:org.vaadin.addon.JFreeChartWrapper.java
@Override public Resource getSource() { if (res == null) { StreamSource streamSource = new StreamResource.StreamSource() { private ByteArrayInputStream bytestream = null; ByteArrayInputStream getByteStream() { if (chart != null && bytestream == null) { int widht = getGraphWidth(); int height = getGraphHeight(); if (mode == RenderingMode.SVG) { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = null; try { docBuilder = docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e1) { throw new RuntimeException(e1); }// w w w .j av a2 s. co m Document document = docBuilder.newDocument(); Element svgelem = document.createElement("svg"); document.appendChild(svgelem); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // draw the chart in the SVG generator chart.draw(svgGenerator, new Rectangle(widht, height)); Element el = svgGenerator.getRoot(); el.setAttributeNS(null, "viewBox", "0 0 " + widht + " " + height + ""); el.setAttributeNS(null, "style", "width:100%;height:100%;"); el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio()); // Write svg to buffer ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream(); Writer out; try { OutputStream outputStream = gzipEnabled ? new GZIPOutputStream(baoutputStream) : baoutputStream; out = new OutputStreamWriter(outputStream, "UTF-8"); /* * don't use css, FF3 can'd deal with the result * perfectly: wrong font sizes */ boolean useCSS = false; svgGenerator.stream(el, out, useCSS, false); outputStream.flush(); outputStream.close(); bytestream = new ByteArrayInputStream(baoutputStream.toByteArray()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SVGGraphics2DIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { // Draw png to bytestream try { byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(widht, height)); bytestream = new ByteArrayInputStream(bytes); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else { bytestream.reset(); } return bytestream; } @Override public InputStream getStream() { return getByteStream(); } }; res = new StreamResource(streamSource, String.format("graph%d", System.currentTimeMillis())) { @Override public int getBufferSize() { if (getStreamSource().getStream() != null) { try { return getStreamSource().getStream().available(); } catch (IOException e) { return 0; } } else { return 0; } } @Override public long getCacheTime() { return 0; } @Override public String getFilename() { if (mode == RenderingMode.PNG) { return super.getFilename() + ".png"; } else { return super.getFilename() + (gzipEnabled ? ".svgz" : ".svg"); } } @Override public DownloadStream getStream() { DownloadStream downloadStream = new DownloadStream(getStreamSource().getStream(), getMIMEType(), getFilename()); if (gzipEnabled && mode == RenderingMode.SVG) { downloadStream.setParameter("Content-Encoding", "gzip"); } return downloadStream; } @Override public String getMIMEType() { if (mode == RenderingMode.PNG) { return "image/png"; } else { return "image/svg+xml"; } } }; } return res; }
From source file:org.wso2.esb.integration.common.utils.clients.SimpleHttpClient.java
/** * Send a HTTP PUT request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response//from www. j a v a 2 s.co m * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPut(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPut(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes()); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
From source file:edu.cornell.med.icb.goby.modes.SplitTranscriptsMode.java
/** * Perform the split transcripts mode./*from w w w. jav a 2 s . c om*/ * * @throws IOException error reading / writing */ @Override public void execute() throws IOException { // Load the gene to transcripts file if (!config.validate()) { throw new IOException("Invalid SplitTranscripts configuration"); } final GeneTranscriptRelationships gtr = new GeneTranscriptRelationships(); final IndexedIdentifier transcriptIdents = new IndexedIdentifier(); final Int2ObjectMap<MutableString> transcriptIndexToIdMap = new Int2ObjectOpenHashMap<MutableString>(); final List<FastXEntry> fastxEntries = new LinkedList<FastXEntry>(); // // Pass through the file once to collect the transcript - gene relationships // int entryCount = 0; try { for (final FastXEntry entry : new FastXReader(config.getInputFile())) { entryCount++; parseHeader(entry.getEntryHeader()); final MutableString transcriptId = transcriptHeader.get("transcriptId"); final MutableString geneId = transcriptHeader.get("geneId"); final int transcriptIndex = transcriptIdents.registerIdentifier(transcriptId); gtr.addRelationship(geneId, transcriptIndex); transcriptIndexToIdMap.put(transcriptIndex, transcriptId); fastxEntries.add(entry.clone()); } } catch (CloneNotSupportedException e) { LOG.error("Couldn't clone for some reason", e); throw new GobyRuntimeException("Couldn't clone for some reason", e); } LOG.info("Loading map of genes-transcripts complete."); // // Scan through the transcript-gene relationships to determine which // transcript id goes into which file // final Int2IntMap transcriptIndex2FileIndex = new Int2IntOpenHashMap(); final String configOutputFilename = config.getOutputBase() + ".config"; final String configOutputPath = FilenameUtils.getFullPath(configOutputFilename); if (StringUtils.isNotBlank(configOutputPath)) { LOG.info("Creating output directory: " + configOutputPath); FileUtils.forceMkdir(new File(configOutputPath)); } PrintWriter configOutput = null; try { configOutput = new PrintWriter(configOutputFilename); configOutput.println("Ensembl Gene ID\tEnsembl Transcript ID"); final Int2IntMap fileIndex2NumberOfEntries = new Int2IntOpenHashMap(); fileIndex2NumberOfEntries.defaultReturnValue(0); transcriptIndex2FileIndex.defaultReturnValue(-1); final int initialNumberOfFiles = getNumberOfFiles(gtr, transcriptIndex2FileIndex); for (int geneIndex = 0; geneIndex < gtr.getNumberOfGenes(); geneIndex++) { final MutableString geneId = gtr.getGeneId(geneIndex); final IntSet transcriptIndices = gtr.getTranscriptSet(geneIndex); int fileNum = 0; for (final int transcriptIndex : transcriptIndices) { if (transcriptIndex2FileIndex.get(transcriptIndex) != -1) { LOG.warn("Skipping repeated transcriptIndex: " + transcriptIndex); continue; } final int maxEntriesPerFile = config.getMaxEntriesPerFile(); final int numberOfEntriesInOriginalBucket = fileIndex2NumberOfEntries.get(fileNum); final int adjustedFileIndex = fileNum + initialNumberOfFiles * (numberOfEntriesInOriginalBucket / maxEntriesPerFile); transcriptIndex2FileIndex.put(transcriptIndex, adjustedFileIndex); fileIndex2NumberOfEntries.put(fileNum, fileIndex2NumberOfEntries.get(fileNum) + 1); final MutableString transcriptId = transcriptIndexToIdMap.get(transcriptIndex); configOutput.printf("%s\t%s%n", geneId, transcriptId); fileNum++; } } } finally { IOUtils.closeQuietly(configOutput); } final int numFiles = getFileIndices(transcriptIndex2FileIndex).size(); if (LOG.isInfoEnabled()) { LOG.info(NumberFormat.getInstance().format(entryCount) + " entries will be written to " + numFiles + " files"); final int maxEntriesPerFile = config.getMaxEntriesPerFile(); if (maxEntriesPerFile < Integer.MAX_VALUE) { LOG.info("Each file will contain at most " + maxEntriesPerFile + " entries"); } } // formatter for uniquely numbering files each with the same number of digits final NumberFormat fileNumberFormatter = getNumberFormatter(numFiles - 1); final ProgressLogger progressLogger = new ProgressLogger(); progressLogger.expectedUpdates = entryCount; progressLogger.itemsName = "entries"; progressLogger.start(); // Write each file one at a time rather than in the order they appear in the input file // to avoid the issue of having too many streams open at the same or continually opening // and closing streams which is quite costly. We could store the gene/transcripts in // memory and then just write the files at the end but that could be worse. for (final int fileIndex : getFileIndices(transcriptIndex2FileIndex)) { final String filename = config.getOutputBase() + "." + fileNumberFormatter.format(fileIndex) + ".fa.gz"; PrintStream printStream = null; try { // each file is compressed printStream = new PrintStream(new GZIPOutputStream(new FileOutputStream(filename))); // // Read through the input file get the actual sequence information // final Iterator<FastXEntry> entries = fastxEntries.iterator(); while (entries.hasNext()) { final FastXEntry entry = entries.next(); parseHeader(entry.getEntryHeader()); final MutableString transcriptId = transcriptHeader.get("transcriptId"); final MutableString geneId = transcriptHeader.get("geneId"); final int transcriptIndex = transcriptIdents.getInt(transcriptId); final int transcriptFileIndex = transcriptIndex2FileIndex.get(transcriptIndex); if (transcriptFileIndex == fileIndex) { printStream.print(entry.getHeaderSymbol()); printStream.print(transcriptId); printStream.print(" gene:"); printStream.println(geneId); printStream.println(entry.getEntrySansHeader()); entries.remove(); progressLogger.lightUpdate(); } } } finally { IOUtils.closeQuietly(printStream); } } assert progressLogger.count == entryCount : "Some entries were not processed!"; progressLogger.done(); }
From source file:com.github.jillesvangurp.persistentcachingmap.PersistentCachingMap.java
private static BufferedWriter gzipFileWriter(File file) throws IOException { return new BufferedWriter( new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(file)), Charset.forName("utf-8")), 64 * 1024);/*from w w w.j a va 2 s. com*/ }
From source file:net.sf.ehcache.constructs.web.PageInfoTest.java
/** * @param ungzipped the bytes to be gzipped * @return gzipped bytes/*from w w w . ja va 2 s . c om*/ */ private byte[] gzip(byte[] ungzipped) throws IOException { final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes); gzipOutputStream.write(ungzipped); gzipOutputStream.close(); return bytes.toByteArray(); }
From source file:com.nridge.core.base.std.FilUtl.java
/** * Compresses the input file into a GZIP file container. * * @param anInPathFileName The file name to be compressed. * @param aGzipPathFileName The file name of the ZIP container. * * @throws IOException Related to opening the file streams and * related read/write operations.//from www. j av a 2 s. c om */ public static void gzipFile(String anInPathFileName, String aGzipPathFileName) throws IOException { int byteCount; FileInputStream fileInputStream = new FileInputStream(anInPathFileName); FileOutputStream fileOutputStream = new FileOutputStream(aGzipPathFileName); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream); byte[] ioBuf = new byte[FILE_IO_BUFFER_SIZE]; byteCount = fileInputStream.read(ioBuf); while (byteCount > 0) { gzipOutputStream.write(ioBuf, 0, byteCount); byteCount = fileInputStream.read(ioBuf); } gzipOutputStream.close(); fileOutputStream.close(); fileInputStream.close(); }
From source file:com.facebook.presto.bloomfilter.BloomFilter.java
public static byte[] compress(byte[] b) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(b);/*from w w w . java2s.c o m*/ gzip.close(); return out.toByteArray(); }