Debian Codegraph @

src/org/tukaani/xz/SimpleInputStream.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
5 package org.tukaani.xz;
6
7 import java.io.InputStream;
8 import java.io.IOException;
9 import org.tukaani.xz.simple.SimpleFilter;
10
11 class SimpleInputStream extends InputStream {
12 private static final int FILTER_BUF_SIZE = 4096;
13
14 private InputStream in;
15 private final SimpleFilter simpleFilter;
16
17 private final byte[] filterBuf = new byte[FILTER_BUF_SIZE];
18 private int pos = 0;
19 private int filtered = 0;
20 private int unfiltered = 0;
21
22 private boolean endReached = false;
23 private IOException exception = null;
24
25 private final byte[] tempBuf = new byte[1];
26
27 static int getMemoryUsage() {
28 return 1 + FILTER_BUF_SIZE / 1024;
29 }
30
31 SimpleInputStream(InputStream in, SimpleFilter simpleFilter) {
32 // Check for null because otherwise null isn't detect
33 // in this constructor.
34 if (in == null)
35 throw new NullPointerException();
36
37 // The simpleFilter argument comes from this package
38 // so it is known to be non-null already.
39 assert simpleFilter != null;
40
41 this.in = in;
42 this.simpleFilter = simpleFilter;
43 }
44
45 @Override
46 public int read() throws IOException {
47 return read(tempBuf, 0, 1) == -1 ? -1 : (tempBuf[0] & 0xFF);
48 }
49
50 @Override
51 public int read(byte[] buf, int off, int len) throws IOException {
52 if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length)
53 throw new IndexOutOfBoundsException();
54
55 if (len == 0)
56 return 0;
57
58 if (in == null)
59 throw new XZIOException("Stream closed");
60
61 if (exception != null)
62 throw exception;
63
64 try {
65 int size = 0;
66
67 while (true) {
68 // Copy filtered data into the caller-provided buffer.
69 int copySize = Math.min(filtered, len);
70 System.arraycopy(filterBuf, pos, buf, off, copySize);
71 pos += copySize;
72 filtered -= copySize;
73 off += copySize;
74 len -= copySize;
75 size += copySize;
76
77 // If end of filterBuf was reached, move the pending data to
78 // the beginning of the buffer so that more data can be
79 // copied into filterBuf on the next loop iteration.
80 if (pos + filtered + unfiltered == FILTER_BUF_SIZE) {
81 System.arraycopy(filterBuf, pos, filterBuf, 0,
82 filtered + unfiltered);
83 pos = 0;
84 }
85
86 if (len == 0 || endReached)
87 return size > 0 ? size : -1;
88
89 assert filtered == 0;
90
91 // Get more data into the temporary buffer.
92 int inSize = FILTER_BUF_SIZE - (pos + filtered + unfiltered);
93 inSize = in.read(filterBuf, pos + filtered + unfiltered,
94 inSize);
95
96 if (inSize == -1) {
97 // Mark the remaining unfiltered bytes to be ready
98 // to be copied out.
99 endReached = true;
100 filtered = unfiltered;
101 unfiltered = 0;
102 } else {
103 // Filter the data in filterBuf.
104 unfiltered += inSize;
105 filtered = simpleFilter.code(filterBuf, pos, unfiltered);
106 assert filtered <= unfiltered;
107 unfiltered -= filtered;
108 }
109 }
110 } catch (IOException e) {
111 exception = e;
112 throw e;
113 }
114 }
115
116 @Override
117 public int available() throws IOException {
118 if (in == null)
119 throw new XZIOException("Stream closed");
120
121 if (exception != null)
122 throw exception;
123
124 return filtered;
125 }
126
127 @Override
128 public void close() throws IOException {
129 if (in != null) {
130 try {
131 in.close();
132 } finally {
133 in = null;
134 }
135 }
136 }
137 }