changes for reading input files
[IRC.git] / Robust / src / ClassLibrary / SocketInputStream.java
index b63bfcd8a378f2a85f4f0cda661f5023dc49ace9..0d12a4cd38f7a6be9cf2956a7c7be9cf432eda55 100644 (file)
@@ -1,14 +1,40 @@
 public class SocketInputStream extends InputStream {
-    Socket s;
-    public SocketInputStream(Socket s) {
-       this.s=s;
-    }
+  Socket s;
+  public SocketInputStream(Socket s) {
+    this.s=s;
+  }
+
+  public int read() {
+    byte[] x=new byte[1];
+    int len=s.read(x);
+    if (len<=0)
+      return -1;
+    else return x[0];
+  }
 
-    public int read() {
-       byte[] x=new byte[1];
-       int len=s.read(x);
-       if (len==0)
-           return -1;
-       else return x[1];
+  public int read(byte[] b) {
+    return s.read(b);
+  }
+
+  public int readAll(byte[] b) {
+    int offset=read(b);
+    if (offset<0)
+      return offset;
+    int toread=b.length-offset;
+    while(toread>0) {
+      byte[] t=new byte[toread];
+      int rd=read(t);
+      if (rd<0)
+        return rd;
+      for(int i=0; i<rd; i++)
+        b[i+offset]=t[i];
+      offset+=rd;
+      toread-=rd;
     }
+    return b.length;
+  }
+
+  public void close() {
+    s.close();
+  }
 }