Package smsqmulator

Class IPReadAheadBuffer

java.lang.Object
smsqmulator.IPReadAheadBuffer

public class IPReadAheadBuffer extends Object
A (very primitive) "peekable" byte (not char!) buffer for an InputStream (supposedly from a socket). Contrary to BufferedReader, this operates on bytes, not on characters. It allows "peeks", i.e. reading bytes into an array without removing them from the buffer. This is so that the "C" recv function can actually peek at the content of the InputStream. This is not a real buffer in the sense that it would read more bytes than requested in one go to make reading more efficient. Implementation note : the buffer is discarded by setting the empty flag to true. Note for future versions : could reuse existing "empty" buffer if possible, use pointers to mark start/stop
  • Field Details

  • Constructor Details

    • IPReadAheadBuffer

      public IPReadAheadBuffer(InputStream raw_In)
      Create a buffer for an InputStream.
      Parameters:
      raw_In - the InputStream
  • Method Details

    • read

      public int read(byte[] result, boolean keep) throws Exception
      Read bytes from the stream and/or buffer.
      Parameters:
      result - destination array : where to put the bytes.
      keep - true if the buffer should be created/kept, else it will be discarded at the end. If the buffer is kept, the bytes are copied to the destination but still remain in the buffer : PEEK If the buffer is not "empty", the size of the buffer is always adjusted such that the buffer is always of the size of valid bytes got from the stream.
      Returns:
      number of bytes read. (If an exception occurs, return the exception to the caller).
      Throws:
      Exception - any exception from operations on the underlying stream.