more bugs
[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     private SocketOutputStream sout;
8     
9     public Socket() {
10         sin=new SocketInputStream(this);
11         sout=new SocketOutputStream(this);
12     }
13
14     public InputStream getInputStream() {
15         return sin;
16     }
17
18     public OutputStream getOutputStream() {
19         return sout;
20     }
21
22     public Socket(String host, int port) {
23         InetAddress address=InetAddress.getByName(host);
24         fd=nativeBind(address.getAddress(), port);
25         nativeConnect(fd, address.getAddress(), port);
26     }
27     
28     public Socket(InetAddress address, int port) {
29         fd=nativeBind(address.getAddress(), port);
30         nativeConnect(fd, address.getAddress(), port);
31     }
32
33     public void connect(InetAddress address, int port) {
34         fd=nativeBind(address.getAddress(), port);
35         nativeConnect(fd, address.getAddress(), port);
36     }
37
38     public static native int nativeBind(byte[] address, int port);
39
40     public native int nativeConnect(int fd, byte[] address, int port);
41     
42     int setFD(int filed) {
43         fd=filed;
44     }
45
46     public int read(byte[] b) {
47         return nativeRead(b);
48     }
49     public void write(byte[] b) {
50         nativeWrite(b, 0, b.length);
51     }
52
53     public void write(byte[] b, int offset, int len) {
54         nativeWrite(b, offset, len);
55     }
56
57     private native void nativeBindFD(int fd);
58     private native int nativeRead(byte[] b);
59     private native void nativeWrite(byte[] b, int offset, int len);
60     private native void nativeClose();
61
62     public void close() {
63         nativeClose();
64     }
65 }