use of java.util.zip.Deflater in project voltdb by VoltDB.
the class FileArchiver method archive.
/*
public static void compressFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.archive(infilename, outfilename, storage, COMPRESSION_ZIP);
}
public static void decompressFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.unarchive(
infilename, outfilename, storage, COMPRESSION_ZIP);
}
public static void copyFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.archive(
infilename, outfilename, storage, COMPRESSION_NONE);
}
public static void restoreFile(String infilename, String outfilename,
FileAccess storage) throws IOException {
FileArchiver.unarchive(
infilename, outfilename, storage, COMPRESSION_NONE);
}
*/
public static void archive(String infilename, String outfilename, FileAccess storage, int compressionType) throws IOException {
InputStream in = null;
OutputStream f = null;
DeflaterOutputStream deflater = null;
boolean completed = false;
// if there is no file
if (!storage.isStreamElement(infilename)) {
return;
}
try {
byte[] b = new byte[COPY_BLOCK_SIZE];
in = storage.openInputStreamElement(infilename);
f = storage.openOutputStreamElement(outfilename);
switch(compressionType) {
case COMPRESSION_ZIP:
f = deflater = new DeflaterOutputStream(f, new Deflater(Deflater.BEST_SPEED), b.length);
break;
case COMPRESSION_GZIP:
f = deflater = new GZIPOutputStream(f, b.length);
break;
case COMPRESSION_NONE:
break;
default:
throw new RuntimeException("FileArchiver" + compressionType);
}
while (true) {
int l = in.read(b, 0, b.length);
if (l == -1) {
break;
}
f.write(b, 0, l);
}
completed = true;
} catch (Throwable e) {
throw FileUtil.toIOException(e);
} finally {
try {
if (in != null) {
in.close();
}
if (f != null) {
if (deflater != null) {
deflater.finish();
}
f.close();
}
if (!completed && storage.isStreamElement(outfilename)) {
storage.removeElement(outfilename);
}
} catch (Throwable e) {
throw FileUtil.toIOException(e);
}
}
}
use of java.util.zip.Deflater in project graylog2-server by Graylog2.
the class TestHelper method zlibCompress.
public static byte[] zlibCompress(String what, int level) throws IOException {
final ByteArrayInputStream compressMe = new ByteArrayInputStream(what.getBytes(StandardCharsets.UTF_8));
final ByteArrayOutputStream compressedMessage = new ByteArrayOutputStream();
try (DeflaterOutputStream out = new DeflaterOutputStream(compressedMessage, new Deflater(level))) {
ByteStreams.copy(compressMe, out);
}
return compressedMessage.toByteArray();
}
use of java.util.zip.Deflater in project HdrHistogram by HdrHistogram.
the class DoubleHistogramTest method testDoubleHistogramSerialization.
void testDoubleHistogramSerialization(DoubleHistogram histogram) throws Exception {
histogram.recordValue(testValueLevel);
histogram.recordValue(testValueLevel * 10);
histogram.recordValueWithExpectedInterval(histogram.getCurrentHighestTrackableValue() - 1, histogram.getCurrentHighestTrackableValue() / 1000);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
ByteArrayInputStream bis = null;
ObjectInput in = null;
DoubleHistogram newHistogram = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(histogram);
Deflater compresser = new Deflater();
compresser.setInput(bos.toByteArray());
compresser.finish();
byte[] compressedOutput = new byte[1024 * 1024];
int compressedDataLength = compresser.deflate(compressedOutput);
System.out.println("Serialized form of " + histogram.getClass() + " with internalHighestToLowestValueRatio = " + histogram.getHighestToLowestValueRatio() + "\n and a numberOfSignificantValueDigits = " + histogram.getNumberOfSignificantValueDigits() + " is " + bos.toByteArray().length + " bytes long. Compressed form is " + compressedDataLength + " bytes long.");
System.out.println(" (estimated footprint was " + histogram.getEstimatedFootprintInBytes() + " bytes)");
bis = new ByteArrayInputStream(bos.toByteArray());
in = new ObjectInputStream(bis);
newHistogram = (DoubleHistogram) in.readObject();
} finally {
if (out != null)
out.close();
bos.close();
if (in != null)
in.close();
if (bis != null)
bis.close();
}
assertNotNull(newHistogram);
assertEqual(histogram, newHistogram);
}
use of java.util.zip.Deflater in project MVPArms by JessYanCoding.
the class ZipHelper method compressForZlib.
/**
* zlib compress 2 byte
*
* @param bytesToCompress
* @return
*/
public static byte[] compressForZlib(byte[] bytesToCompress) {
Deflater deflater = new Deflater();
deflater.setInput(bytesToCompress);
deflater.finish();
byte[] bytesCompressed = new byte[Short.MAX_VALUE];
int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);
byte[] returnValues = new byte[numberOfBytesAfterCompression];
System.arraycopy(bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression);
return returnValues;
}
use of java.util.zip.Deflater in project undertow by undertow-io.
the class CompressionUtilsTest method setup.
@Before
public void setup() throws Exception {
compress = new Deflater(Deflater.BEST_SPEED, true);
decompress = new Inflater(true);
}
Aggregations