aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/pkix/src/main/java/org/spongycastle/cms/CMSProcessableInputStream.java
blob: fc644c95733ee8deba0e4d13a4374c345ede4b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
    }
}