FileDocCategorySizeDatePackage
SocketInputStream.javaAPI DocJava SE 5 API6391Fri Aug 26 14:57:08 BST 2005java.net

SocketInputStream

public class SocketInputStream extends FileInputStream
This stream extends FileInputStream to implement a SocketInputStream. Note that this class should NOT be public.
version
1.34, 12/19/03
author
Jonathan Payne
author
Arthur van Hoff

Fields Summary
private boolean
eof
private PlainSocketImpl
impl
private byte[]
temp
private Socket
socket
private boolean
closing
Closes the stream.
Constructors Summary
SocketInputStream(PlainSocketImpl impl)
Creates a new SocketInputStream. Can only be called by a Socket. This method needs to hang on to the owner Socket so that the fd will not be closed.

param
impl the implemented socket input stream


                                             
        
	super(impl.getFileDescriptor());
	this.impl = impl;
	socket = impl.getSocket();
    
Methods Summary
public intavailable()
Returns the number of bytes that can be read without blocking.

return
the number of immediately available bytes

	return impl.available();
    
public voidclose()

         
	// Prevent recursion. See BugId 4484411
	if (closing)
	    return;
	closing = true;
	if (socket != null) {
	    if (!socket.isClosed())
		socket.close();
	} else
	    impl.close();
	closing = false;
    
protected voidfinalize()
Overrides finalize, the fd is closed by the Socket.

public final java.nio.channels.FileChannelgetChannel()
Returns the unique {@link java.nio.channels.FileChannel FileChannel} object associated with this file input stream.

The getChannel method of SocketInputStream returns null since it is a socket based stream.

return
the file channel associated with this file input stream
since
1.4
spec
JSR-51

	return null;
    
private static native voidinit()
Perform class load-time initializations.

public intread(byte[] b)
Reads into a byte array data from the socket.

param
b the buffer into which the data is read
return
the actual number of bytes read, -1 is returned when the end of the stream is reached.
exception
IOException If an I/O error has occurred.

	return read(b, 0, b.length);
    
public intread(byte[] b, int off, int length)
Reads into a byte array b at offset off, length bytes of data.

param
b the buffer into which the data is read
param
off the start offset of the data
param
len the maximum number of bytes read
return
the actual number of bytes read, -1 is returned when the end of the stream is reached.
exception
IOException If an I/O error has occurred.

	int n;

	// EOF already encountered
	if (eof) {
	    return -1;
	}

	// connection reset
	if (impl.isConnectionReset()) {
	    throw new SocketException("Connection reset");
	}

	// bounds check
	if (length <= 0 || off < 0 || off + length > b.length) {
	    if (length == 0) {
		return 0;
	    }
	    throw new ArrayIndexOutOfBoundsException();
	}

	boolean gotReset = false;

	// acquire file descriptor and do the read
	FileDescriptor fd = impl.acquireFD();
 	try {	
	    n = socketRead0(fd, b, off, length, impl.getTimeout());
	    if (n > 0) {
		return n;
	    }
	} catch (ConnectionResetException rstExc) {
	    gotReset = true;
	} finally {
	    impl.releaseFD();
	}

	/*
	 * We receive a "connection reset" but there may be bytes still
	 * buffered on the socket
	 */
	if (gotReset) {
	    impl.setConnectionResetPending();
	    impl.acquireFD();
	    try {
	        n = socketRead0(fd, b, off, length, impl.getTimeout());
		if (n > 0) {
		    return n;
		}
	    } catch (ConnectionResetException rstExc) {
	    } finally {
		impl.releaseFD();
	    }
	}

	/*
	 * If we get here we are at EOF, the socket has been closed,
	 * or the connection has been reset.
	 */
        if (impl.isClosedOrPending()) {
            throw new SocketException("Socket closed");
        }
	if (impl.isConnectionResetPending()) {
	    impl.setConnectionReset();
	} 
	if (impl.isConnectionReset()) {
	    throw new SocketException("Connection reset");
	}
	eof = true;
	return -1;
    
public intread()
Reads a single byte from the socket.

	if (eof) {
	    return -1;
	}
	temp = new byte[1];
 	int n = read(temp, 0, 1);
	if (n <= 0) {
	    return -1;
	}
	return temp[0] & 0xff;
    
voidsetEOF(boolean eof)

	this.eof = eof;
    
public longskip(long numbytes)
Skips n bytes of input.

param
n the number of bytes to skip
return
the actual number of bytes skipped.
exception
IOException If an I/O error has occurred.

	if (numbytes <= 0) {
	    return 0;
	}
	long n = numbytes;
	int buflen = (int) Math.min(1024, n);
	byte data[] = new byte[buflen];
	while (n > 0) {
	    int r = read(data, 0, (int) Math.min((long) buflen, n));
	    if (r < 0) {
		break;
	    }
	    n -= r;
	}
	return numbytes - n;
    
private native intsocketRead0(java.io.FileDescriptor fd, byte[] b, int off, int len, int timeout)
Reads into an array of bytes at the specified offset using the received socket primitive.

param
fd the FileDescriptor
param
b the buffer into which the data is read
param
off the start offset of the data
param
len the maximum number of bytes read
param
timeout the read timeout in ms
return
the actual number of bytes read, -1 is returned when the end of the stream is reached.
exception
IOException If an I/O error has occurred.