Search in sources :

Example 41 with Deflater

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[] {});
    }
}
Also used : Deflater(java.util.zip.Deflater) CRC32(java.util.zip.CRC32) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Date(java.util.Date) Test(org.junit.Test)

Example 42 with Deflater

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;
}
Also used : Timer(java.util.Timer) Deflater(java.util.zip.Deflater) TimerTask(java.util.TimerTask) DataOutputStream(java.io.DataOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 43 with Deflater

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();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 44 with Deflater

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;
}
Also used : Deflater(java.util.zip.Deflater)

Example 45 with Deflater

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);
    }
}
Also used : Deflater(java.util.zip.Deflater)

Aggregations

Deflater (java.util.zip.Deflater)85 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)25 IOException (java.io.IOException)18 Test (org.junit.Test)13 Inflater (java.util.zip.Inflater)9 OutputStream (java.io.OutputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 Field (java.lang.reflect.Field)3 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 CharArrayReader (java.io.CharArrayReader)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Reader (java.io.Reader)2