1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| package org.globus.gsi.gssapi; |
17 |
| |
18 |
| import java.io.InputStream; |
19 |
| import java.io.IOException; |
20 |
| import java.util.LinkedList; |
21 |
| |
22 |
| import org.apache.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class TokenInputStream extends InputStream { |
29 |
| |
30 |
| private static Log logger = |
31 |
| LogFactory.getLog(TokenInputStream.class.getName()); |
32 |
| |
33 |
| private LinkedList tokens; |
34 |
| private byte [] buff; |
35 |
| private int index; |
36 |
| |
37 |
| private boolean closed; |
38 |
| |
39 |
726
| public TokenInputStream() {
|
40 |
726
| this.tokens = new LinkedList();
|
41 |
726
| this.index = 0;
|
42 |
726
| this.closed = false;
|
43 |
| } |
44 |
| |
45 |
| |
46 |
3840
| public void putToken(byte [] buf, int off, int len) {
|
47 |
3840
| if (buf == null || len <=0) {
|
48 |
488
| return;
|
49 |
| } |
50 |
| |
51 |
3352
| if (logger.isDebugEnabled()) {
|
52 |
0
| logger.debug("put token: " + len);
|
53 |
| } |
54 |
| |
55 |
3352
| byte[] localBuf = buf;
|
56 |
3352
| if (off != 0) {
|
57 |
0
| localBuf = new byte[len];
|
58 |
0
| System.arraycopy(buf, off, localBuf, 0, len);
|
59 |
| } |
60 |
| |
61 |
3352
| synchronized(this) {
|
62 |
3352
| if (this.buff == null ||
|
63 |
| this.buff != null && this.buff.length == this.index) { |
64 |
3352
| this.buff = localBuf;
|
65 |
3352
| this.index = 0;
|
66 |
| } else { |
67 |
0
| this.tokens.add(localBuf);
|
68 |
| } |
69 |
3352
| notify();
|
70 |
| } |
71 |
| } |
72 |
| |
73 |
0
| public int read(byte [] data)
|
74 |
| throws IOException { |
75 |
0
| return read(data, 0, data.length);
|
76 |
| } |
77 |
| |
78 |
4231
| public int read(byte [] data, int off, int len)
|
79 |
| throws IOException { |
80 |
4231
| if (logger.isDebugEnabled()) {
|
81 |
0
| logger.debug("read byte array: " + len);
|
82 |
| } |
83 |
| |
84 |
4231
| if (!checkData()) {
|
85 |
0
| return -1;
|
86 |
| } |
87 |
| |
88 |
4231
| int size = Math.min(len, buff.length-index);
|
89 |
4231
| System.arraycopy(buff, index, data, off, size);
|
90 |
4231
| index += size;
|
91 |
| |
92 |
4231
| return size;
|
93 |
| } |
94 |
| |
95 |
21155
| public int read()
|
96 |
| throws IOException { |
97 |
21155
| logger.debug("read byte");
|
98 |
| |
99 |
21155
| if (!checkData()) {
|
100 |
0
| return -1;
|
101 |
| } |
102 |
| |
103 |
21155
| return buff[index++] & 0xff;
|
104 |
| } |
105 |
| |
106 |
25386
| protected synchronized boolean checkData() {
|
107 |
25386
| try {
|
108 |
25386
| while(!hasData()) {
|
109 |
0
| wait();
|
110 |
0
| if (closed) {
|
111 |
0
| return false;
|
112 |
| } |
113 |
| } |
114 |
| } catch(Exception e) { |
115 |
0
| return false;
|
116 |
| } |
117 |
25386
| return true;
|
118 |
| } |
119 |
| |
120 |
37139
| protected boolean hasData() {
|
121 |
37139
| if (this.buff == null) {
|
122 |
488
| return false;
|
123 |
| } |
124 |
| |
125 |
36651
| if (this.buff.length == this.index) {
|
126 |
3445
| if (tokens.isEmpty()) {
|
127 |
3445
| return false;
|
128 |
| } else { |
129 |
0
| this.buff = (byte[])tokens.removeFirst();
|
130 |
0
| this.index = 0;
|
131 |
0
| return true;
|
132 |
| } |
133 |
| } |
134 |
33206
| return true;
|
135 |
| } |
136 |
| |
137 |
11753
| public int available()
|
138 |
| throws IOException { |
139 |
11753
| if (!hasData()) {
|
140 |
3933
| return 0;
|
141 |
| } else { |
142 |
7820
| return buff.length-index;
|
143 |
| } |
144 |
| } |
145 |
| |
146 |
0
| public void close()
|
147 |
| throws IOException { |
148 |
0
| logger.debug("close() called");
|
149 |
0
| synchronized(this) {
|
150 |
0
| this.closed = true;
|
151 |
0
| notify();
|
152 |
| } |
153 |
| } |
154 |
| |
155 |
0
| public String toString() {
|
156 |
0
| return tokens.toString() + " " + index + " " + buff.length;
|
157 |
| } |
158 |
| |
159 |
| } |