1 |
/* |
/* |
2 |
* $Id$ |
* SaslInputStream.java |
3 |
* Copyright (C) 2002 The Free Software Foundation |
* Copyright (C) 2002 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
49 |
* client. |
* client. |
50 |
* |
* |
51 |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
52 |
*/ |
*/ |
53 |
public class SaslInputStream extends FilterInputStream |
public class SaslInputStream |
54 |
|
extends FilterInputStream |
55 |
{ |
{ |
56 |
|
|
57 |
/* |
/* |
74 |
* @param sasl the SASL client |
* @param sasl the SASL client |
75 |
* @param in the underlying input stream |
* @param in the underlying input stream |
76 |
*/ |
*/ |
77 |
public SaslInputStream (SaslClient sasl, InputStream in) |
public SaslInputStream(SaslClient sasl, InputStream in) |
78 |
{ |
{ |
79 |
super (in); |
super(in); |
80 |
this.sasl = sasl; |
this.sasl = sasl; |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* Reads a single character. |
* Reads a single character. |
85 |
*/ |
*/ |
86 |
public int read () throws IOException |
public int read() |
87 |
{ |
throws IOException |
88 |
if (buf != null) |
{ |
89 |
{ |
if (buf != null) |
90 |
// Return next characer in buffer |
{ |
91 |
int c = (int) buf[pos++]; |
// Return next characer in buffer |
92 |
if (pos == buf.length) |
int c = (int) buf[pos++]; |
93 |
{ |
if (pos == buf.length) |
94 |
buf = null; |
{ |
95 |
} |
buf = null; |
96 |
return c; |
} |
97 |
} |
return c; |
98 |
int c = super.read (); |
} |
99 |
if (c == -1) |
int c = super.read(); |
100 |
{ |
if (c == -1) |
101 |
return c; |
{ |
102 |
} |
return c; |
103 |
byte[] bytes = new byte[1]; |
} |
104 |
byte[] unwrapped = sasl.unwrap (bytes, 0, 1); |
byte[] bytes = new byte[1]; |
105 |
// FIXME if we get 0 bytes, we have a problem |
byte[] unwrapped = sasl.unwrap(bytes, 0, 1); |
106 |
c = (int) unwrapped[0]; |
// FIXME if we get 0 bytes, we have a problem |
107 |
if (unwrapped.length > 1) |
c = (int) unwrapped[0]; |
108 |
{ |
if (unwrapped.length > 1) |
109 |
// Store in overflow buffer |
{ |
110 |
int l = unwrapped.length - 1; |
// Store in overflow buffer |
111 |
buf = new byte[l]; |
int l = unwrapped.length - 1; |
112 |
System.arraycopy (unwrapped, 1, buf, 0, l); |
buf = new byte[l]; |
113 |
pos = 0; |
System.arraycopy(unwrapped, 1, buf, 0, l); |
114 |
} |
pos = 0; |
115 |
return c; |
} |
116 |
} |
return c; |
117 |
|
} |
118 |
public int read (byte[] bytes) throws IOException |
|
119 |
{ |
public int read(byte[] bytes) |
120 |
return read (bytes, 0, bytes.length); |
throws IOException |
121 |
} |
{ |
122 |
|
return read(bytes, 0, bytes.length); |
123 |
|
} |
124 |
|
|
125 |
/** |
/** |
126 |
* Block read. |
* Block read. |
127 |
*/ |
*/ |
128 |
public int read (byte[] bytes, int off, int len) throws IOException |
public int read(byte[] bytes, int off, int len) |
129 |
{ |
throws IOException |
130 |
if (buf != null) |
{ |
131 |
{ |
if (buf != null) |
132 |
// Return bytes from buffer |
{ |
133 |
int l = buf.length; |
// Return bytes from buffer |
134 |
if (l - pos <= len) |
int l = buf.length; |
135 |
{ |
if (l - pos <= len) |
136 |
System.arraycopy (buf, pos, bytes, off, l); |
{ |
137 |
buf = null; |
System.arraycopy(buf, pos, bytes, off, l); |
138 |
return l; |
buf = null; |
139 |
} |
return l; |
140 |
else |
} |
141 |
{ |
else |
142 |
System.arraycopy (buf, pos, bytes, off, len); |
{ |
143 |
pos += len; |
System.arraycopy(buf, pos, bytes, off, len); |
144 |
return len; |
pos += len; |
145 |
} |
return len; |
146 |
} |
} |
147 |
int l = super.read (bytes, off, len); |
} |
148 |
if (l == -1) |
int l = super.read(bytes, off, len); |
149 |
{ |
if (l == -1) |
150 |
return l; |
{ |
151 |
} |
return l; |
152 |
byte[] unwrapped = sasl.unwrap (bytes, off, l); |
} |
153 |
int l2 = unwrapped.length; |
byte[] unwrapped = sasl.unwrap(bytes, off, l); |
154 |
if (l2 > len) |
int l2 = unwrapped.length; |
155 |
{ |
if (l2 > len) |
156 |
// Store excess bytes in buffer |
{ |
157 |
int d = l2 - len; |
// Store excess bytes in buffer |
158 |
buf = new byte[d]; |
int d = l2 - len; |
159 |
System.arraycopy (unwrapped, 0, bytes, off, len); |
buf = new byte[d]; |
160 |
System.arraycopy (unwrapped, len, buf, 0, d); |
System.arraycopy(unwrapped, 0, bytes, off, len); |
161 |
pos = 0; |
System.arraycopy(unwrapped, len, buf, 0, d); |
162 |
return len; |
pos = 0; |
163 |
} |
return len; |
164 |
else |
} |
165 |
{ |
else |
166 |
System.arraycopy (unwrapped, 0, bytes, off, l2); |
{ |
167 |
// Zero bytes from l2..l to ensure none of the original |
System.arraycopy(unwrapped, 0, bytes, off, l2); |
168 |
// bytes received can be read by the caller |
// Zero bytes from l2..l to ensure none of the original |
169 |
for (int i = l2; i < l; i++) |
// bytes received can be read by the caller |
170 |
{ |
for (int i = l2; i < l; i++) |
171 |
bytes[off + l2] = 0; |
{ |
172 |
} |
bytes[off + l2] = 0; |
173 |
return l2; |
} |
174 |
} |
return l2; |
175 |
} |
} |
176 |
|
} |
177 |
|
|
178 |
} |
} |
179 |
|
|