Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

255
LINES

< > BotCompany Repo | #1031086 // InflaterInputStream_relaxed - InflaterInputStream that allows incomplete files

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (285L/3K).

1  
!7
2  
3  
sclass InflaterInputStream_relaxed extends FilterInputStream {
4  
    /**
5  
     * Decompressor for this stream.
6  
     */
7  
    protected Inflater inf;
8  
9  
    /**
10  
     * Input buffer for decompression.
11  
     */
12  
    protected byte[] buf;
13  
14  
    /**
15  
     * Length of input buffer.
16  
     */
17  
    protected int len;
18  
19  
    boolean closed = false;
20  
    // this flag is set to true after EOF has reached
21  
    private boolean reachEOF = false;
22  
23  
    /**
24  
     * Check to make sure that this stream has not been closed
25  
     */
26  
    void ensureOpen() throws IOException {
27  
        if (closed) {
28  
            throw new IOException("Stream closed");
29  
        }
30  
    }
31  
32  
33  
    /**
34  
     * Creates a new input stream with the specified decompressor and
35  
     * buffer size.
36  
     * @param in the input stream
37  
     * @param inf the decompressor ("inflater")
38  
     * @param size the input buffer size
39  
     * @throws    IllegalArgumentException if {@code size <= 0}
40  
     */
41  
    public InflaterInputStream_relaxed(InputStream in, Inflater inf, int size) {
42  
        super(in);
43  
        if (in == null || inf == null) {
44  
            throw new NullPointerException();
45  
        } else if (size <= 0) {
46  
            throw new IllegalArgumentException("buffer size <= 0");
47  
        }
48  
        this.inf = inf;
49  
        buf = new byte[size];
50  
    }
51  
52  
    /**
53  
     * Creates a new input stream with the specified decompressor and a
54  
     * default buffer size.
55  
     * @param in the input stream
56  
     * @param inf the decompressor ("inflater")
57  
     */
58  
    public InflaterInputStream_relaxed(InputStream in, Inflater inf) {
59  
        this(in, inf, 512);
60  
    }
61  
62  
    boolean usesDefaultInflater = false;
63  
64  
    /**
65  
     * Creates a new input stream with a default decompressor and buffer size.
66  
     * @param in the input stream
67  
     */
68  
    public InflaterInputStream_relaxed(InputStream in) {
69  
        this(in, new Inflater());
70  
        usesDefaultInflater = true;
71  
    }
72  
73  
    private byte[] singleByteBuf = new byte[1];
74  
75  
    /**
76  
     * Reads a byte of uncompressed data. This method will block until
77  
     * enough input is available for decompression.
78  
     * @return the byte read, or -1 if end of compressed input is reached
79  
     * @throws    IOException if an I/O error has occurred
80  
     */
81  
    public int read() throws IOException {
82  
        ensureOpen();
83  
        return read(singleByteBuf, 0, 1) == -1 ? -1 : Byte.toUnsignedInt(singleByteBuf[0]);
84  
    }
85  
86  
    /**
87  
     * Reads uncompressed data into an array of bytes. If {@code len} is not
88  
     * zero, the method will block until some input can be decompressed; otherwise,
89  
     * no bytes are read and {@code 0} is returned.
90  
     * @param b the buffer into which the data is read
91  
     * @param off the start offset in the destination array {@code b}
92  
     * @param len the maximum number of bytes read
93  
     * @return the actual number of bytes read, or -1 if the end of the
94  
     *         compressed input is reached or a preset dictionary is needed
95  
     * @throws     NullPointerException If {@code b} is {@code null}.
96  
     * @throws     IndexOutOfBoundsException If {@code off} is negative,
97  
     * {@code len} is negative, or {@code len} is greater than
98  
     * {@code b.length - off}
99  
     * @throws    ZipException if a ZIP format error has occurred
100  
     * @throws    IOException if an I/O error has occurred
101  
     */
102  
    public int read(byte[] b, int off, int len) throws IOException {
103  
        ensureOpen();
104  
        if (b == null) {
105  
            throw new NullPointerException();
106  
        } else if (off < 0 || len < 0 || len > b.length - off) {
107  
            throw new IndexOutOfBoundsException();
108  
        } else if (len == 0) {
109  
            return 0;
110  
        }
111  
        try {
112  
            int n;
113  
            while ((n = inf.inflate(b, off, len)) == 0) {
114  
                if (inf.finished() || inf.needsDictionary()) {
115  
                    reachEOF = true;
116  
                    return -1;
117  
                }
118  
                if (inf.needsInput()) {
119  
                  if (!fill())
120  
                    ret -1;
121  
                }
122  
            }
123  
            return n;
124  
        } catch (DataFormatException e) {
125  
            String s = e.getMessage();
126  
            throw new ZipException(s != null ? s : "Invalid ZLIB data format");
127  
        }
128  
    }
129  
130  
    /**
131  
     * Returns 0 after EOF has been reached, otherwise always return 1.
132  
     * <p>
133  
     * Programs should not count on this method to return the actual number
134  
     * of bytes that could be read without blocking.
135  
     *
136  
     * @return     1 before EOF and 0 after EOF.
137  
     * @throws     IOException  if an I/O error occurs.
138  
     *
139  
     */
140  
    public int available() throws IOException {
141  
        ensureOpen();
142  
        if (reachEOF) {
143  
            return 0;
144  
        } else if (inf.finished()) {
145  
            // the end of the compressed data stream has been reached
146  
            reachEOF = true;
147  
            return 0;
148  
        } else {
149  
            return 1;
150  
        }
151  
    }
152  
153  
    private byte[] b = new byte[512];
154  
155  
    /**
156  
     * Skips specified number of bytes of uncompressed data.
157  
     * @param n the number of bytes to skip
158  
     * @return the actual number of bytes skipped.
159  
     * @throws    IOException if an I/O error has occurred
160  
     * @throws    IllegalArgumentException if {@code n < 0}
161  
     */
162  
    public long skip(long n) throws IOException {
163  
        if (n < 0) {
164  
            throw new IllegalArgumentException("negative skip length");
165  
        }
166  
        ensureOpen();
167  
        int max = (int)Math.min(n, Integer.MAX_VALUE);
168  
        int total = 0;
169  
        while (total < max) {
170  
            int len = max - total;
171  
            if (len > b.length) {
172  
                len = b.length;
173  
            }
174  
            len = read(b, 0, len);
175  
            if (len == -1) {
176  
                reachEOF = true;
177  
                break;
178  
            }
179  
            total += len;
180  
        }
181  
        return total;
182  
    }
183  
184  
    /**
185  
     * Closes this input stream and releases any system resources associated
186  
     * with the stream.
187  
     * @throws    IOException if an I/O error has occurred
188  
     */
189  
    public void close() throws IOException {
190  
        if (!closed) {
191  
            if (usesDefaultInflater)
192  
                inf.end();
193  
            in.close();
194  
            closed = true;
195  
        }
196  
    }
197  
198  
    /**
199  
     * Fills input buffer with more data to decompress.
200  
     * @throws    IOException if an I/O error has occurred
201  
     * @return true if not at end of stream
202  
     */
203  
    protected bool fill() throws IOException {
204  
        ensureOpen();
205  
        len = in.read(buf, 0, buf.length);
206  
        if (len == -1)
207  
          false;
208  
        inf.setInput(buf, 0, len);
209  
        true;
210  
    }
211  
212  
    /**
213  
     * Tests if this input stream supports the {@code mark} and
214  
     * {@code reset} methods. The {@code markSupported}
215  
     * method of {@code InflaterInputStream_relaxed} returns
216  
     * {@code false}.
217  
     *
218  
     * @return  a {@code boolean} indicating if this stream type supports
219  
     *          the {@code mark} and {@code reset} methods.
220  
     * @see     java.io.InputStream#mark(int)
221  
     * @see     java.io.InputStream#reset()
222  
     */
223  
    public boolean markSupported() {
224  
        return false;
225  
    }
226  
227  
    /**
228  
     * Marks the current position in this input stream.
229  
     *
230  
     * <p> The {@code mark} method of {@code InflaterInputStream_relaxed}
231  
     * does nothing.
232  
     *
233  
     * @param   readlimit   the maximum limit of bytes that can be read before
234  
     *                      the mark position becomes invalid.
235  
     * @see     java.io.InputStream#reset()
236  
     */
237  
    public synchronized void mark(int readlimit) {
238  
    }
239  
240  
    /**
241  
     * Repositions this stream to the position at the time the
242  
     * {@code mark} method was last called on this input stream.
243  
     *
244  
     * <p> The method {@code reset} for class
245  
     * {@code InflaterInputStream_relaxed} does nothing except throw an
246  
     * {@code IOException}.
247  
     *
248  
     * @throws     IOException  if this method is invoked.
249  
     * @see     java.io.InputStream#mark(int)
250  
     * @see     java.io.IOException
251  
     */
252  
    public synchronized void reset() throws IOException {
253  
        throw new IOException("mark/reset not supported");
254  
    }
255  
}

Author comment

Began life as a copy of #1031085

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031086
Snippet name: InflaterInputStream_relaxed - InflaterInputStream that allows incomplete files
Eternal ID of this version: #1031086/4
Text MD5: dbb468beed812c54376498ffab12b004
Transpilation MD5: d2a067f1194fd009c92a166b0d0938c2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-30 23:51:14
Source code size: 8337 bytes / 255 lines
Pitched / IR pitched: No / No
Views / Downloads: 129 / 318
Version history: 3 change(s)
Referenced in: [show references]