start of new file
[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(String host, int port) {
34         InetAddress address=InetAddress.getByName(host);
35         fd=nativeBind(address.getAddress(), port);
36         nativeConnect(fd, address.getAddress(), port);
37     }
38
39     public void connect(InetAddress address, int port) {
40         fd=nativeBind(address.getAddress(), port);
41         nativeConnect(fd, address.getAddress(), port);
42     }
43
44     public static native int nativeBind(byte[] address, int port);
45
46     public native int nativeConnect(int fd, byte[] address, int port);
47     
48     int setFD(int filed) {
49         fd=filed;
50     }
51
52     public int read(byte[] b) {
53         return nativeRead(b);
54     }
55     public void write(byte[] b) {
56         nativeWrite(b, 0, b.length);
57     }
58
59     public void write(byte[] b, int offset, int len) {
60         nativeWrite(b, offset, len);
61     }
62
63     private native void nativeBindFD(int fd);
64     private native int nativeRead(byte[] b);
65     private native void nativeWrite(byte[] b, int offset, int len);
66     private native void nativeClose();
67
68     public void close() {
69         nativeClose();
70     }
71 }