Java Code Examples for okio.DeflaterSink

Following code examples demonstrate how to use okio.DeflaterSinkfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use okio.DeflaterSinkand various code implementation of this class.

  public void deflatePoorlyCompressed() throws IOException {
    ByteString original = randomBytes(1024 * 1024);
    Buffer data = new Buffer();
    data.write(original);
    Buffer sink = new Buffer();
    DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
    deflaterSink.write(data, data.size());
    deflaterSink.close();
    Buffer inflated = inflate(sink);
    ByteString inflatedStr =  inflated.readByteString();
    for(int i = 0; i < 8193; i++){
      if(inflatedStr.getByte(i) != original.getByte(i)){
        System.out.println(String.format(Locale.CHINA, "difference: position %d original %d inflated %d", i, original.getByte(i), inflatedStr.getByte(i)));
      }
    }
    assertEquals(original, inflatedStr);
  } 


  public void deflateWellCompressed() throws IOException {
    String original = repeat('a', 1024 * 1024);
    Buffer data = new Buffer();
    data.writeUtf8(original);
    Buffer sink = new Buffer();
    DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
    deflaterSink.write(data, data.size());
    deflaterSink.close();
    Buffer inflated = inflate(sink);
    assertEquals(original, inflated.readUtf8());
  } 

  public void deflateWithClose() throws Exception {
    Buffer data = new Buffer();
    String original = "They're moving in herds. They do move in herds.";
    data.writeUtf8(original);
    Buffer sink = new Buffer();
    DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
    deflaterSink.write(data, data.size());
    deflaterSink.close();
    Buffer inflated = inflate(sink);
    assertEquals(original, inflated.readUtf8());
  } 

  public void deflateIntoNonemptySink() throws Exception {
    String original = "They're moving in herds. They do move in herds.";

    for (int i = 0; i < Segment.SIZE; i++) {
      Buffer data = new Buffer().writeUtf8(original);
      Buffer sink = new Buffer().writeUtf8(repeat('a', i));

      DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
      deflaterSink.write(data, data.size());
      deflaterSink.close();

      sink.skip(i);
      Buffer inflated = inflate(sink);
      assertEquals(original, inflated.readUtf8());
    }
  } 

  public void closeWithExceptionWhenWritingAndClosing() throws IOException {
    MockSink mockSink = new MockSink();
    mockSink.scheduleThrow(0, new IOException("first"));
    mockSink.scheduleThrow(1, new IOException("second"));
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.NO_COMPRESSION);
    DeflaterSink deflaterSink = new DeflaterSink(mockSink, deflater);
    deflaterSink.write(new Buffer().writeUtf8(repeat('a', Segment.SIZE)), Segment.SIZE);
    try {
      deflaterSink.close();
      fail();
    } catch (IOException expected) {
      assertEquals("first", expected.getMessage());
    }
    mockSink.assertLogContains("close()");
  } 

  public void multipleSegmentsWithoutCompression() throws IOException {
    Buffer buffer = new Buffer();
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.NO_COMPRESSION);
    DeflaterSink deflaterSink = new DeflaterSink(buffer, deflater);
    int byteCount = Segment.SIZE * 4;
    deflaterSink.write(new Buffer().writeUtf8(repeat('a', byteCount)), byteCount);
    deflaterSink.close();
    assertEquals(repeat('a', byteCount), inflate(buffer).readUtf8(byteCount));
  } 

  public void deflateWithSyncFlush() throws Exception {
    String original = "Yes, yes, yes. That's why we're taking extreme precautions.";
    Buffer data = new Buffer();
    data.writeUtf8(original);
    Buffer sink = new Buffer();
    DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
    deflaterSink.write(data, data.size());
    deflaterSink.flush();
    Buffer inflated = inflate(sink);
    assertEquals(original, inflated.readUtf8());
  } 

    public void write(final OutputStream output) throws Exception {
        final Buffer buffer = new Buffer();

        final long tocBufferSize;
        final Buffer tocCompressedBuffer;
        try (final Buffer tocBuffer = new Buffer()) {
            ToCFactory.toOutputStream(toc, tocBuffer.outputStream());
            tocBufferSize = tocBuffer.size();

            tocCompressedBuffer = new Buffer();

            try (final Sink deflaterSink = new DeflaterSink(tocCompressedBuffer, new Deflater(Deflater.BEST_COMPRESSION))) {
                deflaterSink.write(tocBuffer, tocBuffer.size());
            }
        }

        final long tocCompressedBufferSize = tocCompressedBuffer.size();

        final ByteString tocCompressedBufferHash = HashUtils.hash(tocCompressedBuffer, checksumAlgorithm);

        buffer.write(XarHeader.createHeader(tocCompressedBufferSize, tocBufferSize, checksumAlgorithm));

        buffer.writeAll(tocCompressedBuffer);

        if (tocCompressedBufferHash != null) {
            buffer.write(tocCompressedBufferHash);
        }

        for (final XarEntrySource xs : sources) {
            try (final BufferedSource source = Okio.buffer(xs.getSource())) {
                buffer.writeAll(source);
            }
        }

        try (final Sink sink = Okio.sink(output)) {
            buffer.readAll(sink);
        }
    } 

            switch (encoding) {
                case NONE:
                    this.buffer.writeAll(fileSource);
                    this.archivedChecksum = extractedChecksum;
                    break;
                case GZIP:
                    try (final BufferedSink output = Okio.buffer(new DeflaterSink(this.buffer, new Deflater(Deflater.BEST_COMPRESSION)))) {
                        output.writeAll(fileSource);
                    }
                    this.archivedChecksum = HashUtils.hashHex(this.buffer, checksumStyle);
                    break;
                case BZIP2:
                    throw new UnsupportedEncodingException("Encoding not supported: " + encoding.name());
            } 

    public GzipSink(Sink sink) {
        if (sink == null) {
            throw new IllegalArgumentException("sink == null");
        }
        this.deflater = new Deflater(-1, true);
        this.sink = Okio.buffer(sink);
        this.deflaterSink = new DeflaterSink(this.sink, this.deflater);
        this.writeHeader();
    } 

    }

    public final void windowUpdate(int i, long l)
    {
        this;
        JVM INSTR monitorenter ;
        if (e)
        {
            throw new IOException("closed");
        }
        break MISSING_BLOCK_LABEL_26;
        Exception exception;
        exception;
        this;
        JVM INSTR monitorexit ;
        throw exception;
        if (l != 0L && l <= 0x7fffffffL)
        {
            break MISSING_BLOCK_LABEL_64;
        }
        throw new IllegalArgumentException((new StringBuilder("windowSizeIncrement must be between 1 and 0x7fffffff: ")).append(l).toString());
        a.d(0x80030009);
        a.d(8);
        a.d(i);
        a.d((int)l);
        a.b();
        this;
        JVM INSTR monitorexit ;
    } 

Advertisement
Javadoc
A sink that uses DEFLATE to compress data written to another source. Sync flush Aggressive flushing

of this stream may result in reduced compression. Each call to #flush immediately compresses all currently-buffered data; this early compression may be less effective than compression performed without flushing.

This is equivalent to using Deflater with the sync flush option. This class does not offer any partial flush mechanism. For best performance, only call #flush when application behavior requires it.

Read More
Advertisement