1 28 package org.jruby.util; 29 30 import java.io.EOFException ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 35 import org.jruby.Ruby; 36 import org.jruby.util.IOHandlerUnseekable; 37 38 public class IOHandlerSocket extends IOHandlerUnseekable { 39 public IOHandlerSocket(Ruby runtime, InputStream inStream, OutputStream outStream) 40 throws IOException { 41 super(runtime, inStream, outStream); 42 } 43 44 public ByteList recv(int len) throws IOException , BadDescriptorException { 45 if(!isOpen()) { 46 throw new IOException ("Socket not open"); 47 } 48 if(len < 1) { 49 return new ByteList(ByteList.NULL_ARRAY); 50 } 51 52 int c = sysread(); 54 if (c == -1) { 55 throw new EOFException (); 56 } 57 int available = getInputStream().available(); 58 len = len - 1 < available ? len - 1 : available; 59 ByteList buf = new ByteList(len + 1); 60 buf.append(c); 61 sysread(buf, 1, len); 62 return buf; 63 } 64 } 65 | Popular Tags |