~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/bcpg/UserAttributeSubpacketInputStream.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.bcpg;
 
2
 
 
3
import java.io.*;
 
4
 
 
5
import org.bouncycastle.bcpg.attr.ImageAttribute;
 
6
 
 
7
/**
 
8
 * reader for user attribute sub-packets
 
9
 */
 
10
public class UserAttributeSubpacketInputStream
 
11
    extends InputStream implements UserAttributeSubpacketTags
 
12
{
 
13
    InputStream    in;
 
14
    
 
15
    public UserAttributeSubpacketInputStream(
 
16
        InputStream    in)
 
17
    {
 
18
        this.in = in;
 
19
    }
 
20
    
 
21
    public int available()
 
22
        throws IOException
 
23
    {
 
24
        return in.available();
 
25
    }
 
26
    
 
27
    public int read()
 
28
        throws IOException
 
29
    {
 
30
        return in.read();
 
31
    }
 
32
    
 
33
    private void readFully(
 
34
        byte[]    buf,
 
35
        int       off,
 
36
        int       len)
 
37
        throws IOException
 
38
    {
 
39
        if (len > 0)
 
40
        {
 
41
            int    b = this.read();
 
42
            
 
43
            if (b < 0)
 
44
            {
 
45
                throw new EOFException();
 
46
            }
 
47
            
 
48
            buf[off] = (byte)b;
 
49
            off++;
 
50
            len--;
 
51
        }
 
52
        
 
53
        while (len > 0)
 
54
        {
 
55
            int    l = in.read(buf, off, len);
 
56
            
 
57
            if (l < 0)
 
58
            {
 
59
                throw new EOFException();
 
60
            }
 
61
            
 
62
            off += l;
 
63
            len -= l;
 
64
        }
 
65
    }
 
66
    
 
67
    public UserAttributeSubpacket readPacket()
 
68
        throws IOException
 
69
    {
 
70
        int            l = this.read();
 
71
        int            bodyLen = 0;
 
72
        
 
73
        if (l < 0)
 
74
        {
 
75
            return null;
 
76
        }
 
77
 
 
78
        if (l < 192)
 
79
        {
 
80
            bodyLen = l;
 
81
        }
 
82
        else if (l <= 223)
 
83
        {
 
84
            bodyLen = ((l - 192) << 8) + (in.read()) + 192;
 
85
        }
 
86
        else if (l == 255)
 
87
        {
 
88
            bodyLen = (in.read() << 24) | (in.read() << 16) |  (in.read() << 8)  | in.read();
 
89
        }
 
90
        else
 
91
        {
 
92
            // TODO Error?
 
93
        }
 
94
 
 
95
       int        tag = in.read();
 
96
 
 
97
       if (tag < 0)
 
98
       {
 
99
               throw new EOFException("unexpected EOF reading user attribute sub packet");
 
100
       }
 
101
       
 
102
       byte[]    data = new byte[bodyLen - 1];
 
103
 
 
104
       this.readFully(data, 0, data.length);
 
105
       
 
106
       int       type = tag;
 
107
 
 
108
       switch (type)
 
109
       {
 
110
       case IMAGE_ATTRIBUTE:
 
111
           return new ImageAttribute(data);
 
112
       }
 
113
 
 
114
       return new UserAttributeSubpacket(type, data);
 
115
    }
 
116
}