/
PacketOutputStream.java
63 lines (51 loc) · 1.93 KB
/
PacketOutputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package javaw2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class PacketOutputStream extends ByteArrayOutputStream {
private byte[] byteBuffer = new byte[1];
private byte[] shortBuffer = new byte[2];
private byte[] intBuffer = new byte[4];
private byte[] longBuffer = new byte[8];
public synchronized void putByte(byte val) throws IOException {
byteBuffer[0] = (byte) (val >>> 8);
write(byteBuffer);
}
public synchronized void putBoolean(boolean val) throws IOException {
byteBuffer[0] = (byte) (val ? 1 : 0);
write(byteBuffer);
}
public synchronized void putChar(char val) throws IOException {
shortBuffer[1] = (byte) (val >>> 8);
shortBuffer[0] = (byte) (val);
write(shortBuffer);
}
public synchronized void putShort(short val) throws IOException {
shortBuffer[1] = (byte) (val >>> 8);
shortBuffer[0] = (byte) (val);
write(shortBuffer);
}
public synchronized void putInt(int val) throws IOException {
intBuffer[3] = (byte) (val >>> 24);
intBuffer[2] = (byte) (val >>> 16);
intBuffer[1] = (byte) (val >>> 8);
intBuffer[0] = (byte) (val);
write(intBuffer);
}
public synchronized void putFloat(float val) throws IOException {
putInt(Float.floatToIntBits(val));
}
public synchronized void putLong(long val) throws IOException {
longBuffer[7] = (byte) (val >>> 56);
longBuffer[6] = (byte) (val >>> 48);
longBuffer[5] = (byte) (val >>> 40);
longBuffer[4] = (byte) (val >>> 32);
longBuffer[3] = (byte) (val >>> 24);
longBuffer[2] = (byte) (val >>> 16);
longBuffer[1] = (byte) (val >>> 8);
longBuffer[0] = (byte) (val);
write(longBuffer);
}
public synchronized void putDouble(double val) throws IOException {
putLong(Double.doubleToLongBits(val));
}
}