View Javadoc
1   /*
2    * Copyright (C) 2005-2015 Schlichtherle IT Services.
3    * All rights reserved. Use is subject to license terms.
4    */
5   package net.java.truevfs.comp.zip;
6   
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.util.zip.CheckedInputStream;
10  import java.util.zip.Checksum;
11  
12  /**
13   * An input stream that also maintains a checksum of the data being read.
14   * The checksum can then be used to verify the integrity of the input data.
15   * <p>
16   * In constrast to its super class, this class accepts a parameter to customize
17   * the skip buffer size.
18   *
19   * @author Christian Schlichtherle
20   */
21  class CheckedInputStreamWithSkipBufferSize extends CheckedInputStream {
22  
23      private final int skipBufferSize;
24  
25      /**
26       * Constructs an input stream using the specified checksum and skip buffer
27       * size.
28       *
29       * @param in the input stream
30       * @param cksum the checksum
31       * @param skipBufferSize the skip buffer size
32       */
33      CheckedInputStreamWithSkipBufferSize(
34              final InputStream in,
35              final Checksum cksum,
36              final int skipBufferSize) {
37          super(in, cksum);
38          this.skipBufferSize = skipBufferSize;
39      }
40  
41      @Override
42      public long skip(long n) throws IOException {
43          final byte[] buf = new byte[skipBufferSize];
44          long total = 0;
45          for (long len; 0 < (len = n - total); total += len) {
46              len = read(buf, 0, len < buf.length ? (int) len : buf.length);
47              if (0 > len) break;
48          }
49          return total;
50      }
51  }