aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/wrapper/enc/BrotliEncoderChannel.java
blob: 047c3562ecf1020a2d3164050489247ab9e39526 (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
70
71
72
73
74
75
76
77
/* 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.enc;

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;

/**
 * WritableByteChannel that wraps native brotli encoder.
 */
public class BrotliEncoderChannel extends Encoder implements WritableByteChannel {
  /** 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 BrotliEncoderChannel.
   *
   * @param destination underlying destination
   * @param params encoding settings
   * @param bufferSize intermediate buffer size
   */
  public BrotliEncoderChannel(WritableByteChannel destination, Encoder.Parameters params,
      int bufferSize) throws IOException {
    super(destination, params, bufferSize);
  }

  public BrotliEncoderChannel(WritableByteChannel destination, Encoder.Parameters params)
      throws IOException {
    this(destination, params, DEFAULT_BUFFER_SIZE);
  }

  public BrotliEncoderChannel(WritableByteChannel destination) throws IOException {
    this(destination, new Encoder.Parameters());
  }

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

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

  @Override
  public int write(ByteBuffer src) throws IOException {
    synchronized (mutex) {
      if (closed) {
        throw new ClosedChannelException();
      }
      int result = 0;
      while (src.hasRemaining() && encode(EncoderJNI.Operation.PROCESS)) {
        int limit = Math.min(src.remaining(), inputBuffer.remaining());
        ByteBuffer slice = src.slice();
        ((Buffer) slice).limit(limit);
        inputBuffer.put(slice);
        result += limit;
        ((Buffer) src).position(src.position() + limit);
      }
      return result;
    }
  }
}