aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/dec/IntReader.java
blob: 38bf7740ba59e59e4f8993fafd550191bf29b7de (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
/* 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.dec;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

/**
 * Byte-to-int conversion magic.
 */
final class IntReader {

  static final int CAPACITY = 1024 + 16;

  private final ByteBuffer byteBuffer =
      ByteBuffer.allocateDirect(CAPACITY << 2).order(ByteOrder.LITTLE_ENDIAN);
  private final IntBuffer intBuffer = byteBuffer.asIntBuffer();

  /**
   * Reinitialize reader with new data chunk.
   *
   * NB: intLen == 4 * byteSize!
   * NB: intLen should be less or equal to {@link CAPACITY}
   */
  static void reload(IntReader ir, byte[] data, int offset, int intLen) {
    ir.byteBuffer.clear();
    ir.byteBuffer.put(data, offset, intLen << 2);
    ir.intBuffer.rewind();
  }

  static int position(IntReader ir) {
    return ir.intBuffer.position();
  }

  static void setPosition(IntReader ir, int position) {
    ir.intBuffer.position(position);
  }

  static int read(IntReader ir) {
    // Advances position by 1.
    return ir.intBuffer.get();
  }
}