| 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.delta.DeltaDecoder; |
| 10 | |
| 11 | /** |
| 12 | * Decodes raw Delta-filtered data (no XZ headers). |
| 13 | * <p> |
| 14 | * The delta filter doesn't change the size of the data and thus it |
| 15 | * cannot have an end-of-payload marker. It will simply decode until |
| 16 | * its input stream indicates end of input. |
| 17 | */ |
| 18 | public class DeltaInputStream extends InputStream { |
| 19 | /** |
| 20 | * Smallest supported delta calculation distance. |
| 21 | */ |
| 22 | public static final int DISTANCE_MIN = 1; |
| 23 | |
| 24 | /** |
| 25 | * Largest supported delta calculation distance. |
| 26 | */ |
| 27 | public static final int DISTANCE_MAX = 256; |
| 28 | |
| 29 | private InputStream in; |
| 30 | private final DeltaDecoder delta; |
| 31 | |
| 32 | private IOException exception = null; |
| 33 | |
| 34 | private final byte[] tempBuf = new byte[1]; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new Delta decoder with the given delta calculation distance. |
| 38 | * |
| 39 | * @param in input stream from which Delta filtered data |
| 40 | * is read |
| 41 | * |
| 42 | * @param distance delta calculation distance, must be in the |
| 43 | * range [{@code DISTANCE_MIN}, |
| 44 | * {@code DISTANCE_MAX}] |
| 45 | */ |
| 46 | public DeltaInputStream(InputStream in, int distance) { |
| 47 | // Check for null because otherwise null isn't detect |
| 48 | // in this constructor. |
| 49 | if (in == null) |
| 50 | throw new NullPointerException(); |
| 51 | |
| 52 | this.in = in; |
| 53 | this.delta = new DeltaDecoder(distance); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Decode the next byte from this input stream. |
| 58 | * |
| 59 | * @return the next decoded byte, or {@code -1} to indicate |
| 60 | * the end of input on the input stream {@code in} |
| 61 | * |
| 62 | * @throws IOException may be thrown by {@code in} |
| 63 | */ |
| 64 | @Override |
| 65 | public int read() throws IOException { |
| 66 | return read(tempBuf, 0, 1) == -1 ? -1 : (tempBuf[0] & 0xFF); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Decode into an array of bytes. |
| 71 | * <p> |
| 72 | * This calls {@code in.read(buf, off, len)} and defilters the |
| 73 | * returned data. |
| 74 | * |
| 75 | * @param buf target buffer for decoded data |
| 76 | * @param off start offset in {@code buf} |
| 77 | * @param len maximum number of bytes to read |
| 78 | * |
| 79 | * @return number of bytes read, or {@code -1} to indicate |
| 80 | * the end of the input stream {@code in} |
| 81 | * |
| 82 | * @throws XZIOException if the stream has been closed |
| 83 | * |
| 84 | * @throws IOException may be thrown by underlying input |
| 85 | * stream {@code in} |
| 86 | */ |
| 87 | @Override |
| 88 | public int read(byte[] buf, int off, int len) throws IOException { |
| 89 | if (len == 0) |
| 90 | return 0; |
| 91 | |
| 92 | if (in == null) |
| 93 | throw new XZIOException("Stream closed"); |
| 94 | |
| 95 | if (exception != null) |
| 96 | throw exception; |
| 97 | |
| 98 | int size; |
| 99 | try { |
| 100 | size = in.read(buf, off, len); |
| 101 | } catch (IOException e) { |
| 102 | exception = e; |
| 103 | throw e; |
| 104 | } |
| 105 | |
| 106 | if (size == -1) |
| 107 | return -1; |
| 108 | |
| 109 | delta.decode(buf, off, size); |
| 110 | return size; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Calls {@code in.available()}. |
| 115 | * |
| 116 | * @return the value returned by {@code in.available()} |
| 117 | */ |
| 118 | @Override |
| 119 | public int available() throws IOException { |
| 120 | if (in == null) |
| 121 | throw new XZIOException("Stream closed"); |
| 122 | |
| 123 | if (exception != null) |
| 124 | throw exception; |
| 125 | |
| 126 | return in.available(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Closes the stream and calls {@code in.close()}. |
| 131 | * If the stream was already closed, this does nothing. |
| 132 | * |
| 133 | * @throws IOException if thrown by {@code in.close()} |
| 134 | */ |
| 135 | @Override |
| 136 | public void close() throws IOException { |
| 137 | if (in != null) { |
| 138 | try { |
| 139 | in.close(); |
| 140 | } finally { |
| 141 | in = null; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |