/** * <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 getReadyForHeaders(final WriteContext wCtx) { headersByteArrayOutputStream.reset(); endOfLineBuffer.recycle(HEADER_DELIMITER, headersByteArrayOutputStream); headers = new HashMap<String, List<String>>(); goToState(State.READ_HEADERS); wCtx.setFinishedIfNoMoreData(); }
void skipPreamble(final WriteContext wCtx) { int byteOfData; while ((byteOfData = wCtx.read()) != -1) { if (endOfLineBuffer.write((byte)byteOfData)) { goToState(State.IDENTIFY_PREAMBLE_DELIMITER); wCtx.setFinishedIfNoMoreData(); return; } } wCtx.setFinishedIfNoMoreData(); }
boolean updateEndOfLineMatchingStatus(final byte b){ if (endOfLineSequence[endOfLineSequenceMatchingLength] == b){ endOfLineSequenceMatchingLength++; }else if (endOfLineSequence[0] == b){ endOfLineSequenceMatchingLength = 1; }else{ endOfLineSequenceMatchingLength = 0; } return isEndOfLine(); }
this.endOfLineBuffer = new EndOfLineBuffer(actualBufferSize, getPreambleDelimiterPrefix(delimiterPrefixes.peek()), null);
void readBody(final WriteContext wCtx) { int byteOfData; while ((byteOfData = wCtx.read()) != -1) { if (endOfLineBuffer.write((byte)byteOfData)) { goToState(State.IDENTIFY_BODY_DELIMITER); wCtx.setFinishedIfNoMoreData(); return; } } wCtx.setFinishedIfNoMoreData(); }
boolean updateEndOfLineMatchingStatus(final byte b){ if (endOfLineSequence[endOfLineSequenceMatchingLength] == b){ endOfLineSequenceMatchingLength++; }else if (endOfLineSequence[0] == b){ endOfLineSequenceMatchingLength = 1; }else{ endOfLineSequenceMatchingLength = 0; } return isEndOfLine(); }
this.endOfLineBuffer = new EndOfLineBuffer(actualBufferSize, getPreambleDelimiterPrefix(delimiterPrefixes.peek()), null);
/** * <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 getReadyForHeaders(final WriteContext wCtx) { headersByteArrayOutputStream.reset(); endOfLineBuffer.recycle(HEADER_DELIMITER, headersByteArrayOutputStream); headers = new HashMap<String, List<String>>(); goToState(State.READ_HEADERS); wCtx.setFinishedIfNoMoreData(); }
void skipPreamble(final WriteContext wCtx) { int byteOfData; while ((byteOfData = wCtx.read()) != -1) { if (endOfLineBuffer.write((byte)byteOfData)) { goToState(State.IDENTIFY_PREAMBLE_DELIMITER); wCtx.setFinishedIfNoMoreData(); return; } } wCtx.setFinishedIfNoMoreData(); }
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 getReadyForBody(final WriteContext wCtx) { partBodyStreamStorage = partBodyStreamStorageFactory.newStreamStorageForPartBody(headers, partIndex); endOfLineBuffer.recycle(delimiterPrefixes.peek(), partBodyStreamStorage); delimiterType.reset(); goToState(State.READ_BODY); wCtx.setFinishedIfNoMoreData(); }
void readBody(final WriteContext wCtx) { int byteOfData; while ((byteOfData = wCtx.read()) != -1) { if (endOfLineBuffer.write((byte)byteOfData)) { goToState(State.IDENTIFY_BODY_DELIMITER); wCtx.setFinishedIfNoMoreData(); return; } } wCtx.setFinishedIfNoMoreData(); }
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 nestedPartRead(final WriteContext wCtx){ delimiterPrefixes.pop(); delimiterType.reset(); endOfLineBuffer.recycle(getPreambleDelimiterPrefix(delimiterPrefixes.peek()), null); goToState(State.SKIP_PREAMBLE); nioMultipartParserListener.onNestedPartFinished(); wCtx.setFinishedIfNoMoreData(); }
void readHeaders(final WriteContext wCtx) { int byteOfData; while ((byteOfData = wCtx.read()) != -1) { if (endOfLineBuffer.write((byte)byteOfData)) { parseHeaders(); String contentType = MultipartUtils.getHeader(MultipartUtils.CONTENT_TYPE, headers); if (MultipartUtils.isMultipart(contentType)) { goToState(State.GET_READY_FOR_NESTED_MULTIPART); } else { goToState(State.GET_READY_FOR_BODY); } wCtx.setFinishedIfNoMoreData(); return; } } wCtx.setFinishedIfNoMoreData(); }
void nestedPartRead(final WriteContext wCtx){ delimiterPrefixes.pop(); delimiterType.reset(); endOfLineBuffer.recycle(getPreambleDelimiterPrefix(delimiterPrefixes.peek()), null); goToState(State.SKIP_PREAMBLE); nioMultipartParserListener.onNestedPartFinished(); wCtx.setFinishedIfNoMoreData(); }
void readHeaders(final WriteContext wCtx) { int byteOfData; while ((byteOfData = wCtx.read()) != -1) { if (endOfLineBuffer.write((byte)byteOfData)) { parseHeaders(); String contentType = MultipartUtils.getHeader(MultipartUtils.CONTENT_TYPE, headers); if (MultipartUtils.isMultipart(contentType)) { goToState(State.GET_READY_FOR_NESTED_MULTIPART); } else { goToState(State.GET_READY_FOR_BODY); } wCtx.setFinishedIfNoMoreData(); return; } } wCtx.setFinishedIfNoMoreData(); }
void getReadyForBody(final WriteContext wCtx) { partBodyStreamStorage = partBodyStreamStorageFactory.newStreamStorageForPartBody(headers, partIndex); endOfLineBuffer.recycle(delimiterPrefixes.peek(), partBodyStreamStorage); delimiterType.reset(); goToState(State.READ_BODY); wCtx.setFinishedIfNoMoreData(); }