aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java
blob: 5643decef8f1141f5b6d6387dad2a8dcf9581e5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.compress.archivers.dump;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

import org.apache.commons.compress.utils.IOUtils;

/**
 * Filter stream that mimics a physical tape drive capable of compressing
 * the data stream.
 *
 * @NotThreadSafe
 */
class TapeInputStream extends FilterInputStream {
    private byte[] blockBuffer = new byte[DumpArchiveConstants.TP_SIZE];
    private int currBlkIdx = -1;
    private int blockSize = DumpArchiveConstants.TP_SIZE;
    private static final int RECORD_SIZE = DumpArchiveConstants.TP_SIZE;
    private int readOffset = DumpArchiveConstants.TP_SIZE;
    private boolean isCompressed = false;
    private long bytesRead = 0;

    /**
     * Constructor
     */
    public TapeInputStream(final InputStream in) {
        super(in);
    }

    /**
     * Set the DumpArchive Buffer's block size. We need to sync the block size with the
     * dump archive's actual block size since compression is handled at the
     * block level.
     *
     * @param recsPerBlock
     *            records per block
     * @param isCompressed
     *            true if the archive is compressed
     * @throws IOException
     *             more than one block has been read
     * @throws IOException
     *             there was an error reading additional blocks.
     */
    public void resetBlockSize(final int recsPerBlock, final boolean isCompressed)
        throws IOException {
        this.isCompressed = isCompressed;

        blockSize = RECORD_SIZE * recsPerBlock;

        // save first block in case we need it again
        final byte[] oldBuffer = blockBuffer;

        // read rest of new block
        blockBuffer = new byte[blockSize];
        System.arraycopy(oldBuffer, 0, blockBuffer, 0, RECORD_SIZE);
        readFully(blockBuffer, RECORD_SIZE, blockSize - RECORD_SIZE);

        this.currBlkIdx = 0;
        this.readOffset = RECORD_SIZE;
    }

    /**
     * @see java.io.InputStream#available
     */
    @Override
    public int available() throws IOException {
        if (readOffset < blockSize) {
            return blockSize - readOffset;
        }

        return in.available();
    }

    /**
     * @see java.io.InputStream#read()
     */
    @Override
    public int read() throws IOException {
        throw new IllegalArgumentException(
            "all reads must be multiple of record size (" + RECORD_SIZE +
            " bytes.");
    }

    /**
     * {@inheritDoc}
     *
     * <p>reads the full given length unless EOF is reached.</p>
     *
     * @param len length to read, must be a multiple of the stream's
     * record size
     */
    @Override
    public int read(final byte[] b, int off, final int len) throws IOException {
        if ((len % RECORD_SIZE) != 0) {
            throw new IllegalArgumentException(
                "all reads must be multiple of record size (" + RECORD_SIZE +
                " bytes.");
        }

        int bytes = 0;

        while (bytes < len) {
            // we need to read from the underlying stream.
            // this will reset readOffset value.
            // return -1 if there's a problem.
            if (readOffset == blockSize) {
                try {
                    readBlock(true);
                } catch (ShortFileException sfe) { // NOSONAR
                    return -1;
                }
            }

            int n = 0;

            if ((readOffset + (len - bytes)) <= blockSize) {
                // we can read entirely from the buffer.
                n = len - bytes;
            } else {
                // copy what we can from the buffer.
                n = blockSize - readOffset;
            }

            // copy data, increment counters.
            System.arraycopy(blockBuffer, readOffset, b, off, n);
            readOffset += n;
            bytes += n;
            off += n;
        }

        return bytes;
    }

    /**
     * Skip bytes. Same as read but without the arraycopy.
     *
     * <p>skips the full given length unless EOF is reached.</p>
     *
     * @param len length to read, must be a multiple of the stream's
     * record size
     */
    @Override
    public long skip(final long len) throws IOException {
        if ((len % RECORD_SIZE) != 0) {
            throw new IllegalArgumentException(
                "all reads must be multiple of record size (" + RECORD_SIZE +
                " bytes.");
        }

        long bytes = 0;

        while (bytes < len) {
            // we need to read from the underlying stream.
            // this will reset readOffset value. We do not perform
            // any decompression if we won't eventually read the data.
            // return -1 if there's a problem.
            if (readOffset == blockSize) {
                try {
                    readBlock((len - bytes) < blockSize);
                } catch (ShortFileException sfe) { // NOSONAR
                    return -1;
                }
            }

            long n = 0;

            if ((readOffset + (len - bytes)) <= blockSize) {
                // we can read entirely from the buffer.
                n = len - bytes;
            } else {
                // copy what we can from the buffer.
                n = (long) blockSize - readOffset;
            }

            // do not copy data but still increment counters.
            readOffset += n;
            bytes += n;
        }

        return bytes;
    }

