Search in sources :

Example 91 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream 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 92 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project dropwizard by dropwizard.

the class BiDiGzipHandlerTest method testDecompressDeflateRequestGzipIncompatible.

@Test
public void testDecompressDeflateRequestGzipIncompatible() throws Exception {
    request.setMethod("POST");
    request.setURI("/banner");
    request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate");
    request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos)) {
        Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate);
    }
    request.setContent(baos.toByteArray());
    gzipHandler.setInflateNoWrap(false);
    HttpTester.Response response = HttpTester.parseResponse(servletTester.getResponses(request.generate()));
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContent()).isEqualTo("Banner has been updated");
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 93 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project bazel by bazelbuild.

the class ProfilerTest method testUnsupportedProfilerRecord.

@Test
public void testUnsupportedProfilerRecord() throws Exception {
    Path dataFile = cacheDir.getRelative("profile5.dat");
    profiler.start(ProfiledTaskKinds.ALL, dataFile.getOutputStream(), "phase test", false, BlazeClock.instance(), BlazeClock.instance().nanoTime());
    profiler.startTask(ProfilerTask.TEST, "outer task");
    profiler.logEvent(ProfilerTask.EXCEPTION, "inner task");
    profiler.completeTask(ProfilerTask.TEST);
    profiler.startTask(ProfilerTask.SCANNER, "outer task 2");
    profiler.logSimpleTask(Profiler.nanoTimeMaybe(), ProfilerTask.TEST, "inner task 2");
    profiler.completeTask(ProfilerTask.SCANNER);
    profiler.stop();
    // Validate our test profile.
    ProfileInfo info = ProfileInfo.loadProfile(dataFile);
    info.calculateStats();
    assertFalse(info.isCorruptedOrIncomplete());
    assertEquals(2, info.getStatsForType(ProfilerTask.TEST, info.rootTasksById).count);
    assertEquals(0, info.getStatsForType(ProfilerTask.UNKNOWN, info.rootTasksById).count);
    // Now replace "TEST" type with something unsupported - e.g. "XXXX".
    InputStream in = new InflaterInputStream(dataFile.getInputStream(), new Inflater(false), 65536);
    byte[] buffer = new byte[60000];
    int len = in.read(buffer);
    in.close();
    // Validate that file was completely decoded.
    assertTrue(len < buffer.length);
    String content = new String(buffer, ISO_8859_1);
    int infoIndex = content.indexOf("TEST");
    assertTrue(infoIndex > 0);
    content = content.substring(0, infoIndex) + "XXXX" + content.substring(infoIndex + 4);
    OutputStream out = new DeflaterOutputStream(dataFile.getOutputStream(), new Deflater(Deflater.BEST_SPEED, false), 65536);
    out.write(content.getBytes(ISO_8859_1));
    out.close();
    // Validate that XXXX records were classified as UNKNOWN.
    info = ProfileInfo.loadProfile(dataFile);
    info.calculateStats();
    assertFalse(info.isCorruptedOrIncomplete());
    assertEquals(0, info.getStatsForType(ProfilerTask.TEST, info.rootTasksById).count);
    assertEquals(1, info.getStatsForType(ProfilerTask.SCANNER, info.rootTasksById).count);
    assertEquals(1, info.getStatsForType(ProfilerTask.EXCEPTION, info.rootTasksById).count);
    assertEquals(2, info.getStatsForType(ProfilerTask.UNKNOWN, info.rootTasksById).count);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Deflater(java.util.zip.Deflater) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Inflater(java.util.zip.Inflater) Test(org.junit.Test)

Example 94 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project dubbo by alibaba.

the class BenchmarkRunner method compressDeflate.

private static byte[] compressDeflate(byte[] data) {
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
        DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
        compresser.write(data, 0, data.length);
        compresser.finish();
        compresser.flush();
        return bout.toByteArray();
    } catch (IOException ex) {
        AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
        ae.initCause(ex);
        throw ae;
    }
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 95 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project jodd by oblac.

the class ZipUtil method zlib.

/**
	 * Compresses a file into zlib archive.
	 */
public static File zlib(File file) throws IOException {
    if (file.isDirectory()) {
        throw new IOException("Can't zlib folder");
    }
    FileInputStream fis = new FileInputStream(file);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    String zlibFileName = file.getAbsolutePath() + ZLIB_EXT;
    DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater);
    try {
        StreamUtil.copy(fis, dos);
    } finally {
        StreamUtil.close(dos);
        StreamUtil.close(fis);
    }
    return new File(zlibFileName);
}
Also used : Deflater(java.util.zip.Deflater) FileOutputStream(java.io.FileOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream)

Aggregations

DeflaterOutputStream (java.util.zip.DeflaterOutputStream)162 ByteArrayOutputStream (java.io.ByteArrayOutputStream)101 Deflater (java.util.zip.Deflater)63 IOException (java.io.IOException)52 OutputStream (java.io.OutputStream)39 DataOutputStream (java.io.DataOutputStream)21 InflaterInputStream (java.util.zip.InflaterInputStream)16 GZIPOutputStream (java.util.zip.GZIPOutputStream)12 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)8 InputStream (java.io.InputStream)7 ObjectOutputStream (java.io.ObjectOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FileInputStream (java.io.FileInputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)5 ByteArrayOutputStream (org.fusesource.hawtbuf.ByteArrayOutputStream)5 Saml2Exception (org.springframework.security.saml2.Saml2Exception)5 ByteBuffer (java.nio.ByteBuffer)4 EOFException (java.io.EOFException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3