List of usage examples for io.netty.buffer ByteBufAllocator buffer
ByteBuf buffer(int initialCapacity, int maxCapacity);
From source file:ratpack.sse.internal.ServerSentEventDecoder.java
License:Apache License
public void decode(ByteBuf in, ByteBufAllocator bufferAllocator, Action<Event<?>> eventAction) throws Exception { ByteBuf currentLineBuffer = null;/*from w w w .j av a 2 s . c o m*/ String eventData = null; String eventType = null; String eventId = null; FieldName currentFieldType = null; State state = State.ReadLine; try { while (in.isReadable()) { final int readerIndexAtStart = in.readerIndex(); switch (state) { case ReadLine: final int endOfLineStartIndex = scanAndFindEndOfLine(in); if (-1 == endOfLineStartIndex) { // End of line not found, set readIndex to the end in.readerIndex(in.capacity()); } else { final int bytesAvailableInThisLine = endOfLineStartIndex - readerIndexAtStart; if (bytesAvailableInThisLine == 0) { state = State.DispatchEvent; } else { currentLineBuffer = bufferAllocator.buffer(bytesAvailableInThisLine, bytesAvailableInThisLine); in.readBytes(currentLineBuffer, bytesAvailableInThisLine); state = State.ReadFieldName; } } break; case DispatchEvent: eventAction.execute(new DefaultEvent<>().id(eventId).event(eventType).data(eventData)); eventId = null; eventType = null; eventData = null; state = State.SkipTillEOL; break; case ReadFieldName: int indexOfColon = scanAndFindColon(currentLineBuffer); if (-1 == indexOfColon) { // No colon found, use the whole line as the field name, and the empty string as the field value. indexOfColon = currentLineBuffer.capacity(); } if (1 == indexOfColon) { // Line starts with a colon, ignore state = State.ReadLine; currentLineBuffer.release(); } else { ByteBuf fieldNameBuffer = bufferAllocator.buffer(indexOfColon, indexOfColon); currentLineBuffer.readBytes(fieldNameBuffer, indexOfColon); state = State.SkipColonAndWhiteSpaces; // We have read the field name, next we should skip colon & WS. try { currentFieldType = readCurrentFieldTypeFromBuffer(fieldNameBuffer); } finally { if (null == currentFieldType) { state = State.SkipTillEOL; // Ignore this event completely. } fieldNameBuffer.release(); } } break; case SkipColonAndWhiteSpaces: skipColonAndWhiteSpaces(currentLineBuffer); state = State.ReadFieldValue; break; case SkipTillEOL: skipTilEOL(in); state = State.ReadLine; break; case ReadFieldValue: final int bytesAvailableInThisValue = currentLineBuffer.readableBytes(); ByteBuf currentFieldValue = bufferAllocator.buffer(bytesAvailableInThisValue, bytesAvailableInThisValue); currentLineBuffer.readBytes(currentFieldValue, bytesAvailableInThisValue); state = State.SkipTillEOL; switch (currentFieldType) { case Data: if (null == eventData) { eventData = currentFieldValue.toString(UTF_8); } else { eventData = eventData + LINE_FEED + currentFieldValue.toString(UTF_8); } break; case Id: eventId = currentFieldValue.toString(UTF_8); break; case Event: eventType = currentFieldValue.toString(UTF_8); break; } currentFieldValue.release(); currentLineBuffer.release(); break; } } } finally { in.release(); } }