    /**
     * Close the input stream.
     *
     * @throws IOException on error
     */
    @Override
    public void close() throws IOException {
        if (in != null && in != System.in) {
            in.close();
        }
    }

    /**
     * Peek at the next record from the input stream and return the data.
     *
     * @return The record data.
     * @throws IOException on error
     */
    public byte[] peek() throws IOException {
        // we need to read from the underlying stream. This
        // isn't a problem since it would be the first step in
        // any subsequent read() anyway.
        if (readOffset == blockSize) {
            try {
                readBlock(true);
            } catch (ShortFileException sfe) { // NOSONAR
                return null;
            }
        }

        // copy data, increment counters.
        final byte[] b = new byte[RECORD_SIZE];
        System.arraycopy(blockBuffer, readOffset, b, 0, b.length);

        return b;
    }

    /**
     * Read a record from the input stream and return the data.
     *
     * @return The record data.
     * @throws IOException on error
     */
    public byte[] readRecord() throws IOException {
        final byte[] result = new byte[RECORD_SIZE];

        // the read implementation will loop internally as long as
        // input is available
        if (-1 == read(result, 0, result.length)) {
            throw new ShortFileException();
        }

        return result;
    }

    /**
     * Read next block. All decompression is handled here.
     *
     * @param decompress if false the buffer will not be decompressed.
     *        This is an optimization for longer seeks.
     */
    private void readBlock(final boolean decompress) throws IOException {
        if (in == null) {
            throw new IOException("input buffer is closed");
        }

        if (!isCompressed || (currBlkIdx == -1)) {
            // file is not compressed
            readFully(blockBuffer, 0, blockSize);
            bytesRead += blockSize;
        } else {
            readFully(blockBuffer, 0, 4);
            bytesRead += 4;

            final int h = DumpArchiveUtil.convert32(blockBuffer, 0);
            final boolean compressed = (h & 0x01) == 0x01;

            if (!compressed) {
                // file is compressed but this block is not.
                readFully(blockBuffer, 0, blockSize);
                bytesRead += blockSize;
            } else {
                // this block is compressed.
                final int flags = (h >> 1) & 0x07;
                int length = (h >> 4) & 0x0FFFFFFF;
                final byte[] compBuffer = new byte[length];
                readFully(compBuffer, 0, length);
                bytesRead += length;

                if (!decompress) {
                    // just in case someone reads the data.
                    Arrays.fill(blockBuffer, (byte) 0);
                } else {
                    switch (DumpArchiveConstants.COMPRESSION_TYPE.find(flags &
                        0x03)) {
                    case ZLIB:

                        final Inflater inflator = new Inflater();
                        try {
                            inflator.setInput(compBuffer, 0, compBuffer.length);
                            length = inflator.inflate(blockBuffer);

                            if (length != blockSize) {
                                throw new ShortFileException();
                            }
                        } catch (final DataFormatException e) {
                            throw new DumpArchiveException("bad data", e);
                        } finally {
                            inflator.end();
                        }

                        break;

                    case BZLIB:
                        throw new UnsupportedCompressionAlgorithmException(
                            "BZLIB2");

                    case LZO:
                        throw new UnsupportedCompressionAlgorithmException(
                            "LZO");

                    default:
                        throw new UnsupportedCompressionAlgorithmException();
                    }
                }
            }
        }

        currBlkIdx++;
        readOffset = 0;
    }

    /**
     * Read buffer
     */
    private void readFully(final byte[] b, final int off, final int len)
        throws IOException {
        final int count = IOUtils.readFully(in, b, off, len);
        if (count < len) {
            throw new ShortFileException();
        }
    }

    /**
     * Get number of bytes read.
     */
    public long getBytesRead() {
        return bytesRead;
    }
}