com.devexperts.qd.qtp
Class AbstractByteArrayParser

java.lang.Object
  extended by com.devexperts.qd.qtp.AbstractByteArrayParser
Direct Known Subclasses:
BlobByteArrayParser, ByteArrayParser, TextByteArrayParser

public abstract class AbstractByteArrayParser
extends java.lang.Object

Base class for classes that parse QTP messages data from byte array.

It consolidates common logic for text and binary byte array parsers. Most of it's logic was simply extracted from ByteArrayParser.

Classes that are interested in extra-high performance can use ByteArrayParser directly, and classes that are interested in opportunity to parse data both in text and binary formats can use this class.

See Also:
ByteArrayParser, TextByteArrayParser, AbstractByteArrayComposer

Field Summary
protected  com.devexperts.io.ByteArrayInput in
           
protected  int limit
           
protected  int processed
           
protected  MessageType readAs
          If not null, then treats all messages as having this type.
protected  RecordBuffer recordBuffer
           
protected  DataScheme scheme
           
protected  SubscriptionBuffer subscriptionBuffer
           
 
Constructor Summary
protected AbstractByteArrayParser(DataScheme scheme)
          Constructs parser with a specified scheme.
 
Method Summary
 void addBytes(byte[] bytes, int offset, int length)
          Adds specified bytes to the end of byte array buffer.
 void ensureCapacity(int requiredCapacity)
          Ensures that byte array buffer has at least specified capacity.
 byte[] getBuffer()
          Returns byte array buffer where data is stored.
 int getLimit()
          Returns number of bytes in the buffer.
 int getProcessed()
          Returns number of processed bytes ready to be removed.
protected  void nextMessage(MessageConsumer consumer, MessageType messageType)
           
abstract  void parse(MessageConsumer consumer)
          Parses accumulated data and retrieve processed messages to specified MessageConsumer.
protected  void processMessage(MessageType type, MessageConsumer consumer)
          Processes message from the recordBuffer or subscriptionBuffer into the consumer.
protected  void processPending(MessageConsumer consumer)
          Processes pending message from buffers (if any) and resets pendingMessageType to null.
 void readAs(MessageType readAs)
           
 void removeBytes(int n)
          Removes specified number of bytes from the start of the buffer.
protected  int replaceReadType(int typeId)
           
 void resetBuffer()
           
 void resetSession()
           
 void setLimit(int newLimit)
          Sets new value for number of bytes in the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scheme

protected final DataScheme scheme

readAs

protected MessageType readAs
If not null, then treats all messages as having this type.


recordBuffer

protected final RecordBuffer recordBuffer

subscriptionBuffer

protected final SubscriptionBuffer subscriptionBuffer

in

protected final com.devexperts.io.ByteArrayInput in

processed

protected int processed

limit

protected int limit
Constructor Detail

AbstractByteArrayParser

protected AbstractByteArrayParser(DataScheme scheme)
Constructs parser with a specified scheme.

Parameters:
scheme - data scheme to use.
Method Detail

resetSession

public void resetSession()

readAs

public void readAs(MessageType readAs)

replaceReadType

protected int replaceReadType(int typeId)

getProcessed

public int getProcessed()
Returns number of processed bytes ready to be removed.

Returns:
number of processed bytes ready to be removed.

getBuffer

public byte[] getBuffer()
Returns byte array buffer where data is stored. Valid index range is from zero inclusive to the limit exclusive.

Note: the array instance used for buffering may change if new messages are added or previous ones are removed.

Returns:
byte array buffer.

removeBytes

public void removeBytes(int n)
Removes specified number of bytes from the start of the buffer.

Parameters:
n - number of bytes to remove.
Throws:
java.lang.IllegalArgumentException - if specified number is negative or is larger than the number of processed bytes.

resetBuffer

public void resetBuffer()

addBytes

public void addBytes(byte[] bytes,
                     int offset,
                     int length)
Adds specified bytes to the end of byte array buffer.

Parameters:
bytes - array to get bytes from.
offset - position of the first byte in bytes
length - number of bytes to add.
Throws:
java.lang.IndexOutOfBoundsException - if copying would cause access of data outside specified byte array bounds.

getLimit

public int getLimit()
Returns number of bytes in the buffer.

Returns:
number of bytes in the buffer.

setLimit

public void setLimit(int newLimit)
Sets new value for number of bytes in the buffer.

Parameters:
newLimit - new value for number of bytes in the buffer.
Throws:
java.lang.IllegalArgumentException - if specified limit is less than the number of processed bytes or is larger than the buffer capacity.

ensureCapacity

public void ensureCapacity(int requiredCapacity)
Ensures that byte array buffer has at least specified capacity.

Note: the array instance used for buffering may change if new messages are added or previous ones are removed. This means that specified capacity is valid only until next operation.

Parameters:
requiredCapacity - capacity to ensure.

parse

public abstract void parse(MessageConsumer consumer)
Parses accumulated data and retrieve processed messages to specified MessageConsumer. This is the main method in this class. It doesn't remove parsed bytes from buffer, it only updates processed value.

Parameters:
consumer - MessageConsumer to pass parsed messages.

processPending

protected void processPending(MessageConsumer consumer)
Processes pending message from buffers (if any) and resets pendingMessageType to null.


nextMessage

protected void nextMessage(MessageConsumer consumer,
                           MessageType messageType)

processMessage

protected void processMessage(MessageType type,
                              MessageConsumer consumer)
Processes message from the recordBuffer or subscriptionBuffer into the consumer.