1 package lejos.nxt.comm;
6 * Extends InputStream for BlueTooth; implements available()
7 * @author Roger Glassey revised on june 23, 2007, modified for Bluetooth2
9 public class BTInputStream extends InputStream {
10 private byte buf[] = new byte[256];
11 private int bufIdx = 0, bufSize = 0;
12 private BTConnection conn = null;
14 BTInputStream(BTConnection conn)
19 * Returns one byte as an integer between 0 and 255.
20 * Returns -1 if the end of the stream is reached.
21 * Does not return till some bytes are available.
25 if (bufIdx >= bufSize) bufSize = 0;
28 bufSize = conn.read(buf, buf.length, true);
29 if (bufSize <= 0) return -1;
32 return buf[bufIdx++] & 0xFF;
36 * returns the number of bytes in the input buffer - can be read without blocking
38 public int available()
40 if (bufIdx >= bufSize) bufSize = 0;
43 bufSize = conn.read(buf, buf.length, false);
45 return bufSize - bufIdx;
49 * the stream is restored to its original state - ready to receive more data.