Class SignatureTrailerInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable
Overview
This class is a specializedTailStrippingInputStream that wraps a SignatureContext
produced stream. The wrapped context emits [body][signature]; this
stream transparently strips the trailing signature bytes from the output and
optionally delivers a defensive copy of the signature to a user-supplied
callback.
The signature length is obtained from TagEngine.tagLength()
and the upstream stream is obtained via
TagEngine.wrap(java.io.InputStream). Resource ownership is
explicit: close() closes both the underlying stream chain and the
SignatureContext.
Usage
SignatureContext sc = session.createContext("Ed25519", KeyUsage.SIGN, privateKey, spec);
try (InputStream in = new SignatureTrailerInputStream(
sc,
originalContent.getStream(),
8192,
sig -> ctx.put(SIG_KEY, sig))) {
// read 'in' to obtain only the body; signature is delivered to the callback
in.transferTo(out);
}
Thread-safety
Instances are not thread-safe. Use from a single thread and do not share across readers.-
Constructor Summary
ConstructorsConstructorDescriptionSignatureTrailerInputStream(SignatureContext sc, InputStream upstream, int bufferSize, Consumer<byte[]> onSignature) Creates a new signature-stripping stream that passes through only the body while capturing the trailing signature tag. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the stream and the associatedSignatureContext.protected voidprocessTail(byte[] tail) Receives the stripped tail bytes and forwards a defensive copy to the registered callback.Methods inherited from class TailStrippingInputStream
available, markSupported, read, read, reset, tailLengthMethods inherited from class InputStream
mark, nullInputStream, read, readAllBytes, readNBytes, readNBytes, skip, skipNBytes, transferTo
-
Constructor Details
-
SignatureTrailerInputStream
public SignatureTrailerInputStream(SignatureContext sc, InputStream upstream, int bufferSize, Consumer<byte[]> onSignature) throws IOException Creates a new signature-stripping stream that passes through only the body while capturing the trailing signature tag.This constructor immediately calls
TagEngine.wrap(java.io.InputStream)to obtain the producing stream and configures the tail length toTagEngine.tagLength().- Parameters:
sc- the signature context that wraps the upstream stream and provides tag metadata; must not be nullupstream- the upstream stream that yields[body][signature]; must not be nullbufferSize- internal buffer size used by the tail stripper; must be positiveonSignature- optional callback invoked exactly once with a defensive copy of the finalized signature bytes; may be null- Throws:
IOException- if initializing the underlying wrapped stream failsNullPointerException- ifscorupstreamis null
-
-
Method Details
-
processTail
Receives the stripped tail bytes and forwards a defensive copy to the registered callback.If no callback is set or the tail is
null, this method returns without action. Any runtime exception thrown by the callback is caught and ignored so that stream semantics remain unaffected.- Specified by:
processTailin classTailStrippingInputStream- Parameters:
tail- the stripped signature bytes, ornullif no tail was detected- Throws:
IOException- never thrown by this implementation, preserved for the template method contract
-
close
Closes the stream and the associatedSignatureContext.This method first closes the underlying stream chain (draining to EOF if required by the base class), then closes the signature context. If closing the underlying stream throws an
IOException, that exception is rethrown after attempting to close the context.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classTailStrippingInputStream- Throws:
IOException- if closing the underlying stream fails
-