-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatagramInputStream.java
More file actions
68 lines (59 loc) · 1.39 KB
/
DatagramInputStream.java
File metadata and controls
68 lines (59 loc) · 1.39 KB
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
64
65
66
67
68
package javaforce;
import java.io.*;
import java.net.*;
public class DatagramInputStream extends InputStream {
private static final int MAX = 1460;
private DatagramSocket ds;
private byte buffer[];
private int buffersize = 0, bufferpos = 0;
private boolean fillBuffer() {
while (buffersize == 0) {
byte data[] = new byte[MAX];
DatagramPacket pack = new DatagramPacket(data, MAX);
try {
ds.receive(pack);
} catch (Exception e) {
return false;
}
buffer = pack.getData();
buffersize = pack.getLength();
bufferpos = 0;
}
return true;
}
public DatagramInputStream(DatagramSocket ds) {
this.ds = ds;
}
public boolean markSupported() {
return false;
}
public int read() {
if (!fillBuffer()) {
return -1;
}
int ret = ((int) buffer[bufferpos++]) & 0xff;
buffersize--;
return ret;
}
public int read(byte buf[]) {
return read(buf, 0, buf.length);
}
public int read(byte buf[], int pos, int len) {
int ret;
if (!fillBuffer()) {
return -1;
}
if (len > buffersize) {
ret = buffersize;
System.arraycopy(buffer, bufferpos, buf, pos, buffersize);
buffersize = 0;
bufferpos = 0;
} else {
ret = len;
System.arraycopy(buffer, bufferpos, buf, pos, len);
buffersize -= len;
bufferpos += len;
}
return ret;
}
}