001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.zip;
027
028 import java.io.FilterInputStream;
029 import java.io.InputStream;
030 import java.io.IOException;
031
032 /**
033 * An input stream that also maintains a checksum of the data being read.
034 * The checksum can then be used to verify the integrity of the input data.
035 *
036 * @see Checksum
037 * @version 1.28, 05/05/07
038 * @author David Connelly
039 */
040 public class CheckedInputStream extends FilterInputStream {
041 private Checksum cksum;
042
043 /**
044 * Creates an input stream using the specified Checksum.
045 * @param in the input stream
046 * @param cksum the Checksum
047 */
048 public CheckedInputStream(InputStream in, Checksum cksum) {
049 super (in);
050 this .cksum = cksum;
051 }
052
053 /**
054 * Reads a byte. Will block if no input is available.
055 * @return the byte read, or -1 if the end of the stream is reached.
056 * @exception IOException if an I/O error has occurred
057 */
058 public int read() throws IOException {
059 int b = in.read();
060 if (b != -1) {
061 cksum.update(b);
062 }
063 return b;
064 }
065
066 /**
067 * Reads into an array of bytes. If <code>len</code> is not zero, the method
068 * blocks until some input is available; otherwise, no
069 * bytes are read and <code>0</code> is returned.
070 * @param buf the buffer into which the data is read
071 * @param off the start offset in the destination array <code>b</code>
072 * @param len the maximum number of bytes read
073 * @return the actual number of bytes read, or -1 if the end
074 * of the stream is reached.
075 * @exception NullPointerException If <code>buf</code> is <code>null</code>.
076 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
077 * <code>len</code> is negative, or <code>len</code> is greater than
078 * <code>buf.length - off</code>
079 * @exception IOException if an I/O error has occurred
080 */
081 public int read(byte[] buf, int off, int len) throws IOException {
082 len = in.read(buf, off, len);
083 if (len != -1) {
084 cksum.update(buf, off, len);
085 }
086 return len;
087 }
088
089 /**
090 * Skips specified number of bytes of input.
091 * @param n the number of bytes to skip
092 * @return the actual number of bytes skipped
093 * @exception IOException if an I/O error has occurred
094 */
095 public long skip(long n) throws IOException {
096 byte[] buf = new byte[512];
097 long total = 0;
098 while (total < n) {
099 long len = n - total;
100 len = read(buf, 0, len < buf.length ? (int) len
101 : 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 }
|