|
13 | 13 | */ |
14 | 14 | package io.airlift.compress.v3.zstd; |
15 | 15 |
|
16 | | -import java.io.IOException; |
17 | 16 | import java.io.InputStream; |
18 | | -import java.util.Arrays; |
19 | 17 |
|
20 | | -import static io.airlift.compress.v3.zstd.Util.checkPositionIndexes; |
21 | | -import static io.airlift.compress.v3.zstd.Util.checkState; |
22 | | -import static java.lang.Math.max; |
23 | | -import static java.util.Objects.requireNonNull; |
24 | | -import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET; |
25 | | - |
26 | | -public class ZstdInputStream |
| 18 | +/** |
| 19 | + * Abstract base class for zstd decompressing input streams. |
| 20 | + * <p> |
| 21 | + * This class provides a common type for both the pure Java implementation ({@link ZstdJavaInputStream}) |
| 22 | + * and the native implementation ({@link ZstdNativeInputStream}). |
| 23 | + */ |
| 24 | +public abstract sealed class ZstdInputStream |
27 | 25 | extends InputStream |
28 | | -{ |
29 | | - private static final int MIN_BUFFER_SIZE = 4096; |
30 | | - |
31 | | - private final InputStream inputStream; |
32 | | - private final ZstdIncrementalFrameDecompressor decompressor = new ZstdIncrementalFrameDecompressor(); |
33 | | - |
34 | | - private byte[] inputBuffer = new byte[decompressor.getInputRequired()]; |
35 | | - private int inputBufferOffset; |
36 | | - private int inputBufferLimit; |
37 | | - |
38 | | - private byte[] singleByteOutputBuffer; |
39 | | - |
40 | | - private boolean closed; |
41 | | - |
42 | | - public ZstdInputStream(InputStream inputStream) |
43 | | - { |
44 | | - this.inputStream = requireNonNull(inputStream, "inputStream is null"); |
45 | | - } |
46 | | - |
47 | | - @Override |
48 | | - public int read() |
49 | | - throws IOException |
50 | | - { |
51 | | - if (singleByteOutputBuffer == null) { |
52 | | - singleByteOutputBuffer = new byte[1]; |
53 | | - } |
54 | | - int readSize = read(singleByteOutputBuffer, 0, 1); |
55 | | - checkState(readSize != 0, "A zero read size should never be returned"); |
56 | | - if (readSize != 1) { |
57 | | - return -1; |
58 | | - } |
59 | | - return singleByteOutputBuffer[0] & 0xFF; |
60 | | - } |
61 | | - |
62 | | - @Override |
63 | | - public int read(final byte[] outputBuffer, final int outputOffset, final int outputLength) |
64 | | - throws IOException |
65 | | - { |
66 | | - if (closed) { |
67 | | - throw new IOException("Stream is closed"); |
68 | | - } |
69 | | - |
70 | | - if (outputBuffer == null) { |
71 | | - throw new NullPointerException(); |
72 | | - } |
73 | | - checkPositionIndexes(outputOffset, outputOffset + outputLength, outputBuffer.length); |
74 | | - if (outputLength == 0) { |
75 | | - return 0; |
76 | | - } |
77 | | - |
78 | | - final int outputLimit = outputOffset + outputLength; |
79 | | - int outputUsed = 0; |
80 | | - while (outputUsed < outputLength) { |
81 | | - boolean enoughInput = fillInputBufferIfNecessary(decompressor.getInputRequired()); |
82 | | - if (!enoughInput) { |
83 | | - if (decompressor.isAtStoppingPoint()) { |
84 | | - return outputUsed > 0 ? outputUsed : -1; |
85 | | - } |
86 | | - throw new IOException("Not enough input bytes"); |
87 | | - } |
88 | | - |
89 | | - decompressor.partialDecompress( |
90 | | - inputBuffer, |
91 | | - inputBufferOffset + ARRAY_BYTE_BASE_OFFSET, |
92 | | - inputBufferLimit + ARRAY_BYTE_BASE_OFFSET, |
93 | | - outputBuffer, |
94 | | - outputOffset + outputUsed, |
95 | | - outputLimit); |
96 | | - |
97 | | - inputBufferOffset += decompressor.getInputConsumed(); |
98 | | - outputUsed += decompressor.getOutputBufferUsed(); |
99 | | - } |
100 | | - return outputUsed; |
101 | | - } |
102 | | - |
103 | | - private boolean fillInputBufferIfNecessary(int requiredSize) |
104 | | - throws IOException |
105 | | - { |
106 | | - if (inputBufferLimit - inputBufferOffset >= requiredSize) { |
107 | | - return true; |
108 | | - } |
109 | | - |
110 | | - // compact existing buffered data to the front of the buffer |
111 | | - if (inputBufferOffset > 0) { |
112 | | - int copySize = inputBufferLimit - inputBufferOffset; |
113 | | - System.arraycopy(inputBuffer, inputBufferOffset, inputBuffer, 0, copySize); |
114 | | - inputBufferOffset = 0; |
115 | | - inputBufferLimit = copySize; |
116 | | - } |
117 | | - |
118 | | - if (inputBuffer.length < requiredSize) { |
119 | | - inputBuffer = Arrays.copyOf(inputBuffer, max(requiredSize, MIN_BUFFER_SIZE)); |
120 | | - } |
121 | | - |
122 | | - while (inputBufferLimit < inputBuffer.length) { |
123 | | - int readSize = inputStream.read(inputBuffer, inputBufferLimit, inputBuffer.length - inputBufferLimit); |
124 | | - if (readSize < 0) { |
125 | | - break; |
126 | | - } |
127 | | - inputBufferLimit += readSize; |
128 | | - } |
129 | | - return inputBufferLimit >= requiredSize; |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - public int available() |
134 | | - throws IOException |
135 | | - { |
136 | | - if (closed) { |
137 | | - return 0; |
138 | | - } |
139 | | - return decompressor.getRequestedOutputSize(); |
140 | | - } |
141 | | - |
142 | | - @Override |
143 | | - public void close() |
144 | | - throws IOException |
145 | | - { |
146 | | - if (!closed) { |
147 | | - closed = true; |
148 | | - inputStream.close(); |
149 | | - } |
150 | | - } |
151 | | -} |
| 26 | + permits ZstdJavaInputStream, ZstdNativeInputStream {} |
0 commit comments