Home | History | Annotate | Download | only in zip
      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.OutputStream;
     22 
     23 /**
     24  * The {@code CheckedOutputStream} class is used to maintain a running checksum
     25  * of all data written to a stream. The purpose of this checksum is to establish
     26  * data integrity, by publishing the checksum to other parties wanting to read
     27  * the non corrupted data.
     28  */
     29 public class CheckedOutputStream extends java.io.FilterOutputStream {
     30 
     31     private final Checksum check;
     32 
     33     /**
     34      * Constructs a new {@code CheckedOutputStream} on {@code OutputStream}
     35      * {@code os}. The checksum is calculated using the algorithm implemented
     36      * by {@code csum}.
     37      *
     38      * @param os
     39      *            the output stream to calculate checksum for.
     40      * @param cs
     41      *            an entity implementing the checksum algorithm.
     42      */
     43     public CheckedOutputStream(OutputStream os, Checksum cs) {
     44         super(os);
     45         check = cs;
     46     }
     47 
     48     /**
     49      * Returns the checksum calculated on the stream read so far.
     50      *
     51      * @return the updated checksum.
     52      */
     53     public Checksum getChecksum() {
     54         return check;
     55     }
     56 
     57     /**
     58      * Writes the specified byte to the underlying stream. The checksum is
     59      * updated with {@code val}.
     60      *
     61      * @param val
     62      *            the data value to written to the output stream.
     63      * @throws IOException
     64      *             if an IO error has occurred.
     65      */
     66     @Override
     67     public void write(int val) throws IOException {
     68         out.write(val);
     69         check.update(val);
     70     }
     71 
     72     /**
     73      * Writes n bytes of data from {@code buf} starting at offset {@code off} to
     74      * the underlying stream. The checksum is updated with the bytes written.
     75      *
     76      * @param buf
     77      *            data written to the output stream.
     78      * @param off
     79      *            the offset to start reading the data from {@code buf} written
     80      *            to the output stream.
     81      * @param nbytes
     82      *            number of bytes to write to the output stream.
     83      * @throws IOException
     84      *             if an IO error has occurred.
     85      */
     86     @Override
     87     public void write(byte[] buf, int off, int nbytes) throws IOException {
     88         out.write(buf, off, nbytes);
     89         check.update(buf, off, nbytes);
     90     }
     91 }
     92