~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cms/CMSProcessableFile.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.cms;
 
2
 
 
3
import java.io.BufferedInputStream;
 
4
import java.io.File;
 
5
import java.io.FileInputStream;
 
6
import java.io.IOException;
 
7
import java.io.InputStream;
 
8
import java.io.OutputStream;
 
9
 
 
10
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
 
11
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
 
12
 
 
13
/**
 
14
 * a holding class for a file of data to be processed.
 
15
 */
 
16
public class CMSProcessableFile
 
17
    implements CMSTypedData, CMSReadable
 
18
{
 
19
    private static final int DEFAULT_BUF_SIZE = 32 * 1024;
 
20
 
 
21
    private final ASN1ObjectIdentifier type;
 
22
    private final File file;
 
23
    private final byte[] buf;
 
24
 
 
25
    public CMSProcessableFile(
 
26
        File file)
 
27
    {
 
28
        this(file, DEFAULT_BUF_SIZE);
 
29
    }
 
30
    
 
31
    public CMSProcessableFile(
 
32
        File file,
 
33
        int  bufSize)
 
34
    {
 
35
        this(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()), file, bufSize);
 
36
    }
 
37
 
 
38
    public CMSProcessableFile(
 
39
        ASN1ObjectIdentifier type,
 
40
        File file,
 
41
        int  bufSize)
 
42
    {
 
43
        this.type = type;
 
44
        this.file = file;
 
45
        buf = new byte[bufSize];
 
46
    }
 
47
 
 
48
    public InputStream getInputStream()
 
49
        throws IOException, CMSException
 
50
    {
 
51
        return new BufferedInputStream(new FileInputStream(file), DEFAULT_BUF_SIZE);
 
52
    }
 
53
 
 
54
    public void write(OutputStream zOut)
 
55
        throws IOException, CMSException
 
56
    {
 
57
        FileInputStream     fIn = new FileInputStream(file);
 
58
        int                 len;
 
59
        
 
60
        while ((len = fIn.read(buf, 0, buf.length)) > 0)
 
61
        {
 
62
            zOut.write(buf, 0, len);
 
63
        }
 
64
        
 
65
        fIn.close();
 
66
    }
 
67
 
 
68
    /**
 
69
     * Return the file handle.
 
70
     */
 
71
    public Object getContent()
 
72
    {
 
73
        return file;
 
74
    }
 
75
 
 
76
    public ASN1ObjectIdentifier getContentType()
 
77
    {
 
78
        return type;
 
79
    }
 
80
}