KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > sockets > InterruptableInputStream


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.net.sockets;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.SocketTimeoutException JavaDoc;
27
28 /** An InputStream that uses the SocketTimeoutException thrown during read
29  * timeouts to check if the thread has been interrupted.
30  *
31  * @author Scott.Stark@jboss.org
32  * @version $Revision: 1958 $
33  */

34 public class InterruptableInputStream extends InputStream JavaDoc
35 {
36    private InputStream JavaDoc is;
37
38    public InterruptableInputStream(InputStream JavaDoc is)
39    {
40       this.is = is;
41    }
42
43    public int read() throws IOException JavaDoc
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 JavaDoc
51    {
52       return internalRead(b, 0, b.length);
53    }
54
55    public int read(byte[] b, int off, int len) throws IOException JavaDoc
56    {
57       return internalRead(b, off, len);
58    }
59
60    public long skip(long n) throws IOException JavaDoc
61    {
62       return is.skip(n);
63    }
64
65    public int available() throws IOException JavaDoc
66    {
67       return is.available();
68    }
69
70    public void close() throws IOException JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc e)
101          {
102             // Test for thread interrupt
103
if( Thread.interrupted() )
104                throw e;
105          }
106       }
107    }
108 }
109
Popular Tags