01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.OutputStream;
22:
23: /**
24: * The CheckedOutputStream class is used to maintain a running Checksum of all
25: * data written to a stream.
26: */
27: public class CheckedOutputStream extends java.io.FilterOutputStream {
28:
29: private final Checksum check;
30:
31: /**
32: * Constructs a new CheckedOutputStream on OutputStream os. The Checksum
33: * will be calculated using the algorithm implemented by csum.
34: *
35: * @param os
36: * OutputStream to calculate checksum from
37: * @param cs
38: * Type of Checksum to calculate
39: */
40: public CheckedOutputStream(OutputStream os, Checksum cs) {
41: super (os);
42: check = cs;
43: }
44:
45: /**
46: * Returns the Checksum calculated on the stream thus far.
47: *
48: * @return A java.util.zip.Checksum
49: */
50: public Checksum getChecksum() {
51: return check;
52: }
53:
54: /**
55: * Writes byte value val to the underlying stream. The Checksum is updated
56: * with val.
57: *
58: * @param val
59: * Value of the byte to write out
60: *
61: * @throws IOException
62: * if an IO error has occured
63: */
64: @Override
65: public void write(int val) throws IOException {
66: out.write(val);
67: check.update(val);
68: }
69:
70: /**
71: * Writes nbytes of data from buf starting at offset off to the underlying
72: * stream. The Checksum is updated with the bytes written.
73: *
74: * @param buf
75: * data to write out
76: * @param off
77: * the start offset of the data
78: * @param nbytes
79: * number of bytes to write out
80: *
81: * @throws IOException
82: * if an IO error has occured
83: */
84: @Override
85: public void write(byte[] buf, int off, int nbytes)
86: throws IOException {
87: out.write(buf, off, nbytes);
88: check.update(buf, off, nbytes);
89: }
90: }
|