Class DigesterRepresentation


  • public class DigesterRepresentation
    extends WrapperRepresentation
    Representation capable of computing a digest. It wraps another representation and allows to get the computed digest of the wrapped entity after reading or writing operations. The final digest value is guaranteed to be correct only after the wrapped representation has been entirely exhausted (that is to say read or written).

    This wrapper allows to get the computed digest at the same time the representation is read or written. It does not need two separate operations which may require specific attention for transient representations.
    Author:
    Jerome Louvel
    See Also:
    .
    • Constructor Detail

      • DigesterRepresentation

        public DigesterRepresentation​(Representation wrappedRepresentation)
                               throws java.security.NoSuchAlgorithmException
        Constructor.
        By default, the instance relies on the Digest.ALGORITHM_MD5 digest algorithm.
        Parameters:
        wrappedRepresentation - The wrapped representation.
        Throws:
        java.security.NoSuchAlgorithmException
      • DigesterRepresentation

        public DigesterRepresentation​(Representation wrappedRepresentation,
                                      java.lang.String algorithm)
                               throws java.security.NoSuchAlgorithmException
        Constructor.
        Parameters:
        wrappedRepresentation - The wrapped representation.
        algorithm - The digest algorithm.
        Throws:
        java.security.NoSuchAlgorithmException
    • Method Detail

      • checkDigest

        public boolean checkDigest()
        Check that the digest computed from the representation content and the digest declared by the representation are the same.
        Since this method relies on the computeDigest(String) method, and since this method reads entirely the representation's stream, user must take care of the content of the representation in case the latter is transient. WrapperRepresentation.isTransient()
        Returns:
        True if both digests are not null and equals.
      • checkDigest

        public boolean checkDigest​(java.lang.String algorithm)
        Check that the digest computed from the representation content and the digest declared by the representation are the same. It also first checks that the algorithms are the same.
        Since this method relies on the computeDigest(String) method, and since this method reads entirely the representation's stream, user must take care of the content of the representation in case the latter is transient. WrapperRepresentation.isTransient()
        Parameters:
        algorithm - The algorithm used to compute the digest to compare with. See constant values in Digest.
        Returns:
        True if both digests are not null and equals.
      • computeDigest

        public Digest computeDigest()
        Compute the representation digest according to MD5 algorithm.
        If case this algorithm is the same than the one provided at instantiation, the computation operation is made with the current stored computed value and does not require to exhaust entirely the representation's stream.
      • computeDigest

        public Digest computeDigest​(java.lang.String algorithm)
        Compute the representation digest according to the given algorithm.
        Since this method reads entirely the representation's stream, user must take care of the content of the representation in case the latter is transient. WrapperRepresentation.isTransient()
        Parameters:
        algorithm - The algorithm used to compute the digest. See constant values in Digest.
        Returns:
        The computed digest or null if the digest cannot be computed.
      • exhaust

        public long exhaust()
                     throws java.io.IOException
        Exhausts the content of the representation by reading it and silently discarding anything read.
        Overrides:
        exhaust in class WrapperRepresentation
        Returns:
        The number of bytes consumed or -1 if unknown.
        Throws:
        java.io.IOException
      • getChannel

        public java.nio.channels.ReadableByteChannel getChannel()
                                                         throws java.io.IOException
        Description copied from class: Representation
        Returns a channel with the representation's content.
        If it is supported by a file, a read-only instance of FileChannel is returned.
        This method is ensured to return a fresh channel for each invocation unless it is a transient representation, in which case null is returned.
        Overrides:
        getChannel in class WrapperRepresentation
        Returns:
        A channel with the representation's content.
        Throws:
        java.io.IOException
      • getComputedDigest

        public Digest getComputedDigest()
        Returns the current computed digest value of the representation. User must be aware that, if the representation has not been entirely read or written, the computed digest value may not be accurate.
        Returns:
        The current computed digest value.
      • getReader

        public java.io.Reader getReader()
                                 throws java.io.IOException
        Description copied from class: Representation
        Returns a characters reader with the representation's content. This method is ensured to return a fresh reader for each invocation unless it is a transient representation, in which case null is returned. If the representation has no character set defined, the system's default one will be used.
        Overrides:
        getReader in class WrapperRepresentation
        Returns:
        A reader with the representation's content.
        Throws:
        java.io.IOException
      • getStream

        public java.io.InputStream getStream()
                                      throws java.io.IOException
        Returns a stream with the representation's content. This method is ensured to return a fresh stream for each invocation unless it is a transient representation, in which case null is returned.
        The stream of the underlying representation is wrapped with a new instance of the DigestInputStream class, which allows to compute progressively the digest value.
        Overrides:
        getStream in class WrapperRepresentation
        Returns:
        A stream with the representation's content.
        Throws:
        java.io.IOException
      • getText

        public java.lang.String getText()
                                 throws java.io.IOException
        Description copied from class: Representation
        Converts the representation to a string value. Be careful when using this method as the conversion of large content to a string fully stored in memory can result in OutOfMemoryErrors being thrown.
        Overrides:
        getText in class WrapperRepresentation
        Returns:
        The representation as a string value.
        Throws:
        java.io.IOException
      • write

        public void write​(java.io.OutputStream outputStream)
                   throws java.io.IOException
        Writes the representation to a byte stream. This method is ensured to write the full content for each invocation unless it is a transient representation, in which case an exception is thrown.

        Note that the class implementing this method shouldn't flush or close the given OutputStream after writing to it as this will be handled by the Restlet connectors automatically.
        The output stream is wrapped with a new instance of the DigestOutputStream class, which allows to compute progressively the digest value.
        Overrides:
        write in class WrapperRepresentation
        Parameters:
        outputStream - The output stream.
        Throws:
        java.io.IOException
      • write

        public void write​(java.nio.channels.WritableByteChannel writableChannel)
                   throws java.io.IOException
        Description copied from class: Representation
        Writes the representation to a byte channel. This method is ensured to write the full content for each invocation unless it is a transient representation, in which case an exception is thrown.
        Overrides:
        write in class WrapperRepresentation
        Parameters:
        writableChannel - A writable byte channel.
        Throws:
        java.io.IOException
      • write

        public void write​(java.io.Writer writer)
                   throws java.io.IOException
        Description copied from class: Representation
        Writes the representation to a characters writer. This method is ensured to write the full content for each invocation unless it is a transient representation, in which case an exception is thrown.

        Note that the class implementing this method shouldn't flush or close the given Writer after writing to it as this will be handled by the Restlet connectors automatically.
        Overrides:
        write in class WrapperRepresentation
        Parameters:
        writer - The characters writer.
        Throws:
        java.io.IOException