1 /* 2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util.zip; 27 28 import java.io.FilterInputStream; 29 import java.io.InputStream; 30 import java.io.IOException; 31 32 /** 33 * An input stream that also maintains a checksum of the data being read. 34 * The checksum can then be used to verify the integrity of the input data. 35 * 36 * @see Checksum 37 * @author David Connelly 38 * @since 1.1 39 */ 40 public 41 class CheckedInputStream extends FilterInputStream { 42 private Checksum cksum; 43 44 /** 45 * Creates an input stream using the specified Checksum. 46 * @param in the input stream 47 * @param cksum the Checksum 48 */ 49 public CheckedInputStream(InputStream in, Checksum cksum) { 50 super(in); 51 this.cksum = cksum; 52 } 53 54 /** 55 * Reads a byte. Will block if no input is available. 56 * @return the byte read, or -1 if the end of the stream is reached. 57 * @throws IOException if an I/O error has occurred 58 */ 59 public int read() throws IOException { 60 int b = in.read(); 61 if (b != -1) { 62 cksum.update(b); 63 } 64 return b; 65 } 66 67 /** 68 * Reads into an array of bytes. If <code>len</code> is not zero, the method 69 * blocks until some input is available; otherwise, no 70 * bytes are read and <code>0</code> is returned. 71 * @param buf the buffer into which the data is read 72 * @param off the start offset in the destination array <code>b</code> 73 * @param len the maximum number of bytes read 74 * @return the actual number of bytes read, or -1 if the end 75 * of the stream is reached. 76 * @throws NullPointerException If <code>buf</code> is <code>null</code>. 77 * @throws IndexOutOfBoundsException If <code>off</code> is negative, 78 * <code>len</code> is negative, or <code>len</code> is greater than 79 * <code>buf.length - off</code> 80 * @throws IOException if an I/O error has occurred 81 */ 82 public int read(byte[] buf, int off, int len) throws IOException { 83 len = in.read(buf, off, len); 84 if (len != -1) { 85 cksum.update(buf, off, len); 86 } 87 return len; 88 } 89 90 /** 91 * Skips specified number of bytes of input. 92 * @param n the number of bytes to skip 93 * @return the actual number of bytes skipped 94 * @throws IOException if an I/O error has occurred 95 */ 96 public long skip(long n) throws IOException { 97 byte[] buf = new byte[512]; 98 long total = 0; 99 while (total < n) { 100 long len = n - total; 101 len = read(buf, 0, len < buf.length ? (int)len : buf.length); 102 if (len == -1) { 103 return total; 104 } 105 total += len; 106 } 107 return total; 108 } 109 110 /** 111 * Returns the Checksum for this input stream. 112 * @return the Checksum value 113 */ 114 public Checksum getChecksum() { 115 return cksum; 116 } 117 }