OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / java / util / zip / CheckedInputStream.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package java.util.zip;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 /**
24  * The {@code CheckedInputStream} class is used to maintain a checksum at the
25  * same time as the data, on which the checksum is computed, is read from a
26  * stream. The purpose of this checksum is to establish data integrity,
27  * comparing the computed checksum against a published checksum value.
28  */
29 public class CheckedInputStream extends java.io.FilterInputStream {
30
31     private final Checksum check;
32
33     /**
34      * Constructs a new {@code CheckedInputStream} on {@code InputStream}
35      * {@code is}. The checksum will be calculated using the algorithm
36      * implemented by {@code csum}.
37      *
38      * <p><strong>Warning:</strong> passing a null source creates an invalid
39      * {@code CheckedInputStream}. All operations on such a stream will fail.
40      *
41      * @param is
42      *            the input stream to calculate checksum from.
43      * @param csum
44      *            an entity implementing the checksum algorithm.
45      */
46     public CheckedInputStream(InputStream is, Checksum csum) {
47         super(is);
48         check = csum;
49     }
50
51     /**
52      * Reads one byte of data from the underlying input stream and updates the
53      * checksum with the byte data.
54      *
55      * @return {@code -1} at the end of the stream, a single byte value
56      *         otherwise.
57      * @throws IOException
58      *             if an {@code IOException} occurs.
59      */
60     @Override
61     public int read() throws IOException {
62         int x = in.read();
63         if (x != -1) {
64             check.update(x);
65         }
66         return x;
67     }
68
69     /**
70      * Reads up to n bytes of data from the underlying input stream, storing it
71      * into {@code buf}, starting at offset {@code off}. The checksum is
72      * updated with the bytes read.
73      *
74      * @param buf
75      *            the byte array in which to store the bytes read.
76      * @param off
77      *            the initial position in {@code buf} to store the bytes read
78      *            from this stream.
79      * @param nbytes
80      *            the maximum number of bytes to store in {@code buf}.
81      * @return the number of bytes actually read or {@code -1} if arrived at the
82      *         end of the filtered stream while reading the data.
83      * @throws IOException
84      *             if this stream is closed or some I/O error occurs.
85      */
86     @Override
87     public int read(byte[] buf, int off, int nbytes) throws IOException {
88         int x = in.read(buf, off, nbytes);
89         if (x != -1) {
90             check.update(buf, off, x);
91         }
92         return x;
93     }
94
95     /**
96      * Returns the checksum calculated on the stream read so far.
97      *
98      * @return the updated checksum.
99      */
100     public Checksum getChecksum() {
101         return check;
102     }
103
104     /**
105      * Skip up to n bytes of data on the underlying input stream. Any skipped
106      * bytes are added to the running checksum value.
107      *
108      * @param nbytes
109      *            the number of bytes to skip.
110      * @throws IOException
111      *             if this stream is closed or another I/O error occurs.
112      * @return the number of bytes skipped.
113      */
114     @Override
115     public long skip(long nbytes) throws IOException {
116         if (nbytes < 1) {
117             return 0;
118         }
119         long skipped = 0;
120         byte[] b = new byte[(int)Math.min(nbytes, 2048L)];
121         int x, v;
122         while (skipped != nbytes) {
123             x = in.read(b, 0,
124                     (v = (int) (nbytes - skipped)) > b.length ? b.length : v);
125             if (x == -1) {
126                 return skipped;
127             }
128             check.update(b, 0, x);
129             skipped += x;
130         }
131         return skipped;
132     }
133 }