|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.io.InputStream
com.cometway.io.PushBackBufferedInputStream
public class PushBackBufferedInputStream
This extension of InputStream is meant to be a J2ME compatible version of the J2SE PushBackInputStream and BufferedInputStream. If you are running J2SE, you are better off using java.io.BufferedInputStream and java.io.PushBackInputStream since they work natively and this class does not.
This class requires a Thread to do that actual reading of the parent InputStream and buffer it accordingly. It will block if the buffer is full and wait for read() to be called to free up more buffer space. The number of times it will wait is determined by the attemptsBeforeFlushBuffer field, which is by default set to 1. The danger of waiting for read() to be called in order to free up buffer space is incoming data through the parent InputStream may be lost. If this class waits the proper number of times and the buffer is still full, 1/3 of the front of the buffer will be flushed (thus the data will be lost). Uses of this class must understand how the parent InputStream works and how data is to be read from this InputStream. If data is sent quickly to the parent InputStream but data read from this class is slow, a large buffer and many attemptsBeforeFlushBuffer may be required for things to run smoothly. If it is expected that the calling thread of read() of this InputStream occurs more frequently than data is written to the parent InputStream, a small buffer_size and few attemptsBeforeFlushBuffer will suffice. If large number of bytes is expected to be pushed back (unread()), a larger buffer_size may be required. The push back mechanism will flush whatever data is at the end of the buffer when data is pushed back. The maximum memory usage for buffer arithemetic, where N is the buffer size, will not exceed 3N and will always be at least (5/4)N. The closeOnEOF field tells this InputStream to stop reading from the parent if it has reached EOF. Otherwise the Thread will continue to block until PushBackBufferedInputStream.close() is called.
BufferedInputStream,
java.io.PushBackInputStream| Field Summary | |
|---|---|
int |
attemptsBeforeFlushBuffer
THis field tells the buffering mechanism how many times to wait for the buffer to be read from before it flushes the buffer |
boolean |
closeOnEOF
This field tells the buffering mechanism to stop reading from the parent InputStream if an EOF is encountered |
static int |
DEFAULT_BUFFER_SIZE
This is the default buffer size. |
| Constructor Summary | |
|---|---|
PushBackBufferedInputStream(java.io.InputStream input)
Construct a PushBackBufferedInputStream with default parameters and the given parent InputStream. |
|
PushBackBufferedInputStream(java.io.InputStream input,
int buffer_size,
int readTimeOut)
Construct a PushBackBufferedInputStream with the given parent InputStream, with a buffer whose size will be buffer_size and with a read() timeout of readTimeOut. |
|
| Method Summary | |
|---|---|
int |
available()
This method returns the number of available bytes that can be read from the buffer. |
void |
close()
This method stops the reading of the parent InputStream and causes the run() method to return. |
static void |
main(java.lang.String[] args)
|
void |
mark(int i)
This functionality is not supported. |
boolean |
markSupported()
This method will always return true |
int |
read()
Read a single byte from the local buffer. |
int |
read(byte[] readBuffer)
Read an array of bytes from the buffer (note: the number of bytes read will never exceed the size of the buffer). |
int |
read(byte[] readBuffer,
int start,
int length)
Read an array of bytes form the buffer with a starting position and length. |
void |
reset()
THis funcitonality is not supported. |
void |
run()
This method reads bytes from the parent InputStream and buffers it accordingly. |
void |
stop()
This method will cause the run() method to return, thus freeing the thread that this instance of PushBackBufferedInputStream was using. |
void |
unread(byte[] b)
Push an array of bytes back into the buffer. |
void |
unread(byte[] b,
int start,
int length)
Push an array of bytes back into the buffer. |
void |
unread(int b)
Push a byte back into the buffer. |
| Methods inherited from class java.io.InputStream |
|---|
skip |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int DEFAULT_BUFFER_SIZE
public int attemptsBeforeFlushBuffer
public boolean closeOnEOF
| Constructor Detail |
|---|
public PushBackBufferedInputStream(java.io.InputStream input,
int buffer_size,
int readTimeOut)
public PushBackBufferedInputStream(java.io.InputStream input)
| Method Detail |
|---|
public int read()
throws java.io.IOException,
java.io.InterruptedIOException
read in class java.io.InputStreamjava.io.IOException
java.io.InterruptedIOException
public int read(byte[] readBuffer)
throws java.io.IOException
read in class java.io.InputStreamjava.io.IOException
public int read(byte[] readBuffer,
int start,
int length)
throws java.io.IOException
read in class java.io.InputStreamjava.io.IOException
public void unread(int b)
throws java.io.IOException
java.io.IOException
public void unread(byte[] b)
throws java.io.IOException
java.io.IOException
public void unread(byte[] b,
int start,
int length)
throws java.io.IOException
java.io.IOExceptionpublic void run()
run in interface java.lang.Runnablepublic int available()
available in class java.io.InputStream
public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in class java.io.InputStreamjava.io.IOExceptionpublic void mark(int i)
mark in class java.io.InputStreampublic void reset()
reset in class java.io.InputStreampublic boolean markSupported()
markSupported in class java.io.InputStreampublic void stop()
public static void main(java.lang.String[] args)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||