diff options
Diffstat (limited to 'pkix/src/main/java/org/spongycastle/cms/CMSProcessableInputStream.java')
-rw-r--r-- | pkix/src/main/java/org/spongycastle/cms/CMSProcessableInputStream.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/pkix/src/main/java/org/spongycastle/cms/CMSProcessableInputStream.java b/pkix/src/main/java/org/spongycastle/cms/CMSProcessableInputStream.java new file mode 100644 index 00000000..fc644c95 --- /dev/null +++ b/pkix/src/main/java/org/spongycastle/cms/CMSProcessableInputStream.java @@ -0,0 +1,50 @@ +package org.spongycastle.cms; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.spongycastle.util.io.Streams; + +class CMSProcessableInputStream implements CMSProcessable, CMSReadable +{ + private InputStream input; + private boolean used = false; + + public CMSProcessableInputStream( + InputStream input) + { + this.input = input; + } + + public InputStream getInputStream() + { + checkSingleUsage(); + + return input; + } + + public void write(OutputStream zOut) + throws IOException, CMSException + { + checkSingleUsage(); + + Streams.pipeAll(input, zOut); + input.close(); + } + + public Object getContent() + { + return getInputStream(); + } + + private synchronized void checkSingleUsage() + { + if (used) + { + throw new IllegalStateException("CMSProcessableInputStream can only be used once"); + } + + used = true; + } +} |