net.jini.url.httpmd
Class MdInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by net.jini.url.httpmd.MdInputStream
All Implemented Interfaces:
Closeable

 class MdInputStream
extends InputStream

An input stream that checks the contents of another input stream against a message digest. The stream throws WrongMessageDigestException if the end of the other input stream is reached and the contents did not have the expected message digest. The message digest is not checked unless the end of the input stream is reached.

Author:
Sun Microsystems, Inc.

Field Summary
private  boolean checked
          Set to true when the contents have been checked against the message digest.
private  byte[] expectedDigest
          The expected digest.
private  String failed
          The exception message if the message digest was incorrect, or null if no exception should be thrown.
private  InputStream in
          The input stream whose contents should be checked.
private static Logger logger
          Logger.
private  MessageDigest messageDigest
          The object to use to compute the message digest of the stream contents.
private static int SKIP_BUFFER_SIZE
          The size buffer to use when skipping input.
private  byte[] skipBuffer
          Set to the buffer to use when skipping input.
private  URL url
          The HTTPMD URL associated with the input stream being checked.
 
Constructor Summary
MdInputStream(URL url, InputStream in, MessageDigest messageDigest, byte[] expectedDigest)
          Creates an input stream that checks the contents of another input stream against a message digest.
 
Method Summary
 int available()
           
private  void checkDigest()
          Checks the message digest.
private  void checkFailed()
          Throws an exception if the message digest was found to be incorrect.
 void close()
           
 int read()
           
 int read(byte[] b, int off, int len)
           
 long skip(long n)
           
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final Logger logger
Logger.


SKIP_BUFFER_SIZE

private static final int SKIP_BUFFER_SIZE
The size buffer to use when skipping input.

See Also:
Constant Field Values

url

private final URL url
The HTTPMD URL associated with the input stream being checked.


in

private final InputStream in
The input stream whose contents should be checked.


messageDigest

private final MessageDigest messageDigest
The object to use to compute the message digest of the stream contents.


expectedDigest

private final byte[] expectedDigest
The expected digest.


skipBuffer

private byte[] skipBuffer
Set to the buffer to use when skipping input.


checked

private boolean checked
Set to true when the contents have been checked against the message digest.


failed

private String failed
The exception message if the message digest was incorrect, or null if no exception should be thrown.

Constructor Detail

MdInputStream

MdInputStream(URL url,
              InputStream in,
              MessageDigest messageDigest,
              byte[] expectedDigest)
Creates an input stream that checks the contents of another input stream against a message digest. The stream throws WrongMessageDigestException if the end of the other input stream is reached and the contents did not have the expected message digest.

Parameters:
url - the HTTPMD URL associated with the input stream being checked
in - the input stream whose contents should be checked
messageDigest - the object to use for computing the message digest of the stream contents
expectedDigest - the expected message digest of the stream contents
Throws:
NullPointerException - if any of the arguments is null
Method Detail

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

checkFailed

private void checkFailed()
                  throws WrongMessageDigestException
Throws an exception if the message digest was found to be incorrect. Call this method before performing any I/O operations to insure that the same exception continues to be thrown after an incorrect message digest is discovered.

Throws:
WrongMessageDigestException

checkDigest

private void checkDigest()
                  throws WrongMessageDigestException
Checks the message digest. Call this method when the end of the other input stream is reached.

Throws:
WrongMessageDigestException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Overrides:
skip in class InputStream
Throws:
IOException

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException


Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.