aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/wrapper/dec/BrotliDecoderChannel.java
blob: c9a752ac3b9f2338f0ff523be160626f68b4c561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright 2017 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

package org.brotli.wrapper.dec;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ReadableByteChannel;

/**
 * ReadableByteChannel that wraps native brotli decoder.
 */
public class BrotliDecoderChannel extends Decoder implements ReadableByteChannel {
  /** The default internal buffer size used by the decoder. */
  private static final int DEFAULT_BUFFER_SIZE = 16384;

  private final Object mutex = new Object();

  /**
   * Creates a BrotliDecoderChannel.
   *
   * @param source underlying source
   * @param bufferSize intermediate buffer size
   * @param customDictionary initial LZ77 dictionary
   */
  public BrotliDecoderChannel(ReadableByteChannel source, int bufferSize) throws IOException {
    super(source, bufferSize);
  }

  public BrotliDecoderChannel(ReadableByteChannel source) throws IOException {
    this(source, DEFAULT_BUFFER_SIZE);
  }

  @Override
  public boolean isOpen() {
    synchronized (mutex) {
      return !closed;
    }
  }

  @Override
  public void close() throws IOException {
    synchronized (mutex) {
      super.close();
    }
  }

  @Override
  public int read(ByteBuffer dst) throws IOException {
    synchronized (mutex) {
      if (closed) {
        throw new ClosedChannelException();
      }
      int result = 0;
      while (dst.hasRemaining()) {
        int outputSize = decode();
        if (outputSize == -1) {
          return result == 0 ? -1 : result;
        }
        result += consume(dst);
      }
      return result;
    }
  }
}