Search in sources :

Example 36 with DeflaterOutputStream

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

the class Bytes method zip.

/**
 * zip.
 *
 * @param bytes source.
 * @return compressed byte array.
 * @throws IOException
 */
public static byte[] zip(byte[] bytes) throws IOException {
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
    OutputStream os = new DeflaterOutputStream(bos);
    try {
        os.write(bytes);
    } finally {
        os.close();
        bos.close();
    }
    return bos.toByteArray();
}
Also used : OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 37 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project jjwt by jwtk.

the class DeflateCompressionCodec method doCompress.

@Override
public byte[] doCompress(byte[] payload) throws IOException {
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    ByteArrayOutputStream outputStream = null;
    DeflaterOutputStream deflaterOutputStream = null;
    try {
        outputStream = new ByteArrayOutputStream();
        deflaterOutputStream = new DeflaterOutputStream(outputStream, deflater, true);
        deflaterOutputStream.write(payload, 0, payload.length);
        deflaterOutputStream.flush();
        return outputStream.toByteArray();
    } finally {
        Objects.nullSafeClose(outputStream, deflaterOutputStream);
    }
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 38 with DeflaterOutputStream

use of java.util.zip.DeflaterOutputStream in project cas by apereo.

the class SamlAuthenticationRequestTests method deflateViaStream.

private static String deflateViaStream(final String samlRequest) throws IOException {
    final byte[] xmlBytes = samlRequest.getBytes(StandardCharsets.UTF_8);
    final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    final DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteOutputStream);
    deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
    deflaterOutputStream.close();
    return EncodingUtils.encodeBase64(byteOutputStream.toByteArray());
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 39 with DeflaterOutputStream

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

the class ScreenVideoEncoder method encodePixelsSVC2.

public static byte[] encodePixelsSVC2(int[] pixels, int width, int height) {
    changePixelScanFromBottomLeftToTopRight(pixels, width, height);
    if (paletteIndex == null)
        createPaletteIndex();
    try {
        // TODO calibrate initial size
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
        for (int i = 0; i < pixels.length; i++) {
            writeAs15_7(pixels[i], baos1);
        }
        // baos2 contains everything from IMAGEBLOCKV2 except the DataSize field
        // TODO calibrate initial size
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
        // IMAGEFORMAT:
        // ColorDepth: UB[2] 10 (15/7 hybrid color image); HasDiffBlocks: UB[1] 0; Zlib prime stuff (2 bits) not used (0)
        baos2.write(16);
        // No ImageBlockHeader (IMAGEDIFFPOSITION, IMAGEPRIMEPOSITION)
        Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
        DeflaterOutputStream deflateroutputstream = new DeflaterOutputStream(baos2, deflater);
        deflateroutputstream.write(baos1.toByteArray());
        deflateroutputstream.finish();
        byte[] dataBuffer = baos2.toByteArray();
        // DataSize field
        int dataSize = dataBuffer.length;
        // TODO calibrate initial size
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writeShort(((OutputStream) (baos)), dataSize);
        // Data
        baos.write(dataBuffer, 0, dataSize);
        return baos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 40 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)

Aggregations

DeflaterOutputStream (java.util.zip.DeflaterOutputStream)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 Deflater (java.util.zip.Deflater)25 IOException (java.io.IOException)24 OutputStream (java.io.OutputStream)13 InflaterInputStream (java.util.zip.InflaterInputStream)9 DataOutputStream (java.io.DataOutputStream)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 EOFException (java.io.EOFException)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 ImageException (cbit.image.ImageException)2 DeflateCompressor (com.linkedin.r2.filter.compression.streaming.DeflateCompressor)2 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 PipedInputStream (java.io.PipedInputStream)2