001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util.zip;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: /**
024: * The CheckedInputStream class is used to maintain a running Checksum of all
025: * data read from a stream.
026: */
027: public class CheckedInputStream extends java.io.FilterInputStream {
028:
029: private final Checksum check;
030:
031: /**
032: * Constructs a new CheckedInputStream on InputStream is. The Checksum will
033: * be calculated using the algorithm implemented by csum.
034: *
035: * @param is
036: * InputStream to calculate checksum from
037: * @param csum
038: * Type of Checksum to calculate
039: */
040: public CheckedInputStream(InputStream is, Checksum csum) {
041: super (is);
042: check = csum;
043: }
044:
045: /**
046: * Reads a byte of data from the underlying stream and recomputes the
047: * Checksum with the byte data.
048: *
049: * @return -1 if end of stream, a single byte value otherwise
050: */
051: @Override
052: public int read() throws IOException {
053: int x = in.read();
054: if (x != -1) {
055: check.update(x);
056: }
057: return x;
058: }
059:
060: /**
061: * Reads up to nbytes of data from the underlying stream, storing it in buf,
062: * starting at offset off. The Checksum is updated with the bytes read.
063: *
064: * @return Number of bytes read, -1 if end of stream
065: */
066: @Override
067: public int read(byte[] buf, int off, int nbytes) throws IOException {
068: int x = in.read(buf, off, nbytes);
069: if (x != -1) {
070: check.update(buf, off, x);
071: }
072: return x;
073: }
074:
075: /**
076: * Returns the Checksum calculated on the stream thus far.
077: *
078: * @return A java.util.zip.Checksum
079: */
080: public Checksum getChecksum() {
081: return check;
082: }
083:
084: /**
085: * Skip upto nbytes of data on the underlying stream. Any skipped bytes are
086: * added to the running Checksum value.
087: *
088: * @param nbytes
089: * long Number of bytes to skip
090: * @return Number of bytes skipped
091: */
092: @Override
093: public long skip(long nbytes) throws IOException {
094: if (nbytes < 1) {
095: return 0;
096: }
097: long skipped = 0;
098: byte[] b = new byte[2048];
099: int x, v;
100: while (skipped != nbytes) {
101: x = in
102: .read(
103: b,
104: 0,
105: (v = (int) (nbytes - skipped)) > b.length ? b.length
106: : v);
107: if (x == -1) {
108: return skipped;
109: }
110: check.update(b, 0, x);
111: skipped += x;
112: }
113: return skipped;
114: }
115: }
|