1 package lejos.nxt.comm;
6 * Extends InputStream for BlueTooth; implements available()
7 * @author Roger Glassey revised on june 23, 2007
9 public class BTInputStream extends InputStream {
10 private byte buf[] = new byte[256];
11 private int bufIdx = 0, bufSize = 0;
14 * Returns one byte as an integer between 0 and 255.
15 * Returns -1 if the end of the stream is reached.
16 * Does not return till some bytes are available.
20 if (bufIdx >= bufSize) bufSize = 0;
21 while(bufSize == 0) bufSize = available();
22 return buf[bufIdx++] & 0xFF;
26 * returns the number of bytes in the input buffer - can be read without blocking
28 public int available()
30 if (bufIdx >= bufSize) bufSize = 0;
33 bufSize = Bluetooth.readPacket(buf, 256);
35 return bufSize - bufIdx;
39 * the stream is restored to its original state - ready to receive more data.