FileDocCategorySizeDatePackage
In.javaAPI DocphoneME MR2 API (J2ME)7141Wed May 02 18:00:00 BST 2007com.sun.midp.ssl

In

public class In extends InputStream
This class is a subclass of InputStream and obtains its input bytes from an SSL connection.

see
com.sun.midp.ssl.SSLStreamConnection
see
com.sun.midp.ssl.Out

Fields Summary
private boolean
isClosed
Indicates the input stream is closed.
private Record
rec
Underlying SSL record layer from which bytes are read.
private int
start
Start of plain text in data buffer.
private int
cnt
Count of unread bytes left in data buffer.
private SSLStreamConnection
ssc
Handle for current SSL stream connection.
private boolean
endOfStream
Signals end of stream.
Constructors Summary
In(Record r, SSLStreamConnection c)
Creates a new In object.

param
r Record layer object from which input bytes are read
param
c SSLStreamConnection object this In object is a part of

        rec = r;
        ssc = c;
    
Methods Summary
public intavailable()
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread.

return
the number of bytes that can be read from this input stream without blocking.
exception
IOException if an I/O error occurs.

        if (isClosed) {
            throw new InterruptedIOException("Stream closed");
        }

        synchronized(rec) {
            if (cnt == 0) {
                // The record buffer is empty, try to refill it without blocking.
                refill(false);
            }
            return cnt;
        }
    
public synchronized voidclose()
Close the stream connection.

exception
IOException is thrown, if an I/O error occurs while shutting down the connection

        if (isClosed) {
            return;
        }

        isClosed = true;
        if (ssc != null) {
            ssc.inputStreamState = SSLStreamConnection.CLOSED;
            rec.closeInputStream();
            ssc.cleanupIfNeeded();
        }
    
public intread()
Reads a byte from this input stream. The method blocks if no input is available.

return
the next byte of data, or -1 if end of stream is reached
exception
IOException if an I/O error occurs

        int val;
        if (isClosed) {
            throw new InterruptedIOException("Stream closed");
        }

        synchronized(rec) {
            if (cnt == 0) {
                refill(true);
                if (cnt == 0) {
                    return -1; // end of stream
                }
            }
    
            val = rec.inputData[start++] & 0xff;
            cnt--;
        }

        return val;
    
public intread(byte[] b)
Reads up to b.length bytes of data from this input stream into the byte array b. Blocks until some input is available. This is equivalent to read(b, 0, b.length).

param
b the buffer into which data is read
return
the actual number of bytes read into the buffer, or -1 if there is no more data and the end of input stream has been reached
exception
IOException if an I/O error occurs

	    return read(b, 0, b.length);
    
public intread(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into b starting at offset off.

param
b buffer into which data is read
param
off starting offset where data is read
param
len maximum number of bytes to be read return the actual number of bytes read into the buffer, or -1 if there is no more data and the end of input stream has been reached
return
number of bytes read
exception
IOException if an I/O error occurs


        int i = 0;
        int numBytes;

        if (isClosed) {
            throw new InterruptedIOException("Stream closed");
        }

        synchronized(rec) {
            if (cnt == 0) {
                // Record buffer empty, block until it is refilled.
                refill(true);
                if (cnt == 0) {
                    return -1; // end of stream
                }
            }

            if (len > cnt) {
                numBytes = cnt;
            } else {
                numBytes = len;
            }

            System.arraycopy(rec.inputData, start, b, off, numBytes);
            start += numBytes;
            cnt -= numBytes;
        }

        return numBytes;
    
private voidrefill(boolean block)
Refills the internal store of decrypted bytes. Called when the byte count in the store reaches zero.

param
block if true the method will not return until data is available, or end of stream
exception
IOException is thrown, if an I/O error occurs filling the the buffer


                                                                      
          
        if (endOfStream) {
            return;
        }

        for (; ;) {
            rec.rdRec(block, Record.APP);
            if (rec.plainTextLength == -1) {
                endOfStream = true;
                return;
            }

            // Do not unblock on a zero byte record unless asked
            if (!block || rec.plainTextLength > 0) {
                break;
            }
        }

        cnt = rec.plainTextLength;
        start = 0;