Java Code Examples for java.util.zip.GZIPOutputStream#close()
The following examples show how to use
java.util.zip.GZIPOutputStream#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtilTest.java From hop with Apache License 2.0 | 6 votes |
/** * https://www.securecoding.cert.org/confluence/display/java/IDS12-J.+Perform+lossless+conversion+ * of+String+data+between+differing+character+encodings * * @param in string to encode * @return * @throws IOException */ private String canonicalBase64Encode( String in ) throws IOException { Charset charset = Charset.forName( DEFAULT_ENCODING ); CharsetEncoder encoder = charset.newEncoder(); encoder.reset(); ByteBuffer baosbf = encoder.encode( CharBuffer.wrap( in ) ); byte[] bytes = new byte[ baosbf.limit() ]; baosbf.get( bytes ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream( baos ); gzos.write( bytes ); gzos.close(); String encoded = new String( Base64.encodeBase64( baos.toByteArray() ) ); return encoded; }
Example 2
Source File: StatisticsPublisher.java From micro-integrator with Apache License 2.0 | 6 votes |
/** * Compress the payload * * @param str * @return */ private static String compress(byte[] str) { if (str == null || str.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(str); gzip.close(); return DatatypeConverter.printBase64Binary(out.toByteArray()); } catch (IOException e) { log.error("Unable to compress data", e); } return null; }
Example 3
Source File: LoadosophiaAPIClient.java From jmeter-bzm-plugins with Apache License 2.0 | 6 votes |
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), 1024 * 8, true); // Open the input file FileInputStream in = new FileInputStream(src); // Transfer bytes from the input file to the GZIP output stream byte[] buf = new byte[10240]; int len; 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); }
Example 4
Source File: BurstCryptoImpl.java From burstkit4j with Apache License 2.0 | 6 votes |
private BurstEncryptedMessage encryptPlainMessage(byte[] message, boolean isText, byte[] myPrivateKey, byte[] theirPublicKey) { if (message.length == 0) { return new BurstEncryptedMessage(new byte[0], new byte[0], isText); } try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(message); gzip.flush(); gzip.close(); byte[] compressedPlaintext = bos.toByteArray(); byte[] nonce = new byte[32]; secureRandom.nextBytes(nonce); byte[] data = aesSharedEncrypt(compressedPlaintext, myPrivateKey, theirPublicKey, nonce); return new BurstEncryptedMessage(data, nonce, isText); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } }
Example 5
Source File: GZipUtils.java From common-mvc with MIT License | 5 votes |
public static byte[] compress(byte[] data) throws IOException { if (data == null || data.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(data); gzip.close(); return out.toByteArray();//out.toString("ISO-8859-1"); }
Example 6
Source File: GzipUtil.java From Data_Processor with Apache License 2.0 | 5 votes |
public static byte[] compress(byte[] data) throws IOException { if (null== data|| 0== data.length) { return null; } ByteArrayOutputStream out= new ByteArrayOutputStream(); GZIPOutputStream gzip= new GZIPOutputStream(out); gzip.write(data); gzip.finish(); gzip.close(); byte[] ret= out.toByteArray(); out.close(); return ret;//out.toString("ISO-8859-1"); }
Example 7
Source File: ClassCache.java From OptiFabric with Mozilla Public License 2.0 | 5 votes |
public void save(File output) throws IOException { if(output.exists()){ output.delete(); } FileOutputStream fos = new FileOutputStream(output); GZIPOutputStream gos = new GZIPOutputStream(fos); DataOutputStream dos = new DataOutputStream(gos); //Write the hash dos.writeInt(hash.length); dos.write(hash); //Write the number of classes dos.writeInt(classes.size()); for(Map.Entry<String, byte[]> clazz : classes.entrySet()){ String name = clazz.getKey(); byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8); byte[] bytes = clazz.getValue(); //Write the name dos.writeInt(nameBytes.length); dos.write(nameBytes); //Write the actual bytes dos.writeInt(bytes.length); dos.write(bytes); } dos.flush(); dos.close(); gos.flush(); gos.close(); fos.flush(); fos.close(); }
Example 8
Source File: Http.java From PacketProxy with Apache License 2.0 | 5 votes |
private static byte[] gzip(byte[] input_data) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(out); gout.write(input_data); gout.close(); return out.toByteArray(); }
Example 9
Source File: CctTransportBackendTest.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
@Test public void send_CompressedResponseIsUncompressed() throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output); gzipOutputStream.write("{\"nextRequestWaitMillis\":3}".getBytes(Charset.forName("UTF-8"))); gzipOutputStream.close(); stubFor( post(urlEqualTo("/api")) .willReturn( aResponse() .withStatus(200) .withHeader("Content-Type", "application/json;charset=UTF8;hello=world") .withHeader("Content-Encoding", "gzip") .withBody(output.toByteArray()))); BackendRequest backendRequest = getCCTBackendRequest(); wallClock.tick(); uptimeClock.tick(); BackendResponse response = BACKEND.send(backendRequest); verify( postRequestedFor(urlEqualTo("/api")) .withHeader("Content-Type", equalTo("application/json")) .withHeader("Content-Encoding", equalTo("gzip"))); assertEquals(BackendResponse.ok(3), response); }
Example 10
Source File: GzipInterceptor.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
public static byte[] compress(byte[] data) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout); gout.write(data); gout.flush(); gout.close(); return bout.toByteArray(); }
Example 11
Source File: GZipUtils.java From zheshiyigeniubidexiangmu with MIT License | 5 votes |
public static byte[] compress(byte[] data) throws IOException { if (data == null || data.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); gzip.write(data); gzip.close(); return out.toByteArray();//out.toString("ISO-8859-1"); }
Example 12
Source File: TestCodec.java From hadoop with Apache License 2.0 | 5 votes |
void GzipConcatTest(Configuration conf, Class<? extends Decompressor> decomClass) throws IOException { Random r = new Random(); long seed = r.nextLong(); r.setSeed(seed); LOG.info(decomClass + " seed: " + seed); final int CONCAT = r.nextInt(4) + 3; final int BUFLEN = 128 * 1024; DataOutputBuffer dflbuf = new DataOutputBuffer(); DataOutputBuffer chkbuf = new DataOutputBuffer(); byte[] b = new byte[BUFLEN]; for (int i = 0; i < CONCAT; ++i) { GZIPOutputStream gzout = new GZIPOutputStream(dflbuf); r.nextBytes(b); int len = r.nextInt(BUFLEN); int off = r.nextInt(BUFLEN - len); chkbuf.write(b, off, len); gzout.write(b, off, len); gzout.close(); } final byte[] chk = Arrays.copyOf(chkbuf.getData(), chkbuf.getLength()); CompressionCodec codec = ReflectionUtils.newInstance(GzipCodec.class, conf); Decompressor decom = codec.createDecompressor(); assertNotNull(decom); assertEquals(decomClass, decom.getClass()); DataInputBuffer gzbuf = new DataInputBuffer(); gzbuf.reset(dflbuf.getData(), dflbuf.getLength()); InputStream gzin = codec.createInputStream(gzbuf, decom); dflbuf.reset(); IOUtils.copyBytes(gzin, dflbuf, 4096); final byte[] dflchk = Arrays.copyOf(dflbuf.getData(), dflbuf.getLength()); assertArrayEquals(chk, dflchk); }
Example 13
Source File: GzipCompressor.java From yosegi with Apache License 2.0 | 5 votes |
@Override public byte[] compress( final byte[] data , final int start , final int length , final CompressResult compressResult ) throws IOException { int level = getCompressLevel( compressResult.getCompressionPolicy() ); int optLevel = compressResult.getCurrentLevel(); if ( ( level - optLevel ) < 1 ) { compressResult.setEnd(); optLevel = compressResult.getCurrentLevel(); } int setLevel = level - optLevel; ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); GZIPOutputStream out = new GZIPOutputStream( byteArrayOut ) { { this.def.setLevel( setLevel ); } }; out.write( data , start , length ); out.flush(); out.finish(); byte[] compressByte = byteArrayOut.toByteArray(); byte[] retVal = new byte[ Integer.BYTES + compressByte.length ]; ByteBuffer wrapBuffer = ByteBuffer.wrap( retVal ); wrapBuffer.putInt( length ); wrapBuffer.put( compressByte ); byteArrayOut.close(); out.close(); compressResult.feedBack( length , compressByte.length ); return retVal; }
Example 14
Source File: FileUtil.java From cc-dbp with Apache License 2.0 | 5 votes |
public static byte[] stringToBytes(String str) { try { ByteArrayOutputStream o = new ByteArrayOutputStream(); GZIPOutputStream gz = new GZIPOutputStream(o); gz.write(str.getBytes()); gz.close(); return o.toByteArray(); } catch (Exception e) { throw new Error(e); } }
Example 15
Source File: Output.java From freeacs with MIT License | 5 votes |
/** * Deliver html. * * @param outputString the output string * @param params the params * @param res the res * @throws IOException Signals that an I/O exception has occurred. * @throws ServletException the servlet exception */ private void deliverHTML(String outputString, ParameterParser params, HttpServletResponse res) throws IOException, ServletException { res.setContentType(contentType); res.setCharacterEncoding("UTF-8"); boolean supportsGzip = isGZIPSupported(params); if (isGZIPEnabled() && supportsGzip && outputString != null) { res.setHeader("Content-Encoding", "gzip"); try { GZIPOutputStream gzos = new GZIPOutputStream(res.getOutputStream()); gzos.write(outputString.getBytes()); gzos.close(); } catch (IOException ie) { logger.error( "Main.doImpl(): a problem occured while trying to write to GZIPOutputStream", ie); throw new IOException("<p>An error occured</p>" + ie); } } else if (!res.isCommitted()) { res.setContentType("text/html"); PrintWriter out = res.getWriter(); if (outputString != null) { out.println(outputString); out.close(); } else { out.close(); throw new ServletException("No data to deliver"); } } }
Example 16
Source File: ZipUtils.java From ans-android-sdk with GNU General Public License v3.0 | 5 votes |
/** * Gzip 压缩数据 */ public static byte[] compressForGzip(String unGzipStr) throws IOException { if (CommonUtils.isEmpty(unGzipStr)) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(baos); gzip.write(unGzipStr.getBytes()); gzip.close(); byte[] encode = baos.toByteArray(); baos.flush(); baos.close(); return encode; }
Example 17
Source File: Metrics.java From NameTagChanger with MIT License | 5 votes |
/** * Gzips the given String. * * @param str The string to gzip. * @return The gzipped String. * @throws IOException If the compression failed. */ private static byte[] compress(final String str) throws IOException { if (str == null) { return null; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(outputStream); gzip.write(str.getBytes("UTF-8")); gzip.close(); return outputStream.toByteArray(); }
Example 18
Source File: StubCreator.java From radon with GNU General Public License v3.0 | 4 votes |
public byte[] createStub() throws IOException { GZIPOutputStream gzip = new GZIPOutputStream(out); DataOutputStream dos = new DataOutputStream(gzip); dos.writeShort(instructionLists.size()); for (List<Instruction> list : instructionLists) { dos.writeInt(list.size()); for (Instruction instruction : list) { dos.writeByte(instruction.getOpcode()); dos.writeByte(instruction.getOperands().length); for (Object operand : instruction.getOperands()) { if (operand instanceof Integer) { dos.writeByte(INT); dos.writeInt((Integer) operand); } else if (operand instanceof Long) { dos.writeByte(LONG); dos.writeLong((Long) operand); } else if (operand instanceof Float) { dos.writeByte(FLOAT); dos.writeFloat((Float) operand); } else if (operand instanceof Double) { dos.writeByte(DOUBLE); dos.writeDouble((Double) operand); } else if (operand instanceof String) { dos.writeByte(STRING); dos.writeUTF((String) operand); } else if (operand instanceof Type) { dos.writeByte(CLASS); Type type = (Type) operand; if (type.getSort() == Type.ARRAY) dos.writeUTF(type.getInternalName()); else dos.writeUTF(type.getClassName()); } } } } gzip.close(); return out.toByteArray(); }
Example 19
Source File: TestGzipOutputFilter.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
/** * Test the interaction betwen gzip and flushing. The idea is to: 1. create * a internal output buffer, response, and attach an active gzipoutputfilter * to the output buffer 2. set the output stream of the internal buffer to * be a ByteArrayOutputStream so we can inspect the output bytes 3. write a * chunk out using the gzipoutputfilter and invoke a flush on the * InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out * what's being written out (flushed) 5. find out what's expected by wrting * to GZIPOutputStream and close it (to force flushing) 6. Compare the size * of the two arrays, they should be close (instead of one being much * shorter than the other one) * * @throws Exception */ @Test public void testFlushingWithGzip() throws Exception { // set up response, InternalOutputBuffer, and ByteArrayOutputStream Response res = new Response(); InternalOutputBuffer iob = new InternalOutputBuffer(res, 8 * 1024); ByteArrayOutputStream bos = new ByteArrayOutputStream(); iob.outputStream = bos; res.setOutputBuffer(iob); // set up GzipOutputFilter to attach to the InternalOutputBuffer GzipOutputFilter gf = new GzipOutputFilter(); iob.addFilter(gf); iob.addActiveFilter(gf); // write a chunk out ByteChunk chunk = new ByteChunk(1024); byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes(); chunk.append(d, 0, d.length); iob.doWrite(chunk, res); // flush the InternalOutputBuffer iob.flush(); // read from the ByteArrayOutputStream to find out what's being written // out (flushed) byte[] dataFound = bos.toByteArray(); // find out what's expected by wrting to GZIPOutputStream and close it // (to force flushing) ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024); GZIPOutputStream gos = new GZIPOutputStream(gbos); gos.write(d); gos.close(); // read the expected data byte[] dataExpected = gbos.toByteArray(); // most of the data should have been flushed out assertTrue(dataFound.length >= (dataExpected.length - 20)); }
Example 20
Source File: Zipping.java From Image-Steganography-Library-Android with MIT License | 3 votes |
public static byte[] compress(String string) throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(string.length()); GZIPOutputStream gos = new GZIPOutputStream(os); gos.write(string.getBytes()); gos.close(); byte[] compressed = os.toByteArray(); os.close(); return compressed; }