use of java.util.zip.Deflater in project bazel by bazelbuild.
the class ZipReaderTest method testZipEntryFields.
@Test
public void testZipEntryFields() throws IOException {
CRC32 crc = new CRC32();
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
// 2/3/1995 04:05:06
long date = 791784306000L;
byte[] extra = new ExtraData((short) 0xaa, new byte[] { (byte) 0xbb, (byte) 0xcd }).getBytes();
byte[] tmp = new byte[128];
try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
ZipEntry foo = new ZipEntry("foo");
foo.setComment("foo comment.");
foo.setMethod(ZipEntry.DEFLATED);
foo.setTime(date);
foo.setExtra(extra);
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");
assertThat(fooEntry.getName()).isEqualTo("foo");
assertThat(fooEntry.getComment()).isEqualTo("foo comment.");
assertThat(fooEntry.getMethod()).isEqualTo(Compression.DEFLATED);
assertThat(fooEntry.getVersion()).isEqualTo(Compression.DEFLATED.getMinVersion());
assertThat(fooEntry.getTime()).isEqualTo(date);
assertThat(fooEntry.getSize()).isEqualTo("foo".length());
deflater.reset();
deflater.setInput("foo".getBytes(UTF_8));
deflater.finish();
assertThat(fooEntry.getCompressedSize()).isEqualTo(deflater.deflate(tmp));
crc.reset();
crc.update("foo".getBytes(UTF_8));
assertThat(fooEntry.getCrc()).isEqualTo(crc.getValue());
assertThat(fooEntry.getExtra().getBytes()).isEqualTo(extra);
ZipFileEntry barEntry = reader.getEntry("bar");
assertThat(barEntry.getName()).isEqualTo("bar");
assertThat(barEntry.getComment()).isEqualTo("bar comment.");
assertThat(barEntry.getMethod()).isEqualTo(Compression.STORED);
assertThat(barEntry.getVersion()).isEqualTo(Compression.STORED.getMinVersion());
assertDateAboutNow(new Date(barEntry.getTime()));
assertThat(barEntry.getSize()).isEqualTo("bar".length());
assertThat(barEntry.getCompressedSize()).isEqualTo("bar".length());
crc.reset();
crc.update("bar".getBytes(UTF_8));
assertThat(barEntry.getCrc()).isEqualTo(crc.getValue());
assertThat(barEntry.getExtra().getBytes()).isEqualTo(new byte[] {});
}
}
use of java.util.zip.Deflater in project bazel by bazelbuild.
the class Profiler method start.
/**
* Enable profiling.
*
* <p>Subsequent calls to beginTask/endTask will be recorded
* in the provided output stream. Please note that stream performance is
* extremely important and buffered streams should be utilized.
*
* @param profiledTaskKinds which kinds of {@link ProfilerTask}s to track
* @param stream output stream to store profile data. Note: passing unbuffered stream object
* reference may result in significant performance penalties
* @param comment a comment to insert in the profile data
* @param recordAllDurations iff true, record all tasks regardless of their duration; otherwise
* some tasks may get aggregated if they finished quick enough
* @param clock a {@code BlazeClock.instance()}
* @param execStartTimeNanos execution start time in nanos obtained from {@code clock.nanoTime()}
*/
public synchronized void start(ProfiledTaskKinds profiledTaskKinds, OutputStream stream, String comment, boolean recordAllDurations, Clock clock, long execStartTimeNanos) throws IOException {
Preconditions.checkState(!isActive(), "Profiler already active");
taskStack = new TaskStack();
taskQueue = new ConcurrentLinkedQueue<>();
describer = new ObjectDescriber();
this.profiledTaskKinds = profiledTaskKinds;
this.clock = clock;
// sanity check for current limitation on the number of supported types due
// to using enum.ordinal() to store them instead of EnumSet for performance reasons.
Preconditions.checkState(TASK_COUNT < 256, "The profiler implementation supports only up to 255 different ProfilerTask values.");
// reset state for the new profiling session
taskId.set(0);
this.recordAllDurations = recordAllDurations;
this.saveException = null;
if (stream != null) {
this.timer = new Timer("ProfilerTimer", true);
// Wrapping deflater stream in the buffered stream proved to reduce CPU consumption caused by
// the save() method. Values for buffer sizes were chosen by running small amount of tests
// and identifying point of diminishing returns - but I have not really tried to optimize
// them.
this.out = new DataOutputStream(new BufferedOutputStream(new DeflaterOutputStream(stream, new Deflater(Deflater.BEST_SPEED, false), 65536), 262144));
// magic
this.out.writeInt(MAGIC);
// protocol_version
this.out.writeInt(VERSION);
this.out.writeUTF(comment);
// ProfileTask.values() method sorts enums using their ordinal() value, so
// there there is no need to store ordinal() value for each entry.
this.out.writeInt(TASK_COUNT);
for (ProfilerTask type : ProfilerTask.values()) {
this.out.writeUTF(type.toString());
}
// Start save thread
timer.schedule(new TimerTask() {
@Override
public void run() {
save();
}
}, SAVE_DELAY, SAVE_DELAY);
} else {
this.out = null;
}
// activate profiler
profileStartTime = execStartTimeNanos;
}
use of java.util.zip.Deflater in project hazelcast by hazelcast.
the class IOUtil method compress.
public static byte[] compress(byte[] input) throws IOException {
if (input.length == 0) {
return new byte[0];
}
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_SPEED);
compressor.setInput(input);
compressor.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length / 10);
byte[] buf = new byte[input.length / 10];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
bos.close();
compressor.end();
return bos.toByteArray();
}
use of java.util.zip.Deflater in project cassandra by apache.
the class DeflateCompressor method compressArray.
public int compressArray(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) {
Deflater def = deflater.get();
def.reset();
def.setInput(input, inputOffset, inputLength);
def.finish();
if (def.needsInput())
return 0;
int len = def.deflate(output, outputOffset, maxOutputLength);
assert def.finished();
return len;
}
use of java.util.zip.Deflater in project cassandra by apache.
the class DeflateCompressor method compressBuffer.
public void compressBuffer(ByteBuffer input, ByteBuffer output) {
Deflater def = deflater.get();
def.reset();
byte[] buffer = getThreadLocalScratchBuffer();
// Use half the buffer for input, half for output.
int chunkLen = buffer.length / 2;
while (input.remaining() > chunkLen) {
input.get(buffer, 0, chunkLen);
def.setInput(buffer, 0, chunkLen);
while (!def.needsInput()) {
int len = def.deflate(buffer, chunkLen, chunkLen);
output.put(buffer, chunkLen, len);
}
}
int inputLength = input.remaining();
input.get(buffer, 0, inputLength);
def.setInput(buffer, 0, inputLength);
def.finish();
while (!def.finished()) {
int len = def.deflate(buffer, chunkLen, chunkLen);
output.put(buffer, chunkLen, len);
}
}
Aggregations