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