/*********************************************************************
* ChannelInputStream.java
* created on 17.03.2005 by netseeker
* $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/io/ChannelInputStream.java,v $
* $Date: 2007/03/22 21:01:31 $
* $Revision: 1.10 $
*
* ====================================================================
*
* Copyright 2005-2006 netseeker aka Michael Manske
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This file is part of the ejoe framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.io;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.NonReadableChannelException;
/**
* This is a very ugly try to work around the following issue when using Channel.newInputStream(): If the client closes
* the connection and we are already reading from the blocking InputStream, that stream will simply throw an IOException
* to notify us of the connection close. Because channel.isOpen() as well as the corresponding socket methods will not
* immediately tell us that the socket is closed we have no chance to decide if the IOException was thrown because the
* socket was closed by client or if there occured an IOException in the reader of the InputStream. To avoid that
* annoying issue we use this FilterInputStream which just wraps all IOExceptions into a NonReadableChannelException to
* tell us that the socket is not readable anymore.
*
* @author netseeker
* @since 0.3.2
*/
public class ChannelInputStream extends FilterInputStream
{
/**
* @param in
*/
public ChannelInputStream(InputStream in)
{
super( in );
}
/*
* (non-Javadoc)
*
* @see java.io.InputStream#read()
*/
public int read() throws IOException
{
try
{
return super.read();
}
catch ( IOException e )
{
throw fillException( e );
}
}
/*
* (non-Javadoc)
*
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read( byte[] b, int off, int len ) throws IOException
{
try
{
return super.read( b, off, len );
}
catch ( IOException e )
{
throw fillException( e );
}
}
/*
* (non-Javadoc)
*
* @see java.io.InputStream#read(byte[])
*/
public int read( byte[] b ) throws IOException
{
try
{
return super.read( b );
}
catch ( IOException e )
{
throw fillException( e );
}
}
/**
* @param e
* @return
*/
private NonReadableChannelException fillException( IOException e )
{
NonReadableChannelException ne = new NonReadableChannelException();
ne.setStackTrace( e.getStackTrace() );
ne.initCause( e.getCause() );
return ne;
}
}