summaryrefslogtreecommitdiffstats
path: root/bcpkix/src/main/java/org/bouncycastle/cms/CMSProcessableFile.java
blob: b1e4527792ff8e2a2383414b81c9bf04d90360c9 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.bouncycastle.cms;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;

/**
 * a holding class for a file of data to be processed.
 */
public class CMSProcessableFile
    implements CMSTypedData, CMSReadable
{
    private static final int DEFAULT_BUF_SIZE = 32 * 1024;

    private final ASN1ObjectIdentifier type;
    private final File file;
    private final byte[] buf;

    public CMSProcessableFile(
        File file)
    {
        this(file, DEFAULT_BUF_SIZE);
    }
    
    public CMSProcessableFile(
        File file,
        int  bufSize)
    {
        this(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()), file, bufSize);
    }

    public CMSProcessableFile(
        ASN1ObjectIdentifier type,
        File file,
        int  bufSize)
    {
        this.type = type;
        this.file = file;
        buf = new byte[bufSize];
    }

    public InputStream getInputStream()
        throws IOException, CMSException
    {
        return new BufferedInputStream(new FileInputStream(file), DEFAULT_BUF_SIZE);
    }

    public void write(OutputStream zOut)
        throws IOException, CMSException
    {
        FileInputStream     fIn = new FileInputStream(file);
        int                 len;
        
        while ((len = fIn.read(buf, 0, buf.length)) > 0)
        {
            zOut.write(buf, 0, len);
        }
        
        fIn.close();
    }

    /**
     * Return the file handle.
     */
    public Object getContent()
    {
        return file;
    }

    public ASN1ObjectIdentifier getContentType()
    {
        return type;
    }
}