5 public class SaslInputStream extends InputStream
7 static final boolean DoEncrypt = true;
8 static final boolean DoDebug = false;
9 private static int BUFFERSIZE = 16384;
11 // if bufferend < bufferstart, we've wrapped around
12 private byte[] buffer=new byte[BUFFERSIZE];
13 private int bufferstart = 0;
14 private int bufferend = 0;
17 private GenericCommon conn;
19 public InputStream in;
21 public SaslInputStream(InputStream in, GenericCommon conn)
24 System.err.println("DEBUG constructing SaslInputStream");
30 public synchronized int available() throws IOException
32 int ina = in.available();
38 private void buffer_add(byte[] str,int len) throws IOException
47 /* xxx this can be optimized */
48 for (int lup=0;lup<len;lup++) {
49 buffer[bufferend]=b[lup];
50 bufferend = (bufferend + 1) % BUFFERSIZE;
53 if (size >= BUFFERSIZE) {
54 throw new IOException();
59 private void buffer_add(byte[] str) throws IOException
61 buffer_add(str,str.length);
64 private void readsome() throws IOException
66 int len=in.available();
69 System.err.println("DEBUG in readsome(), avail " + len);
72 if (len > BUFFERSIZE || len == 0)
75 byte[]tmp=new byte[len];
80 buffer_add( conn.decode(tmp,len) );
87 public synchronized void close() throws IOException
92 public synchronized void reset() throws IOException
94 throw new IOException();
97 public synchronized void mark(int readlimit)
102 public boolean markSupported()
107 /* read a single byte */
108 public synchronized int read() throws IOException
113 System.err.println("DEBUG in read(), size " + size);
121 System.err.println("DEBUG read() returning -1");
126 ret = buffer[bufferstart];
127 bufferstart = (bufferstart + 1) % BUFFERSIZE;
131 System.err.println("DEBUG read() returning " + ret);
136 public synchronized int read(byte b[]) throws IOException
138 return read(b,0,b.length);
141 public synchronized int read(byte b[],
143 int len) throws IOException
146 System.err.println("DEBUG in read(b, off, len), size " + size);
148 if (off < 0 || len < 0) {
149 throw new IndexOutOfBoundsException();
155 // block only if we need to
160 System.err.println("DEBUG read(b, off, len) returning -1");
167 for (l = off; l < len + off; l++) {
168 if (bufferstart == bufferend) break;
170 b[l] = buffer[bufferstart];
171 bufferstart = (bufferstart + 1) % BUFFERSIZE;
176 System.err.println("DEBUG read() returning " + (l - off));
181 public synchronized long skip(long n) throws IOException
196 bufferstart = bufferend = size = 0;
198 // we've got all the data we need to skip
200 bufferstart = (int) ((bufferstart + toskip) % BUFFERSIZE);
204 // skipped the full amount