Debian Codegraph @

src/org/tukaani/xz/SimpleOutputStream.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.IOException;
8 import org.tukaani.xz.simple.SimpleFilter;
9
10 class SimpleOutputStream extends FinishableOutputStream {
11 private static final int FILTER_BUF_SIZE = 4096;
12
13 private FinishableOutputStream out;
14 private final SimpleFilter simpleFilter;
15
16 private final byte[] filterBuf = new byte[FILTER_BUF_SIZE];
17 private int pos = 0;
18 private int unfiltered = 0;
19
20 private IOException exception = null;
21 private boolean finished = false;
22
23 private final byte[] tempBuf = new byte[1];
24
25 static int getMemoryUsage() {
26 return 1 + FILTER_BUF_SIZE / 1024;
27 }
28
29 SimpleOutputStream(FinishableOutputStream out,
30 SimpleFilter simpleFilter) {
31 if (out == null)
32 throw new NullPointerException();
33
34 this.out = out;
35 this.simpleFilter = simpleFilter;
36 }
37
38 @Override
39 public void write(int b) throws IOException {
40 tempBuf[0] = (byte)b;
41 write(tempBuf, 0, 1);
42 }
43
44 @Override
45 public void write(byte[] buf, int off, int len) throws IOException {
46 if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length)
47 throw new IndexOutOfBoundsException();
48
49 if (exception != null)
50 throw exception;
51
52 if (finished)
53 throw new XZIOException("Stream finished or closed");
54
55 while (len > 0) {
56 // Copy more unfiltered data into filterBuf.
57 int copySize = Math.min(len, FILTER_BUF_SIZE - (pos + unfiltered));
58 System.arraycopy(buf, off, filterBuf, pos + unfiltered, copySize);
59 off += copySize;
60 len -= copySize;
61 unfiltered += copySize;
62
63 // Filter the data in filterBuf.
64 int filtered = simpleFilter.code(filterBuf, pos, unfiltered);
65 assert filtered <= unfiltered;
66 unfiltered -= filtered;
67
68 // Write out the filtered data.
69 try {
70 out.write(filterBuf, pos, filtered);
71 } catch (IOException e) {
72 exception = e;
73 throw e;
74 }
75
76 pos += filtered;
77
78 // If end of filterBuf was reached, move the pending unfiltered
79 // data to the beginning of the buffer so that more data can
80 // be copied into filterBuf on the next loop iteration.
81 if (pos + unfiltered == FILTER_BUF_SIZE) {
82 System.arraycopy(filterBuf, pos, filterBuf, 0, unfiltered);
83 pos = 0;
84 }
85 }
86 }
87
88 private void writePending() throws IOException {
89 assert !finished;
90
91 if (exception != null)
92 throw exception;
93
94 try {
95 out.write(filterBuf, pos, unfiltered);
96 } catch (IOException e) {
97 exception = e;
98 throw e;
99 }
100
101 finished = true;
102 }
103
104 @Override
105 public void flush() throws IOException {
106 throw new UnsupportedOptionsException("Flushing is not supported");
107 }
108
109 @Override
110 public void finish() throws IOException {
111 if (!finished) {
112 // If it fails, don't call out.finish().
113 writePending();
114
115 try {
116 out.finish();
117 } catch (IOException e) {
118 exception = e;
119 throw e;
120 }
121 }
122 }
123
124 @Override
125 public void close() throws IOException {
126 if (out != null) {
127 if (!finished) {
128 // out.close() must be called even if writePending() fails.
129 // writePending() saves the possible exception so we can
130 // ignore exceptions here.
131 try {
132 writePending();
133 } catch (IOException e) {}
134 }
135
136 try {
137 out.close();
138 } catch (IOException e) {
139 // If there is an earlier exception, the exception
140 // from out.close() is lost.
141 if (exception == null)
142 exception = e;
143 }
144
145 out = null;
146 }
147
148 if (exception != null)
149 throw exception;
150 }
151 }