Java Reference
In-Depth Information
/**
* Read text one byte at a time. Cast each byte to char
* and then append to a Stringbuffer.
*/
public String receiveText () throws IOException {
byte ch;
fStrBuf.delete (0, fStrBuf.length ());
while ((ch = (byte)fPortInStream.read ())!= -1) {
fStrBuf.append ((char)ch);
// Use \rasanend of text marker.
if (ch =='\r') break;
}
return fStrBuf.toString ();
} // receiveText
/**
* Receives 2 bytes from Javelin and converts them to
*anint value using a ByteArrayInputStream.
*
* Javelin sends bytes in Big-Endian manner, which means
* the high order byte arrives first. For example, if
* the value 258 = 0x0102 were sent from the Javelin, the
* byte 0x01 arrives first and then 0x02.
*
* The readInt () method in DataInputStream, which wraps
* the ByteArrayInputStream, treats the four bytes in
* the array as a four byte int value. So to give a
* value 258, the values in the four byte array must
*goas:
*
* fByteArray=
[0]
[1]
[2]
[3]
*
[00000000] [00000000] [00000001] [00000010]
*
*Wetherefore place the first byte obtained from the
* Javelin into element 2 of the array and the second
* byte into element 3.
*/
public int receiveInt () throws IOException {
int input =0;
// Read high order byte
if ((input = fPortInStream.read ()) == -1)
throw new IOException ();
// A cast to byte truncates 3 top bytes from int value
fByteArray[2] = (byte)input;
 
Search WWH ::




Custom Search