01: package org.bouncycastle.bcpg;
02:
03: import java.io.*;
04:
05: import org.bouncycastle.bcpg.attr.ImageAttribute;
06:
07: /**
08: * reader for user attribute sub-packets
09: */
10: public class UserAttributeSubpacketInputStream extends InputStream
11: implements UserAttributeSubpacketTags {
12: InputStream in;
13:
14: public UserAttributeSubpacketInputStream(InputStream in) {
15: this .in = in;
16: }
17:
18: public int available() throws IOException {
19: return in.available();
20: }
21:
22: public int read() throws IOException {
23: return in.read();
24: }
25:
26: private void readFully(byte[] buf, int off, int len)
27: throws IOException {
28: if (len > 0) {
29: int b = this .read();
30:
31: if (b < 0) {
32: throw new EOFException();
33: }
34:
35: buf[off] = (byte) b;
36: off++;
37: len--;
38: }
39:
40: while (len > 0) {
41: int l = in.read(buf, off, len);
42:
43: if (l < 0) {
44: throw new EOFException();
45: }
46:
47: off += l;
48: len -= l;
49: }
50: }
51:
52: public UserAttributeSubpacket readPacket() throws IOException {
53: int l = this .read();
54: int bodyLen = 0;
55:
56: if (l < 0) {
57: return null;
58: }
59:
60: if (l < 192) {
61: bodyLen = l;
62: } else if (l < 223) {
63: bodyLen = ((l - 192) << 8) + (in.read()) + 192;
64: } else if (l == 255) {
65: bodyLen = (in.read() << 24) | (in.read() << 16)
66: | (in.read() << 8) | in.read();
67: }
68:
69: int tag = in.read();
70:
71: if (tag < 0) {
72: throw new EOFException(
73: "unexpected EOF reading user attribute sub packet");
74: }
75:
76: byte[] data = new byte[bodyLen - 1];
77:
78: this .readFully(data, 0, data.length);
79:
80: int type = tag;
81:
82: switch (type) {
83: case IMAGE_ATTRIBUTE:
84: return new ImageAttribute(data);
85: }
86:
87: return new UserAttributeSubpacket(type, data);
88: }
89: }
|