Java Code Examples for java.util.zip.Deflater#finish()
The following examples show how to use
java.util.zip.Deflater#finish() .
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: EventBufferDumpedCommand.java From visualvm with GNU General Public License v2.0 | 6 votes |
void writeObject(ObjectOutputStream out) throws IOException { out.writeInt(bufSize); out.writeBoolean(buffer != null); if (buffer != null) { Deflater compressor = new Deflater(); // for small buffers, the compressed size can be somewhat larger than the original byte[] compressedBytes = new byte[bufSize + 32]; int compressedSize; compressor.setInput(buffer,startPos,bufSize); compressor.finish(); compressedSize = compressor.deflate(compressedBytes); out.writeInt(compressedSize); out.write(compressedBytes,0,compressedSize); } else { out.writeUTF(eventBufferFileName); } }
Example 2
Source File: Slice.java From TelePlus-Android with GNU General Public License v2.0 | 6 votes |
private void storeData(ByteBuffer data) { try { final byte[] input = data.array(); FileOutputStream fos = new FileOutputStream(file); final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true); deflater.setInput(input, data.arrayOffset(), data.remaining()); deflater.finish(); byte[] buf = new byte[1024]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); fos.write(buf, 0, byteCount); } deflater.end(); fos.close(); } catch (Exception e) { FileLog.e(e); } }
Example 3
Source File: SecureUtil.java From pay with Apache License 2.0 | 6 votes |
public static byte[] deflater(byte[] inputByte) throws IOException { boolean compressedDataLength = false; Deflater compresser = new Deflater(); compresser.setInput(inputByte); compresser.finish(); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while(!compresser.finished()) { int compressedDataLength1 = compresser.deflate(result); o.write(result, 0, compressedDataLength1); } } finally { o.close(); } compresser.end(); return o.toByteArray(); }
Example 4
Source File: DeflateCompressor.java From stratio-cassandra with Apache License 2.0 | 6 votes |
public int compress(byte[] input, int inputOffset, int inputLength, ICompressor.WrappedArray output, int outputOffset) { Deflater def = deflater.get(); def.reset(); def.setInput(input, inputOffset, inputLength); def.finish(); if (def.needsInput()) return 0; int offs = outputOffset; while (true) { offs += def.deflate(output.buffer, offs, output.buffer.length - offs); if (def.finished()) { return offs - outputOffset; } else { // We're not done, output was too small. Increase it and continue byte[] newBuffer = new byte[(output.buffer.length*4)/3 + 1]; System.arraycopy(output.buffer, 0, newBuffer, 0, offs); output.buffer = newBuffer; } } }
Example 5
Source File: EventCompressor.java From Telemetry-Client-for-Android with MIT License | 6 votes |
/** * Compresses the given serialized, batched event string * @param events The event string * @return A compressed version of the event string */ public byte[] compress(String events) { try { byte[] input = events.getBytes("UTF-8"); byte[] output = new byte[SettingsStore.getCllSettingsAsInt(SettingsStore.Settings.MAXEVENTSIZEINBYTES)]; Deflater compressor = new Deflater(Deflater.DEFAULT_COMPRESSION, true); compressor.setInput(input); compressor.finish(); int compressedDataLength = compressor.deflate(output); if(compressedDataLength >= SettingsStore.getCllSettingsAsInt(SettingsStore.Settings.MAXEVENTSIZEINBYTES)) { logger.error(TAG, "Compression resulted in a string of at least the max event buffer size of Vortex. Most likely this means we lost part of the string."); return null; } return Arrays.copyOfRange(output, 0, compressedDataLength); } catch (Exception e) { logger.error(TAG, "Could not compress events"); } return null; }
Example 6
Source File: GzipProcessorUtils.java From datakernel with Apache License 2.0 | 6 votes |
public static ByteBuf toGzip(ByteBuf src) { if (CHECK) checkArgument(src.readRemaining() >= 0); Deflater compressor = ensureCompressor(); compressor.setInput(src.array(), src.head(), src.readRemaining()); compressor.finish(); int dataSize = src.readRemaining(); int crc = getCrc(src, dataSize); int maxDataSize = estimateMaxCompressedSize(dataSize); ByteBuf dst = ByteBufPool.allocate(GZIP_HEADER_SIZE + maxDataSize + GZIP_FOOTER_SIZE + SPARE_BYTES_COUNT); dst.put(GZIP_HEADER); dst = writeCompressedData(compressor, src, dst); dst.writeInt(Integer.reverseBytes(crc)); dst.writeInt(Integer.reverseBytes(dataSize)); moveCompressorToPool(compressor); src.recycle(); return dst; }
Example 7
Source File: HTTPHelpers.java From SAMLRaider with MIT License | 6 votes |
public byte[] compress(byte[] data, boolean gzip) throws IOException { Deflater deflater = new Deflater(5,gzip); deflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); deflater.end(); return output; }
Example 8
Source File: CassandraInputData.java From learning-hadoop with Apache License 2.0 | 6 votes |
/** * Compress a CQL query * * @param queryStr the CQL query * @param compression compression option (GZIP is the only option - so far) * @return an array of bytes containing the compressed query */ public static byte[] compressQuery(String queryStr, Compression compression) { byte[] data = queryStr.getBytes(Charset .forName(CassandraColumnMetaData.UTF8)); Deflater compressor = new Deflater(); compressor.setInput(data); compressor.finish(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!compressor.finished()) { int size = compressor.deflate(buffer); byteArray.write(buffer, 0, size); } return byteArray.toByteArray(); }
Example 9
Source File: JoH.java From xDrip-plus with GNU General Public License v3.0 | 6 votes |
public static String compressString(String source) { try { Deflater deflater = new Deflater(); deflater.setInput(source.getBytes(Charset.forName("UTF-8"))); deflater.finish(); byte[] buf = new byte[source.length() + 256]; int count = deflater.deflate(buf); // check count deflater.end(); return Base64.encodeToString(buf, 0, count, Base64.NO_WRAP); } catch (Exception e) { return null; } }
Example 10
Source File: JoH.java From xDrip with GNU General Public License v3.0 | 6 votes |
public static String compressString(String source) { try { Deflater deflater = new Deflater(); deflater.setInput(source.getBytes(Charset.forName("UTF-8"))); deflater.finish(); byte[] buf = new byte[source.length() + 256]; int count = deflater.deflate(buf); // check count deflater.end(); return Base64.encodeToString(buf, 0, count, Base64.NO_WRAP); } catch (Exception e) { return null; } }
Example 11
Source File: LogUtils.java From Android-UtilCode with Apache License 2.0 | 6 votes |
public static byte[] compress(byte input[]) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Deflater compressor = new Deflater(1); try { compressor.setInput(input); compressor.finish(); final byte[] buf = new byte[2048]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
Example 12
Source File: Gzipper.java From CrossBow with Apache License 2.0 | 5 votes |
public static byte[] zip(byte[] data) { Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); deflater.setInput(data); deflater.finish(); ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length + 1); //write a placeholder byte for the flag baos.write(FLAG_UNCOMPRESSED); byte[] buf = new byte[1024]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); baos.write(buf, 0, byteCount); } deflater.end(); byte[] compressed = baos.toByteArray(); if(compressed.length < data.length) { //compressed is smaller, change the front flag compressed[0] = FLAG_COMPRESSED; return compressed; } else { //compression was no help add flag to data and return byte[] finalData = new byte[data.length + 1]; finalData[0] = FLAG_COMPRESSED; System.arraycopy(data, 0, finalData, 1, data.length); return finalData; } }
Example 13
Source File: TransferUtil.java From Tatala-RPC with Apache License 2.0 | 5 votes |
private static byte[] getOutputByCompress(byte[] toByteArray){ int compressedLength = 0; int unCompressedLength = 0; // out unCompressedLength = toByteArray.length; Deflater deflater = new Deflater(); deflater.setInput(toByteArray); deflater.finish(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); baos.write(buf, 0, byteCount); } deflater.end(); byte[] output = baos.toByteArray(); compressedLength = output.length; //send return data just once byte[] sendData = new byte[TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt() + output.length]; //set compress flag and server call flag sendData[0] = TransferObject.COMPRESS_FLAG; TransferOutputStream fos = new TransferOutputStream(sendData); fos.skipAByte(); fos.writeInt(unCompressedLength); fos.writeInt(compressedLength); System.arraycopy(output, 0, sendData, TransferUtil.getLengthOfByte() + TransferUtil.getLengthOfInt() + TransferUtil.getLengthOfInt(), output.length); return sendData; }
Example 14
Source File: CompressionUtils.java From cxf-fediz with Apache License 2.0 | 5 votes |
public static byte[] deflate(byte[] tokenBytes, boolean nowrap) { Deflater compresser = new Deflater(Deflater.DEFLATED, nowrap); compresser.setInput(tokenBytes); compresser.finish(); byte[] output = new byte[tokenBytes.length * 2]; int compressedDataLength = compresser.deflate(output); byte[] result = new byte[compressedDataLength]; System.arraycopy(output, 0, result, 0, compressedDataLength); return result; }
Example 15
Source File: TestExcessGCLockerCollections.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private void doStep() { byte[] inputArray = new byte[BYTE_ARRAY_LENGTH]; for (int i = 0; i < inputArray.length; i += 1) { inputArray[i] = (byte) (count + i); } Deflater deflater = new Deflater(); deflater.setInput(inputArray); deflater.finish(); byte[] outputArray = new byte[2 * inputArray.length]; deflater.deflate(outputArray); count += 1; }
Example 16
Source File: TestExcessGCLockerCollections.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private void doStep() { byte[] inputArray = new byte[BYTE_ARRAY_LENGTH]; for (int i = 0; i < inputArray.length; i += 1) { inputArray[i] = (byte) (count + i); } Deflater deflater = new Deflater(); deflater.setInput(inputArray); deflater.finish(); byte[] outputArray = new byte[2 * inputArray.length]; deflater.deflate(outputArray); count += 1; }
Example 17
Source File: PackedTransaction.java From EosProxyServer with GNU Lesser General Public License v3.0 | 5 votes |
private byte[] compress( byte[] uncompressedBytes, CompressType compressType) { if ( compressType == null || !CompressType.zlib.equals( compressType)) { return uncompressedBytes; } // zip! Deflater deflater = new Deflater( Deflater.BEST_COMPRESSION ); deflater.setInput( uncompressedBytes ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream( uncompressedBytes.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); return uncompressedBytes; } return outputStream.toByteArray(); }
Example 18
Source File: Misc.java From iaf with Apache License 2.0 | 5 votes |
public static byte[] compress(byte[] input) throws IOException { // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); // Create an expandable byte array to hold the compressed data. // You cannot use an array that's the same size as the orginal because // there is no guarantee that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); // Get the compressed data return bos.toByteArray(); }
Example 19
Source File: ZipReaderTest.java From bazel with Apache License 2.0 | 4 votes |
@Test public void testRawFileData() throws IOException { CRC32 crc = new CRC32(); Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true); try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) { ZipEntry foo = new ZipEntry("foo"); foo.setComment("foo comment."); foo.setMethod(ZipEntry.DEFLATED); zout.putNextEntry(foo); zout.write("foo".getBytes(UTF_8)); zout.closeEntry(); ZipEntry bar = new ZipEntry("bar"); bar.setComment("bar comment."); bar.setMethod(ZipEntry.STORED); bar.setSize("bar".length()); bar.setCompressedSize("bar".length()); crc.reset(); crc.update("bar".getBytes(UTF_8)); bar.setCrc(crc.getValue()); zout.putNextEntry(bar); zout.write("bar".getBytes(UTF_8)); zout.closeEntry(); } try (ZipReader reader = new ZipReader(test, UTF_8)) { ZipFileEntry fooEntry = reader.getEntry("foo"); InputStream fooIn = reader.getRawInputStream(fooEntry); byte[] fooData = new byte[10]; fooIn.read(fooData); byte[] expectedFooData = new byte[10]; deflator.reset(); deflator.setInput("foo".getBytes(UTF_8)); deflator.finish(); deflator.deflate(expectedFooData); assertThat(fooData).isEqualTo(expectedFooData); ZipFileEntry barEntry = reader.getEntry("bar"); InputStream barIn = reader.getRawInputStream(barEntry); byte[] barData = new byte[3]; barIn.read(barData); byte[] expectedBarData = "bar".getBytes(UTF_8); assertThat(barData).isEqualTo(expectedBarData); assertThat(barIn.read()).isEqualTo(-1); assertThat(barIn.read(barData)).isEqualTo(-1); assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1); thrown.expect(IOException.class); thrown.expectMessage("Reset is not supported on this type of stream."); barIn.reset(); } }
Example 20
Source File: LoadedSnapshot.java From netbeans with Apache License 2.0 | 4 votes |
public void save(DataOutputStream dos) throws IOException, OutOfMemoryError { // todo [performance] profile memory use during the save operation // there is ~80MB bytes used for byte[], for the length of uncompressed data ~20MB Properties props = new Properties(); settings.store(props); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("save properties: --------------------------------------------------------------"); // NOI18N LOGGER.finest(settings.debug()); LOGGER.finest("-------------------------------------------------------------------------------"); // NOI18N } ByteArrayOutputStream baos = new ByteArrayOutputStream(1000000); // ~1MB pre-allocated DataOutputStream snapshotDataStream = new DataOutputStream(baos); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(10000); // ~10kB pre-allocated DataOutputStream settingsDataStream = new DataOutputStream(baos2); try { snapshot.writeToStream(snapshotDataStream); snapshotDataStream.flush(); props.store(settingsDataStream, ""); //NOI18N settingsDataStream.flush(); byte[] snapshotBytes = baos.toByteArray(); byte[] compressedBytes = new byte[snapshotBytes.length]; Deflater d = new Deflater(); d.setInput(snapshotBytes); d.finish(); int compressedLen = d.deflate(compressedBytes); int uncompressedLen = snapshotBytes.length; // binary file format: // 1. magic number: "nbprofiler" // 2. int type // 3. int length of snapshot data size // 4. snapshot data bytes // 5. int length of settings data size // 6. settings data bytes (.properties plain text file format) // 7. String (UTF) custom comments if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("save version:" + SNAPSHOT_FILE_VERSION_MAJOR //NOI18N + "." + SNAPSHOT_FILE_VERSION_MINOR); // NOI18N LOGGER.finest("save type:" + getType()); // NOI18N LOGGER.finest("length of uncompressed snapshot data:" + uncompressedLen); // NOI18N LOGGER.finest("save length of snapshot data:" + compressedLen); // NOI18N LOGGER.finest("length of settings data:" + baos2.size()); // NOI18N } dos.writeBytes(PROFILER_FILE_MAGIC_STRING); // 1. magic number: "nbprofiler" dos.writeByte(SNAPSHOT_FILE_VERSION_MAJOR); // 2. file version dos.writeByte(SNAPSHOT_FILE_VERSION_MINOR); // 3. file version dos.writeInt(getType()); // 4. int type dos.writeInt(compressedLen); // 5. int length of compressed snapshot data size dos.writeInt(uncompressedLen); // 5. int length of compressed snapshot data size dos.write(compressedBytes, 0, compressedLen); // 6. compressed snapshot data bytes dos.writeInt(baos2.size()); // 7. int length of settings data size dos.write(baos2.toByteArray()); // 8. settings data bytes (.properties plain text file format) dos.writeUTF(userComments); } catch (OutOfMemoryError e) { baos = null; snapshotDataStream = null; baos2 = null; settingsDataStream = null; throw e; } finally { if (snapshotDataStream != null) { snapshotDataStream.close(); } if (settingsDataStream != null) { settingsDataStream.close(); } } }