View Javadoc

1   /**********************************************************************
2    * ChannelInputStream.java
3    * created on 17.03.2005 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/io/ChannelInputStream.java,v $
5    * $Date: 2006/02/04 14:15:53 $
6    * $Revision: 1.3 $
7    *********************************************************************/
8   package de.netseeker.ejoe.io;
9   
10  import java.io.FilterInputStream;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.nio.channels.NonReadableChannelException;
14  
15  /***
16   * This is a very ugly try to work around the following issue when using
17   * Channel.newInputStream(): If the client closes the connection and we are
18   * already reading from the blocking InputStream, that stream will simply throw
19   * an IOException to notify us of the connection close. Because channel.isOpen()
20   * as well as the corresponding socket methods will not immediately tell us that
21   * the socket is closed we have no chance to decide if the IOException was
22   * thrown because the socket was closed by client or if there occured an
23   * IOException in the reader of the InputStream. To avoid that annoying issue we
24   * use this FilterInputStream which just wraps all IOException into a
25   * NonReadableChannelException to tell us that the socket is not readable
26   * anymore.
27   * 
28   * @author netseeker
29   */
30  public class ChannelInputStream extends FilterInputStream
31  {
32  	/***
33  	 * @param in
34  	 */
35  	public ChannelInputStream(InputStream in)
36  	{
37  		super(in);
38  	}
39  
40  	/*
41  	 * (non-Javadoc)
42  	 * 
43  	 * @see java.io.InputStream#read()
44  	 */
45  	public int read() throws IOException
46  	{
47  		try
48  		{
49  			return super.read();
50  		}
51  		catch (IOException e)
52  		{
53  			throw new NonReadableChannelException();
54  		}
55  	}
56  
57  	/*
58  	 * (non-Javadoc)
59  	 * 
60  	 * @see java.io.InputStream#read(byte[], int, int)
61  	 */
62  	public int read(byte[] b, int off, int len) throws IOException
63  	{
64  		try
65  		{
66  
67  			return super.read(b, off, len);
68  		}
69  		catch (IOException e)
70  		{
71  			throw new NonReadableChannelException();
72  		}
73  	}
74  
75  	/*
76  	 * (non-Javadoc)
77  	 * 
78  	 * @see java.io.InputStream#read(byte[])
79  	 */
80  	public int read(byte[] b) throws IOException
81  	{
82  		try
83  		{
84  			return super.read(b);
85  		}
86  		catch (IOException e)
87  		{
88  			throw new NonReadableChannelException();
89  		}
90  	}
91  }