Debian Codegraph @

src/org/tukaani/xz/UncompressedLZMA2OutputStream.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.DataOutputStream;
8 import java.io.IOException;
9
10 class UncompressedLZMA2OutputStream extends FinishableOutputStream {
11 private final ArrayCache arrayCache;
12
13 private FinishableOutputStream out;
14 private final DataOutputStream outData;
15
16 private final byte[] uncompBuf;
17 private int uncompPos = 0;
18 private boolean dictResetNeeded = true;
19
20 private boolean finished = false;
21 private IOException exception = null;
22
23 private final byte[] tempBuf = new byte[1];
24
25 static int getMemoryUsage() {
26 // uncompBuf + a little extra
27 return 70;
28 }
29
30 UncompressedLZMA2OutputStream(FinishableOutputStream out,
31 ArrayCache arrayCache) {
32 if (out == null)
33 throw new NullPointerException();
34
35 this.out = out;
36 outData = new DataOutputStream(out);
37
38 // We only allocate one array from the cache. We will call
39 // putArray in writeEndMarker.
40 this.arrayCache = arrayCache;
41 uncompBuf = arrayCache.getByteArray(
42 LZMA2OutputStream.COMPRESSED_SIZE_MAX, false);
43 }
44
45 @Override
46 public void write(int b) throws IOException {
47 tempBuf[0] = (byte)b;
48 write(tempBuf, 0, 1);
49 }
50
51 @Override
52 public void write(byte[] buf, int off, int len) throws IOException {
53 if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length)
54 throw new IndexOutOfBoundsException();
55
56 if (exception != null)
57 throw exception;
58
59 if (finished)
60 throw new XZIOException("Stream finished or closed");
61
62 try {
63 while (len > 0) {
64 int copySize = Math.min(LZMA2OutputStream.COMPRESSED_SIZE_MAX
65 - uncompPos, len);
66
67 if (copySize == LZMA2OutputStream.COMPRESSED_SIZE_MAX) {
68 // Copy 64 KiB directly from buf to the output stream.
69 assert(uncompPos == 0);
70 writeChunk(buf, off, copySize);
71 } else {
72 // Copy to uncompBuf and flush 64 KiB at a time
73 // to the output stream.
74 System.arraycopy(buf, off, uncompBuf, uncompPos, copySize);
75 uncompPos += copySize;
76 if (uncompPos == LZMA2OutputStream.COMPRESSED_SIZE_MAX)
77 writeChunk();
78 }
79
80 off += copySize;
81 len -= copySize;
82 }
83 } catch (IOException e) {
84 exception = e;
85 throw e;
86 }
87 }
88
89 private void writeChunk() throws IOException {
90 writeChunk(uncompBuf, 0, uncompPos);
91 uncompPos = 0;
92 }
93
94 private void writeChunk(byte[] buf, int off, int len) throws IOException {
95 outData.writeByte(dictResetNeeded ? 0x01 : 0x02);
96 outData.writeShort(len - 1);
97 outData.write(buf, off, len);
98 dictResetNeeded = false;
99 }
100
101 private void writeEndMarker() throws IOException {
102 if (exception != null)
103 throw exception;
104
105 if (finished)
106 throw new XZIOException("Stream finished or closed");
107
108 try {
109 if (uncompPos > 0)
110 writeChunk();
111
112 out.write(0x00);
113 } catch (IOException e) {
114 exception = e;
115 throw e;
116 }
117
118 finished = true;
119 arrayCache.putArray(uncompBuf);
120 }
121
122 @Override
123 public void flush() throws IOException {
124 if (exception != null)
125 throw exception;
126
127 if (finished)
128 throw new XZIOException("Stream finished or closed");
129
130 try {
131 if (uncompPos > 0)
132 writeChunk();
133
134 out.flush();
135 } catch (IOException e) {
136 exception = e;
137 throw e;
138 }
139 }
140
141 @Override
142 public void finish() throws IOException {
143 if (!finished) {
144 writeEndMarker();
145
146 try {
147 out.finish();
148 } catch (IOException e) {
149 exception = e;
150 throw e;
151 }
152 }
153 }
154
155 @Override
156 public void close() throws IOException {
157 if (out != null) {
158 if (!finished) {
159 try {
160 writeEndMarker();
161 } catch (IOException e) {}
162 }
163
164 try {
165 out.close();
166 } catch (IOException e) {
167 if (exception == null)
168 exception = e;
169 }
170
171 out = null;
172 }
173
174 if (exception != null)
175 throw exception;
176 }
177 }