Debian Codegraph @

src/org/tukaani/xz/rangecoder/RangeEncoderToBuffer.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz.rangecoder;
7
8 import java.io.OutputStream;
9 import java.io.IOException;
10 import org.tukaani.xz.ArrayCache;
11
12 public final class RangeEncoderToBuffer extends RangeEncoder {
13 private final byte[] buf;
14 private int bufPos;
15
16 public RangeEncoderToBuffer(int bufSize, ArrayCache arrayCache) {
17 buf = arrayCache.getByteArray(bufSize, false);
18 reset();
19 }
20
21 public void putArraysToCache(ArrayCache arrayCache) {
22 arrayCache.putArray(buf);
23 }
24
25 public void reset() {
26 super.reset();
27 bufPos = 0;
28 }
29
30 public int getPendingSize() {
31 // With LZMA2 it is known that cacheSize fits into an int.
32 return bufPos + (int)cacheSize + 5 - 1;
33 }
34
35 public int finish() {
36 // super.finish() cannot throw an IOException because writeByte()
37 // provided in this file cannot throw an IOException.
38 try {
39 super.finish();
40 } catch (IOException e) {
41 throw new Error();
42 }
43
44 return bufPos;
45 }
46
47 public void write(OutputStream out) throws IOException {
48 out.write(buf, 0, bufPos);
49 }
50
51 @Override
52 void writeByte(int b) {
53 buf[bufPos++] = (byte)b;
54 }
55 }