check more i/o code in
[IRC.git] / Robust / src / ClassLibrary / Socket.java
1 public class Socket {
2     /* Data pending flag */
3     external flag IOPending;    
4     /* File Descriptor */
5     int fd;
6     private SocketInputStream sin;
7     
8     public Socket() {
9         sin=new SocketInputStream(this);
10         sout=new SocketOutputStream(this);
11     }
12
13     public InputStream getInputStream() {
14         return sin;
15     }
16
17     public OutputSream getOutputStream() {
18         return sout;
19     }
20
21     public Socket(String host, int port) {
22         InetAddress address=InetAddress.getByName(host);
23         fd=nativeBind(address.getAddress(), port);
24         nativeConnect(fd, address.getAddress(), port);
25     }
26     
27     public Socket(InetAddress address, int port) {
28         fd=nativeBind(address.getAddress(), port);
29         nativeConnect(fd, address.getAddress(), port);
30     }
31
32     public static native int nativeBind(byte[] address, int port);
33
34     public native int nativeConnect(int fd, byte[] address, int port);
35     
36     int setFD(int filed) {
37         fd=filed;
38     }
39
40     public int read(byte[] b) {
41         return nativeRead(b);
42     }
43     public void write(byte[] b) {
44         nativeWrite(b, 0, b.length);
45     }
46
47     public void write(byte[] b, int offset, int leng) {
48         nativeWrite(b, offset, len);
49     }
50
51     private native int nativeRead(byte[] b);
52     private native void nativeWrite(byte[] b, int offset, int len);
53     private native void nativeClose();
54
55     public void close() {
56         nativeClose();
57     }
58 }