[Chapter 18] CheckedInputStream

#

| Chapter 18 The java.util.zip Package |

# CheckedInputStream

## Name

CheckedInputStream

## Synopsis

Class Name:

  `java.util.zip.CheckedInputStream`

Superclass:

  `java.io.FilterInputStream`

Immediate Subclasses:

  None

Interfaces Implemented:

  None

Availability:

  New as of JDK 1.1

## Description

The `CheckedInputStream` class represents an `InputStream` with an associated checksum. In other words, a `CheckedInputStream` wraps a regular input stream and computes a checksum value as data is read from the stream. The checksum can verify the integrity of the data. When you create a `CheckedInputStream`, you must specify an object that implements the `Checksum` interface that computes the checksum.

## Class Summary

    public class java.util.zip.CheckedInputStream
                 extends java.io.FilterInputStream {
      // Constructors
      public CheckedInputStream(InputStream in, Checksum cksum);

      // Instance Methods
      public Checksum getChecksum();
      public int read();
      public int read(byte[] buf, int off, int len);
      public long skip(long n);
    }

## Constructors

### CheckedInputStream

#### public CheckedInputStream(InputStream in, Checksum cksum)

Parameters

  `in`

    The underlying input stream to use as a data source.

  `cksum`

    The checksum object.

Description

  This constructor creates a `CheckedInputStream` that reads data from the given `InputStream` and updates the given `Checksum`.

## Instance Methods

### getChecksum

#### public Checksum getChecksum()

Returns

  The `Checksum` associated with this input stream.

Description

  This method returns the `Checksum` object associated with this input stream.

### read

#### public int read() throws IOException

Returns

  The next byte from the stream or `-1` if the end of the stream has been reached.

Throws

  `IOException`

    If any kind of I/O error occurs.

Overrides

  `FilterInputStream.read()`

Description

  This method reads the next byte from the underlying `InputStream` and then updates the checksum. The method blocks until some data is available, the end of the stream is detected, or an exception is thrown.

#### public int read(byte[] buf, int off, int len) throws IOException

Parameters

  `buf`

    An array of bytes to be filled from the stream.

  `off`

    An offset into the byte array.

  `len`

    The number of bytes to read.

Returns

  The number of bytes read or `-1` if the end of the stream is encountered immediately.

Throws

  `IOException`

    If any kind of I/O error occurs.

Overrides

  `FilterInputStream.read(byte[], int, int)`

Description

  This method reads up to `len` bytes from the underlying `InputStream` and places them into the given array starting at `off`. The checksum is then updated with the data that has been read. The method blocks until some data is available.

### skip

#### public long skip(long n) throws IOException

Parameters

  `n`

    The number of bytes to skip.

Returns

  The actual number of bytes skipped.

Throws

  `IOException`

    If any kind of I/O error occurs.

Overrides

  `FilterInputStream.skip()`

Description

  This method skips over the specified number of bytes in the underlying `InputStream`. The skipped bytes are not calculated into the checksum.

## Inherited Methods

Method | Inherited From | Method | Inherited From
--- | --- | --- | ---
`available()` | `FilterInputStream` | `clone()` | `Object`
`close()` | `FilterInputStream` | `equals(Object)` | `Object`
`finalize()` | `Object` | `getClass()` | `Object`
`hashCode()` | `Object` | `mark(int)` | `FilterInputStream`
`markSupported()` | `FilterInputStream` | `notify()` | `Object`
`notifyAll()` | `Object` | `read(byte[])` | `FilterInputStream`
`reset()` | `FilterInputStream` | `toString()` | `Object`
`wait()` | `Object` | `wait(long)` | `Object`
`wait(long, int)` | `Object` |  |

## See Also

`Checksum`, `FilterInputStream`, `InputStream`, `IOException`

Adler32 |  | CheckedOutputStream