/
StdInClient.java
62 lines (56 loc) · 2.02 KB
/
StdInClient.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
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class StdInClient
{
public static void main (String [] args) throws Exception
{
InputStream in = new FileInputStream ("/proc/self/fd/0");
while (true) {
long t0 = System.currentTimeMillis ();
long sum = 0;
int N = 10000000;
for (int i = 0; i < N; i++) {
byte [] type = readBytes (in, 4);
int len = readInt (in);
byte [] msg = readBytes (in, len);
processMessage (type, msg);
sum += msg.length + 8;
}
long t1 = System.currentTimeMillis ();
long t = t1 - t0;
System.out.printf ("Time for %d msg: %d; speed: %d msg/s; %.1f MB/s\n",
N, t, N * 1000L / t, sum * 0.001 / t);
}
}
private static byte [] readBytes (InputStream in, int expectedSize) throws IOException
{
final byte[] buffer = new byte[expectedSize];
int totalReadSize = 0;
while (totalReadSize < expectedSize) {
int readSize = in.read(buffer, totalReadSize, expectedSize - totalReadSize);
if (readSize < 0) throw new EOFException ();
totalReadSize += readSize;
}
return buffer;
}
private static final int readInt (InputStream in) throws IOException
{
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
private static final int readInt (InputStream in) throws IOException
{
byte [] b = readBytes (in, 4);
return (((b[0] & 0xFF) << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + ((b[3] & 0xFF) << 0));
}
private static void processMessage (byte [] type, byte [] msg)
{
}
}