List of usage examples for java.util.zip GZIPOutputStream close
public void close() throws IOException
From source file:com.pinterest.terrapin.zookeeper.ViewInfo.java
public byte[] toCompressedJson() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream zipOs = new GZIPOutputStream(out); zipOs.write(this.toJson()); zipOs.close(); byte[] data = out.toByteArray(); return data;//from www. j a v a 2 s . co m }
From source file:com.epam.wilma.test.server.compress.gzip.GzipCompressor.java
/** * Compresses an {@link InputStream} object into gzip. * @param source the input stream that will be compressed. * @return a {@link ByteArrayOutputStream} containing gzipped byte array. *///from w w w. java 2 s .co m public ByteArrayOutputStream compress(final InputStream source) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { GZIPOutputStream gout = new GZIPOutputStream(baos); //... Code to read from your original uncompressed data and write to gout. IOUtils.copy(source, gout); gout.finish(); gout.close(); } catch (IOException e) { throw new SystemException("error", e); } return baos; }
From source file:org.hupo.psi.mi.psicquic.ws.utils.CompressedStreamingOutput.java
@Override public void write(OutputStream output) throws IOException, WebApplicationException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/*from w w w . j ava 2 s. c o m*/ final GZIPOutputStream gzipOs = new GZIPOutputStream(baos); try { IOUtils.copy(is, gzipOs); output.write(baos.toByteArray()); } finally { gzipOs.close(); } } finally { baos.close(); // close inputStream is.close(); } }
From source file:com.nextdoor.bender.ipc.es.ElasticSearchTransporterTest.java
@Test(expected = TransportException.class) public void testGzipErrorsResponse() throws TransportException, IOException { byte[] respPayload = getResponse().getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream os = new GZIPOutputStream(baos); os.write(respPayload);//ww w . j av a2 s.c o m os.close(); byte[] compressedResponse = baos.toByteArray(); HttpClient client = getMockClientWithResponse(compressedResponse, ContentType.DEFAULT_BINARY, HttpStatus.SC_OK); ElasticSearchTransport transport = new ElasticSearchTransport(client, true); try { transport.sendBatch("foo".getBytes()); } catch (Exception e) { assertEquals("es call failed because expected failure", e.getCause().getMessage()); throw e; } }
From source file:org.outerrim.snippad.data.serialize.XmlSerializer.java
/** * @see org.outerrim.snippad.data.serialize.WikiSerializer#save( * org.outerrim.snippad.data.WikiWord,OutputStream) *///from w w w.j av a2s . co m public void save(final WikiWord wiki, final OutputStream output) throws SerializeException { Element root = saveWikiWords(wiki); // Somewhat hackish, but the root element is not a <wiki> element, // so change it and add the XML version attribute root.setName("snippad"); root.setAttribute("version", VERSION); DocType doctype = new DocType(ROOT, SYSTEMID); Document doc = new Document(root, doctype); XMLOutputter outputter = new XMLOutputter(" ", true); try { // Use a ZipOutputStream for writing the file GZIPOutputStream zipOutput = new GZIPOutputStream(new BufferedOutputStream(output)); outputter.output(doc, zipOutput); zipOutput.close(); } catch (IOException e) { throw new SerializeException(e.getMessage(), e); } }
From source file:com.moss.simpledeb.core.DebWriter.java
private byte[] buildGzipTar(List<ArchivePath> paths) throws Exception { byte[] tarData; {// w ww .j a va 2 s . com ByteArrayOutputStream tarOut = new ByteArrayOutputStream(); TarArchiveOutputStream tar = new TarArchiveOutputStream(tarOut); Set<String> writtenPaths = new HashSet<String>(); for (ArchivePath path : paths) { String name = path.entry().getName(); if (writtenPaths.contains(name)) { throw new RuntimeException("Duplicate archive entry: " + name); } writtenPaths.add(name); tar.putArchiveEntry(path.entry()); if (!path.entry().isDirectory()) { InputStream in = path.read(); byte[] buffer = new byte[1024 * 10]; for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) { tar.write(buffer, 0, numRead); } in.close(); } tar.closeArchiveEntry(); } tar.close(); tarData = tarOut.toByteArray(); } byte[] gzipData; { ByteArrayOutputStream gzipOut = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(gzipOut); gzip.write(tarData); gzip.close(); gzipData = gzipOut.toByteArray(); } return gzipData; }
From source file:ca.farrelltonsolar.classic.PVOutputService.java
private byte[] serializeBundle(final Bundle bundle) { byte[] rval = null; final Parcel parcel = Parcel.obtain(); try {/*from w ww . j a v a 2 s . c om*/ parcel.writeBundle(bundle); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(bos)); zos.write(parcel.marshall()); zos.close(); rval = bos.toByteArray(); } catch (IOException ex) { Log.w(getClass().getName(), String.format("serializeBundle failed ex: %s", ex)); } finally { parcel.recycle(); } return rval; }
From source file:com.nextdoor.bender.ipc.http.HttpTransportTest.java
@Test(expected = TransportException.class) public void testGzipErrorsResponse() throws TransportException, IOException { byte[] respPayload = "gzip resp".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream os = new GZIPOutputStream(baos); os.write(respPayload);/*ww w .j a v a2 s. c o m*/ os.close(); byte[] compressedResponse = baos.toByteArray(); HttpClient client = getMockClientWithResponse(compressedResponse, ContentType.DEFAULT_BINARY, HttpStatus.SC_INTERNAL_SERVER_ERROR, true); HttpTransport transport = new HttpTransport(client, "", true, 1, 1); try { transport.sendBatch("foo".getBytes()); } catch (Exception e) { assertEquals("http transport call failed because \"expected failure\" payload response \"gzip resp\"", e.getCause().getMessage()); throw e; } }
From source file:de.iai.ilcd.model.common.XmlFile.java
/** * Compress the content of XML file prior to persist/merge events in order to save database space and be compatible * with MySQL server default configurations * as long as possible (1MB max package size) * //from w w w.ja va 2s. c om * @throws Exception * if anything goes wrong, just in-memory IO operations, should not happen * @see #decompressContent() */ @PrePersist protected void compressContent() throws Exception { if (this.content == null) { this.compressedContent = null; } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzipOut = new GZIPOutputStream(out); gzipOut.write(this.content.getBytes("UTF-8")); gzipOut.flush(); gzipOut.close(); this.compressedContent = out.toByteArray(); } }
From source file:gdt.data.entity.ArchiveHandler.java
private static void compressGzipFile(String tarFile$, String gzipFile$) { try {// w w w.j ava2 s. c o m FileInputStream fis = new FileInputStream(tarFile$); FileOutputStream fos = new FileOutputStream(gzipFile$); GZIPOutputStream gzipOS = new GZIPOutputStream(fos); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { gzipOS.write(buffer, 0, len); } gzipOS.close(); fos.close(); fis.close(); } catch (Exception e) { Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString()); } }