Class DataReader

java.lang.Object
io.helidon.common.buffers.DataReader

public class DataReader extends Object
Data reader that can pull additional data.
  • Constructor Details

    • DataReader

      public DataReader(Supplier<byte[]> bytesSupplier)
      Data reader from a supplier of bytes.
      Parameters:
      bytesSupplier - supplier that can be pulled for more data
    • DataReader

      public DataReader(Supplier<byte[]> bytesSupplier, boolean ignoreLoneEol)
      Data reader from a supplier of bytes.
      Parameters:
      bytesSupplier - supplier that can be pulled for more data
      ignoreLoneEol - ignore LF without CR and CR without LF
  • Method Details

    • available

      public int available()
      Number of bytes available in the currently pulled data.
      Returns:
      number of bytes available
    • pullData

      public void pullData()
      Pull next data.
    • skip

      public void skip(int lenToSkip)
      Skip n bytes.
      Parameters:
      lenToSkip - number of bytes to skip (must be less or equal to current capacity)
    • ensureAvailable

      public void ensureAvailable()
      Ensure we have at least one byte available.
    • read

      public byte read()
      Read 1 byte.
      Returns:
      next byte
    • lookup

      public byte lookup()
      Look at the next byte (does not modify position).
      Returns:
      next byte
    • startsWithNewLine

      public boolean startsWithNewLine()
      Does the data start with a new line (CRLF).
      Returns:
      whether the data starts with a new line (will pull data to have at least two bytes available)
    • startsWith

      public boolean startsWith(byte[] prefix)
      Does the current data start with the prefix.
      Parameters:
      prefix - prefix to find, will pull data to have at least prefix.length bytes available
      Returns:
      whether the data starts with the provided prefix
    • readBuffer

      public BufferData readBuffer()
      Read next buffer. Will read available() number of bytes into a buffer and move position.
      Returns:
      buffer data wrapping the available bytes
    • readBuffer

      public BufferData readBuffer(int length)
      Read next buffer of defined size. Will pull additional data if length is not available. Will move position.
      Parameters:
      length - length of data to read
      Returns:
      buffer data with the length requested
    • getBuffer

      public BufferData getBuffer(int length)
      Get the next buffer of the requested size without moving position.
      Parameters:
      length - bytes to read
      Returns:
      buffer data with the length requested
    • readLazyString

      public LazyString readLazyString(Charset charset, int len)
      Read the next len bytes as a LazyString. This should be used for example for headers, where we want to materialize the string only when needed.
      Parameters:
      charset - character set to use
      len - number of bytes of the string
      Returns:
      lazy string
    • readAsciiString

      public String readAsciiString(int len)
      Read ascii string.
      Parameters:
      len - number of bytes of the string
      Returns:
      string value
    • readBytes

      public byte[] readBytes(int len)
      Read byte array.
      Parameters:
      len - number of bytes of the string
      Returns:
      string value
    • readLine

      public String readLine() throws DataReader.IncorrectNewLineException
      Read an ascii string until new line.
      Returns:
      string with the next line
      Throws:
      DataReader.IncorrectNewLineException - when new line cannot be found
    • findOrNewLine

      public int findOrNewLine(byte b, int max) throws DataReader.IncorrectNewLineException
      Find the byte or next new line.
      Parameters:
      b - - byte to find
      max - - search limit
      Returns:
      i > 0 - index; i == max - not found; i < 0 - new line found at (-i-1) position
      Throws:
      DataReader.IncorrectNewLineException - in case new line was incorrect (such as CR not before LF)
    • debugDataHex

      public String debugDataHex()
      Debug data as a hex string.
      Returns:
      hex string, including headers
    • findNewLine

      public int findNewLine(int max) throws DataReader.IncorrectNewLineException
      Find new line with the next n bytes.
      Parameters:
      max - length to search
      Returns:
      index of the new line, or max if not found
      Throws:
      DataReader.IncorrectNewLineException - in case there is a LF without CR, or CR without a LF
    • listener

      public <T> void listener(DataListener<T> listener, T context)
      Configure data listener.
      Type Parameters:
      T - type of the context
      Parameters:
      listener - listener to write information to
      context - context