Debian Codegraph @

src/org/tukaani/xz/BlockOutputStream.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.OutputStream;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import org.tukaani.xz.common.EncoderUtil;
11 import org.tukaani.xz.check.Check;
12
13 class BlockOutputStream extends FinishableOutputStream {
14 private final OutputStream out;
15 private final CountingOutputStream outCounted;
16 private FinishableOutputStream filterChain;
17 private final Check check;
18
19 private final int headerSize;
20 private final long compressedSizeLimit;
21 private long uncompressedSize = 0;
22
23 private final byte[] tempBuf = new byte[1];
24
25 public BlockOutputStream(OutputStream out, FilterEncoder[] filters,
26 Check check, ArrayCache arrayCache)
27 throws IOException {
28 this.out = out;
29 this.check = check;
30
31 // Initialize the filter chain.
32 outCounted = new CountingOutputStream(out);
33 filterChain = outCounted;
34 for (int i = filters.length - 1; i >= 0; --i)
35 filterChain = filters[i].getOutputStream(filterChain, arrayCache);
36
37 // Prepare to encode the Block Header field.
38 ByteArrayOutputStream bufStream = new ByteArrayOutputStream();
39
40 // Write a dummy Block Header Size field. The real value is written
41 // once everything else except CRC32 has been written.
42 bufStream.write(0x00);
43
44 // Write Block Flags. Storing Compressed Size or Uncompressed Size
45 // isn't supported for now.
46 bufStream.write(filters.length - 1);
47
48 // List of Filter Flags
49 for (int i = 0; i < filters.length; ++i) {
50 EncoderUtil.encodeVLI(bufStream, filters[i].getFilterID());
51 byte[] filterProps = filters[i].getFilterProps();
52 EncoderUtil.encodeVLI(bufStream, filterProps.length);
53 bufStream.write(filterProps);
54 }
55
56 // Header Padding
57 while ((bufStream.size() & 3) != 0)
58 bufStream.write(0x00);
59
60 byte[] buf = bufStream.toByteArray();
61
62 // Total size of the Block Header: Take the size of the CRC32 field
63 // into account.
64 headerSize = buf.length + 4;
65
66 // This is just a sanity check.
67 if (headerSize > EncoderUtil.BLOCK_HEADER_SIZE_MAX)
68 throw new UnsupportedOptionsException();
69
70 // Block Header Size
71 buf[0] = (byte)(buf.length / 4);
72
73 // Write the Block Header field to the output stream.
74 out.write(buf);
75 EncoderUtil.writeCRC32(out, buf);
76
77 // Calculate the maximum allowed size of the Compressed Data field.
78 // It is hard to exceed it so this is mostly to be pedantic.
79 compressedSizeLimit = (EncoderUtil.VLI_MAX & ~3)
80 - headerSize - check.getSize();
81 }
82
83 @Override
84 public void write(int b) throws IOException {
85 tempBuf[0] = (byte)b;
86 write(tempBuf, 0, 1);
87 }
88
89 @Override
90 public void write(byte[] buf, int off, int len) throws IOException {
91 filterChain.write(buf, off, len);
92 check.update(buf, off, len);
93 uncompressedSize += len;
94 validate();
95 }
96
97 @Override
98 public void flush() throws IOException {
99 filterChain.flush();
100 validate();
101 }
102
103 @Override
104 public void finish() throws IOException {
105 // Finish the Compressed Data field.
106 filterChain.finish();
107 validate();
108
109 // Block Padding
110 for (long i = outCounted.getSize(); (i & 3) != 0; ++i)
111 out.write(0x00);
112
113 // Check
114 out.write(check.finish());
115 }
116
117 private void validate() throws IOException {
118 long compressedSize = outCounted.getSize();
119
120 // It is very hard to trigger this exception.
121 // This is just to be pedantic.
122 if (compressedSize < 0 || compressedSize > compressedSizeLimit
123 || uncompressedSize < 0)
124 throw new XZIOException("XZ Stream has grown too big");
125 }
126
127 public long getUnpaddedSize() {
128 return headerSize + outCounted.getSize() + check.getSize();
129 }
130
131 public long getUncompressedSize() {
132 return uncompressedSize;
133 }
134 }