~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cms/CMSTypedStream.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.FilterInputStream;
 
5
import java.io.IOException;
 
6
import java.io.InputStream;
 
7
 
 
8
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
 
9
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
 
10
import org.bouncycastle.util.io.Streams;
 
11
 
 
12
public class CMSTypedStream
 
13
{
 
14
    private static final int BUF_SIZ = 32 * 1024;
 
15
    
 
16
    private final ASN1ObjectIdentifier      _oid;
 
17
    private final InputStream _in;
 
18
 
 
19
    public CMSTypedStream(
 
20
        InputStream in)
 
21
    {
 
22
        this(PKCSObjectIdentifiers.data.getId(), in, BUF_SIZ);
 
23
    }
 
24
    
 
25
    public CMSTypedStream(
 
26
         String oid,
 
27
         InputStream in)
 
28
    {
 
29
        this(new ASN1ObjectIdentifier(oid), in, BUF_SIZ);
 
30
    }
 
31
    
 
32
    public CMSTypedStream(
 
33
        String      oid,
 
34
        InputStream in,
 
35
        int         bufSize)
 
36
    {
 
37
        this(new ASN1ObjectIdentifier(oid), in, bufSize);
 
38
    }
 
39
 
 
40
    public CMSTypedStream(
 
41
         ASN1ObjectIdentifier oid,
 
42
         InputStream in)
 
43
    {
 
44
        this(oid, in, BUF_SIZ);
 
45
    }
 
46
 
 
47
    public CMSTypedStream(
 
48
        ASN1ObjectIdentifier      oid,
 
49
        InputStream in,
 
50
        int         bufSize)
 
51
    {
 
52
        _oid = oid;
 
53
        _in = new FullReaderStream(new BufferedInputStream(in, bufSize));
 
54
    }
 
55
 
 
56
    public ASN1ObjectIdentifier getContentType()
 
57
    {
 
58
        return _oid;
 
59
    }
 
60
    
 
61
    public InputStream getContentStream()
 
62
    {
 
63
        return _in;
 
64
    }
 
65
 
 
66
    public void drain() 
 
67
        throws IOException
 
68
    {
 
69
        Streams.drain(_in);
 
70
        _in.close();
 
71
    }
 
72
 
 
73
    private static class FullReaderStream extends FilterInputStream
 
74
    {
 
75
        FullReaderStream(InputStream in)
 
76
        {
 
77
            super(in);
 
78
        }
 
79
 
 
80
        public int read(byte[] buf, int off, int len) throws IOException
 
81
        {
 
82
            int totalRead = Streams.readFully(super.in, buf, off, len);
 
83
            return totalRead > 0 ? totalRead : -1;
 
84
        }
 
85
    }
 
86
}