1 22 package org.jboss.net.sockets; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.net.SocketTimeoutException ; 27 28 34 public class InterruptableInputStream extends InputStream 35 { 36 private InputStream is; 37 38 public InterruptableInputStream(InputStream is) 39 { 40 this.is = is; 41 } 42 43 public int read() throws IOException 44 { 45 byte[] b = {}; 46 int count = internalRead(b, 0, 1); 47 return count > 0 ? b[0] : -1; 48 } 49 50 public int read(byte[] b) throws IOException 51 { 52 return internalRead(b, 0, b.length); 53 } 54 55 public int read(byte[] b, int off, int len) throws IOException 56 { 57 return internalRead(b, off, len); 58 } 59 60 public long skip(long n) throws IOException 61 { 62 return is.skip(n); 63 } 64 65 public int available() throws IOException 66 { 67 return is.available(); 68 } 69 70 public void close() throws IOException 71 { 72 is.close(); 73 } 74 75 public synchronized void mark(int readlimit) 76 { 77 is.mark(readlimit); 78 } 79 80 public synchronized void reset() throws IOException 81 { 82 is.reset(); 83 } 84 85 public boolean markSupported() 86 { 87 return is.markSupported(); 88 } 89 90 private int internalRead(byte[] b, int off, int len) throws IOException 91 { 92 int n = -1; 93 while( true ) 94 { 95 try 96 { 97 n = is.read(b, off, len); 98 return n; 99 } 100 catch(SocketTimeoutException e) 101 { 102 if( Thread.interrupted() ) 104 throw e; 105 } 106 } 107 } 108 } 109 | Popular Tags |