Methods Summary |
---|
private void | checkFirstWrite()Checks to see if the data output streams need to be opened.
if (firstWrite == true) {
firstWrite = false;
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
}
|
public byte[] | getBytes(int length)Read an array of bytes from the data stream, starting at the current
index. The number of bytes to be read is specified by
length . The index is advanced by length .
byte[] buffer = new byte[length];
for (int i = 0; i < length; i++) {
buffer[i] = data[index++];
}
return buffer;
|
public byte[] | getData()Returns the contents of the packet as a byte array.
byte[] buffer = new byte[0];
if (firstWrite == false) {
dos.close();
buffer = bos.toByteArray();
bos.close();
}
return buffer;
|
public int | getInt()Read an integer (32-bit) value from the data stream, starting at the
current index.
int x1 = ((int)(data[index++] & 0xff));
int x2 = ((int)(data[index++] & 0xff) << 8);
int x3 = ((int)(data[index++] & 0xff) << 16);
int x4 = ((int)(data[index++] & 0xff) << 24);
return (x1 | x2 | x3 | x4);
|
public long | getLong()Read a long (64-bit) value from data stream, starting at the current
index.
long x1 = ((long)(data[index++] & 0xff));
long x2 = ((long)(data[index++] & 0xff) << 8);
long x3 = ((long)(data[index++] & 0xff) << 16);
long x4 = ((long)(data[index++] & 0xff) << 24);
long x5 = ((long)(data[index++] & 0xff) << 32);
long x6 = ((long)(data[index++] & 0xff) << 40);
long x7 = ((long)(data[index++] & 0xff) << 48);
long x8 = ((long)(data[index++] & 0xff) << 56);
return (x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8);
|
public short | getShort()Read a short (16-bit) value from the data stream, starting at the
current index.
short x1 = ((short)(data[index++] & 0xff));
short x2 = (short)((data[index++] & 0xff) << 8);
return (short)(x1 | x2);
|
public java.lang.String | getString()Read a string from the data stream, starting at the current index. The
string is assumed to be terminated by a null character.
int offset = index;
int len = 0;
/* Determine length of string */
for (int i = index; i < data.length; i++) {
if (data[i] == '\0") {
break;
}
len++;
}
/* update index */
index += len + 1;
return new String(data, offset, len);
|
public void | putBytes(byte[] buf)Write an array of bytes into the data stream, starting at the current
index.
checkFirstWrite();
dos.write(buf, 0, buf.length);
|
public void | putInt(int x)Write an integer (32-bit) value into the data stream and advance the
index.
checkFirstWrite();
dos.writeByte((byte) (x & 0xff));
dos.writeByte((byte)((x >> 8) & 0xff));
dos.writeByte((byte)((x >> 16) & 0xff));
dos.writeByte((byte)((x >> 24) & 0xff));
|
public void | putLong(long x)Write a long (64-bit) value into the data stream and advance the index.
checkFirstWrite();
dos.writeByte((byte) (x & 0xff));
dos.writeByte((byte)((x >> 8) & 0xff));
dos.writeByte((byte)((x >> 16) & 0xff));
dos.writeByte((byte)((x >> 24) & 0xff));
dos.writeByte((byte)((x >> 32) & 0xff));
dos.writeByte((byte)((x >> 40) & 0xff));
dos.writeByte((byte)((x >> 48) & 0xff));
dos.writeByte((byte)((x >> 56) & 0xff));
|
public void | putShort(short x)Write a short (16-bit) value into the data stream and advance the
index.
checkFirstWrite();
dos.writeByte((byte) (x & 0xff));
dos.writeByte((byte)((x >> 8) & 0xff));
|
public void | putString(java.lang.String s)Write a string to the data stream, starting at the current index. The
characters in the string are written, followed by a null-character
terminator.
checkFirstWrite();
dos.writeBytes(s);
dos.writeByte(0);
|