boolean updateEndOfLineMatchingStatus(final byte b){ if (endOfLineSequence[endOfLineSequenceMatchingLength] == b){ endOfLineSequenceMatchingLength++; }else if (endOfLineSequence[0] == b){ endOfLineSequenceMatchingLength = 1; }else{ endOfLineSequenceMatchingLength = 0; } return isEndOfLine(); }
boolean updateEndOfLineMatchingStatus(final byte b){ if (endOfLineSequence[endOfLineSequenceMatchingLength] == b){ endOfLineSequenceMatchingLength++; }else if (endOfLineSequence[0] == b){ endOfLineSequenceMatchingLength = 1; }else{ endOfLineSequenceMatchingLength = 0; } return isEndOfLine(); }
/** * <p> Writes a byte of data in the buffer. If the buffer already encountered an end of line sequence, and exception will be thrown. * * @param data The byte of data. * @return true if the buffer encountered one of the end of line sequences, false otherwise. */ public boolean write(final byte data){ if (isEndOfLine()){ throw new IllegalStateException("Buffer is in an end of line state. You need to recycle it before writing."); } flushIfNeeded(); circularBuffer.write(data); boolean isEndOfLine = updateEndOfLineMatchingStatus(data); if (isEndOfLine){ flushIfNeeded(); } return isEndOfLine; }
/** * <p> Writes a byte of data in the buffer. If the buffer already encountered an end of line sequence, and exception will be thrown. * * @param data The byte of data. * @return true if the buffer encountered one of the end of line sequences, false otherwise. */ public boolean write(final byte data){ if (isEndOfLine()){ throw new IllegalStateException("Buffer is in an end of line state. You need to recycle it before writing."); } flushIfNeeded(); circularBuffer.write(data); boolean isEndOfLine = updateEndOfLineMatchingStatus(data); if (isEndOfLine){ flushIfNeeded(); } return isEndOfLine; }
void flushIfNeeded(){ if (flushOutputStream == null){ return; } if (circularBuffer.isFull() || isEndOfLine()) { try { if (circularBuffer.getAvailableDataLength() > 0) { if (endOfLineSequenceMatchingLength > 0) { // Need to flush a chunk int chunkSize = circularBuffer.availableReadLength - endOfLineSequenceMatchingLength; circularBuffer.readChunk(flushOutputStream, chunkSize); } else { // flush all circularBuffer.readAll(flushOutputStream); } } } catch (Exception e) { throw new IllegalStateException("Error flushing the buffer data.", e); } } if (circularBuffer.isFull()){ // Still full after flushing should never happen... throw new IllegalStateException("Unexpected error. Buffer is full after a flush."); } } }
void flushIfNeeded(){ if (flushOutputStream == null){ return; } if (circularBuffer.isFull() || isEndOfLine()) { try { if (circularBuffer.getAvailableDataLength() > 0) { if (endOfLineSequenceMatchingLength > 0) { // Need to flush a chunk int chunkSize = circularBuffer.availableReadLength - endOfLineSequenceMatchingLength; circularBuffer.readChunk(flushOutputStream, chunkSize); } else { // flush all circularBuffer.readAll(flushOutputStream); } } } catch (Exception e) { throw new IllegalStateException("Error flushing the buffer data.", e); } } if (circularBuffer.isFull()){ // Still full after flushing should never happen... throw new IllegalStateException("Unexpected error. Buffer is full after a flush."); } } }