open class DigestOutputStream : FilterOutputStream
kotlin.Any
   ↳ java.io.OutputStream
   ↳ java.io.FilterOutputStream
   ↳ java.security.DigestOutputStream

A transparent stream that updates the associated message digest using the bits going through the stream.

To complete the message digest computation, call one of the digest methods on the associated message digest after your calls to one of this digest output stream's #write(int) methods.

It is possible to turn this stream on or off (see on). When it is on, a call to one of the write methods results in an update on the message digest. But when it is off, the message digest is not updated. The default is for the stream to be on.

Summary

Public constructors

Creates a digest output stream, using the specified output stream and message digest.

Public methods
open MessageDigest!

Returns the message digest associated with this stream.

open Unit

on(on: Boolean)

Turns the digest function on or off.

open Unit

Associates the specified message digest with this stream.

open String

toString()

Prints a string representation of this digest output stream and its associated message digest object.

open Unit

write(b: ByteArray!, off: Int, len: Int)

Updates the message digest (if the digest function is on) using the specified subarray, and in any case writes the subarray to the output stream.

open Unit

write(b: Int)

Updates the message digest (if the digest function is on) using the specified byte, and in any case writes the byte to the output stream.

Inherited functions

From class FilterOutputStream

Unit close()

Closes this output stream and releases any system resources associated with the stream.

When not already closed, the close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

Unit flush()

Flushes this output stream and forces any buffered output bytes to be written out to the stream.

The flush method of FilterOutputStream calls the flush method of its underlying output stream.

Unit write(b: ByteArray!)

Writes b.length bytes to this output stream.

The write method of FilterOutputStream calls its write method of three arguments with the arguments b, 0, and b.length.

Note that this method does not call the one-argument write method of its underlying output stream with the single argument b.

From class OutputStream

OutputStream! nullOutputStream()

Returns a new OutputStream which discards all bytes. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect.

While the stream is open, the write(int), write(byte[]), and write(byte[], int, int) methods do nothing. After the stream has been closed, these methods all throw IOException.

The flush() method does nothing.

Properties
MessageDigest!

The message digest associated with this stream.

Inherited properties

From class FilterOutputStream

OutputStream! out

The underlying output stream to be filtered.

Public constructors

DigestOutputStream

DigestOutputStream(
    stream: OutputStream!,
    digest: MessageDigest!)

Creates a digest output stream, using the specified output stream and message digest.

Parameters
stream OutputStream!: the output stream.
digest MessageDigest!: the message digest to associate with this stream.

Public methods

getMessageDigest

open fun getMessageDigest(): MessageDigest!

Returns the message digest associated with this stream.

Return
MessageDigest! the message digest associated with this stream.

on

open fun on(on: Boolean): Unit

Turns the digest function on or off. The default is on. When it is on, a call to one of the write methods results in an update on the message digest. But when it is off, the message digest is not updated.

Parameters
on Boolean: true to turn the digest function on, false to turn it off.

setMessageDigest

open fun setMessageDigest(digest: MessageDigest!): Unit

Associates the specified message digest with this stream.

Parameters
digest MessageDigest!: the message digest to be associated with this stream.

toString

open fun toString(): String

Prints a string representation of this digest output stream and its associated message digest object.

Return
String a string representation of the object.

write

open fun write(
    b: ByteArray!,
    off: Int,
    len: Int
): Unit

Updates the message digest (if the digest function is on) using the specified subarray, and in any case writes the subarray to the output stream. That is, if the digest function is on (see on), this method calls update on the message digest associated with this stream, passing it the subarray specifications. This method then writes the subarray bytes to the output stream, blocking until the bytes are actually written.

Parameters
b ByteArray!: the array containing the subarray to be used for updating and writing to the output stream.
off Int: the offset into b of the first byte to be updated and written.
len Int: the number of bytes of data to be updated and written from b, starting at offset off.
Exceptions
java.io.IOException if an I/O error occurs.

write

open fun write(b: Int): Unit

Updates the message digest (if the digest function is on) using the specified byte, and in any case writes the byte to the output stream. That is, if the digest function is on (see on), this method calls update on the message digest associated with this stream, passing it the byte b. This method then writes the byte to the output stream, blocking until the byte is actually written.

Parameters
b Int: the byte to be used for updating and writing to the output stream.
Exceptions
java.io.IOException if an I/O error occurs.

Properties

digest

protected var digest: MessageDigest!

The message digest associated with this stream.

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-02-26 UTC.