List of usage examples for io.netty.buffer ByteBuf getByte
public abstract byte getByte(int index);
From source file:com.heliosapm.streams.collector.groovy.ByteBufReaderSource.java
License:Apache License
/** * Returns the index in the buffer of the end of line found. * Returns -1 if no end of line was found in the buffer. *//*w ww. ja va 2 s. c o m*/ private static int findEndOfLine(final ByteBuf buffer) { int i = buffer.forEachByte(ByteProcessor.FIND_LF); if (i > 0 && buffer.getByte(i - 1) == '\r') { i--; } return i; }
From source file:com.heliosapm.streams.metrics.aggregation.StreamedMetricAggregation.java
License:Apache License
/** * Creates a new StreamedMetricAggregation from the passed bytes * @param bytes The bytes to read from//www . j a v a2 s. c o m */ private StreamedMetricAggregation(final byte[] bytes) { size = bytes.length; final ByteBuf b = BufferManager.getInstance().buffer(size).writeBytes(bytes); try { sticky = b.getByte(STICKY) == 1; doubleType = b.getByte(DOUBLE_TYPE) == 1; createTime = b.getLong(CREATE_TIME); period = b.getLong(PERIOD); periodUnit = TUNITS[b.getByte(PERIOD_UNIT)]; b.readerIndex(METRIC_VALUES); values.put(bytes, METRIC_VALUES, VALUE_SIZE); final byte tagCount = b.getByte(TAG_COUNT); b.readerIndex(METRIC_NAME); metricName = nextString(b); for (int i = 0; i < tagCount; i++) { tags.put(nextString(b), nextString(b)); i++; } } finally { try { b.release(); } catch (Exception x) { /* No Op */} } }
From source file:com.heliosapm.tsdblite.handlers.http.SubmitTracesHandler.java
License:Apache License
/** * {@inheritDoc}//from w w w . ja va 2s .com * @see com.heliosapm.tsdblite.handlers.http.HttpRequestHandler#process(com.heliosapm.tsdblite.handlers.http.TSDBHttpRequest) */ @Override protected void process(final TSDBHttpRequest request) { log.debug("Processing [{}]", request.getRequest()); if (!request.hasContent()) { request.send400("No content sent for route [", request.getRoute(), "]"); return; } final ByteBuf content = request.getContent(); final Trace[] traces; try { if (content.getByte(0) == '{') { traces = new Trace[] { JSON.parseToObject(content, Trace.class) }; } else { traces = JSON.parseToObject(content, Trace[].class); } } catch (JSONException jex) { log.error("Failed to parse JSON payload", jex); request.send400("Invalid JSON payload for route [", request.getRoute(), "]:", jex.toString()); return; } final ElapsedTime et = SystemClock.startClock(); for (Trace trace : traces) { //log.debug("TRACE: {}", trace); metricCache.submit(trace); } request.send204().addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(final Future<? super Void> f) throws Exception { if (f.isSuccess()) { log.info("Traces Processed: {}", et.printAvg("traces", traces.length)); } else { log.error("Traces failed", f.cause()); } }; }); }
From source file:com.ibasco.agql.core.utils.ByteBufUtils.java
License:Open Source License
public static String readString(ByteBuf buffer, Charset encoding, boolean readNonNullTerminated, String defaultString) {/*from ww w . j a v a 2 s . co m*/ int length = buffer.bytesBefore((byte) 0); if (length < 0) { if (readNonNullTerminated && buffer.readableBytes() > 0) length = buffer.readableBytes(); else return null; } String data = buffer.readCharSequence(length, encoding).toString(); //Discard the null terminator (if available) if (buffer.readableBytes() > 2 && buffer.getByte(buffer.readerIndex()) == 0) buffer.readByte(); return data; }
From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceRconPacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { final String separator = "================================================================================================="; //TODO: Move all code logic below to SourceRconPacketBuilder log.debug(separator);/* ww w . java 2s. c o m*/ log.debug(" ({}) DECODING INCOMING DATA : Bytes Received = {} {}", index.incrementAndGet(), in.readableBytes(), index.get() > 1 ? "[Continuation]" : ""); log.debug(separator); String desc = StringUtils.rightPad("Minimum allowable size?", PAD_SIZE); //Verify we have the minimum allowable size if (in.readableBytes() < 14) { log.debug(" [ ] {} = NO (Actual Readable Bytes: {})", desc, in.readableBytes()); return; } log.debug(" [x] {} = YES (Actual Readable Bytes: {})", desc, in.readableBytes()); //Reset if this happens to be not a valid source rcon packet in.markReaderIndex(); //Read and Verify size desc = StringUtils.rightPad("Bytes received at least => than the \"declared\" size?", PAD_SIZE); int size = in.readIntLE(); int readableBytes = in.readableBytes(); if (readableBytes < size) { log.debug(" [ ] {} = NO (Declared Size: {}, Actual Bytes Read: {})", desc, readableBytes, size); in.resetReaderIndex(); return; } log.debug(" [x] {} = YES (Declared Size: {}, Actual Bytes Read: {})", desc, readableBytes, size); //Read and verify request id desc = StringUtils.rightPad("Request Id within the valid range?", PAD_SIZE); int id = in.readIntLE(); if (!(id == -1 || id == SourceRconUtil.RCON_TERMINATOR_RID || SourceRconUtil.isValidRequestId(id))) { log.debug(" [ ] {} = NO (Actual: {})", desc, id); in.resetReaderIndex(); return; } log.debug(" [x] {} = YES (Actual: {})", desc, id); //Read and verify request type desc = StringUtils.rightPad("Valid response type?", PAD_SIZE); int type = in.readIntLE(); if (get(type) == null) { log.debug(" [ ] {} = NO (Actual: {})", desc, type); in.resetReaderIndex(); return; } log.debug(" [x] {} = YES (Actual: {} = {})", desc, type, SourceRconResponseType.get(type)); //Read and verify body desc = StringUtils.rightPad("Contains Body?", PAD_SIZE); int bodyLength = in.bytesBefore((byte) 0); String body = StringUtils.EMPTY; if (bodyLength <= 0) log.debug(" [ ] {} = NO", desc); else { body = in.readCharSequence(bodyLength, StandardCharsets.UTF_8).toString(); log.debug(" [x] {} = YES (Length: {}, Body: {})", desc, bodyLength, StringUtils.replaceAll(StringUtils.truncate(body, 30), "\n", "\\\\n")); } //Peek at the last two bytes and verify that they are null-bytes byte bodyTerminator = in.getByte(in.readerIndex()); byte packetTerminator = in.getByte(in.readerIndex() + 1); desc = StringUtils.rightPad("Contains TWO null-terminating bytes at the end?", PAD_SIZE); //Make sure the last two bytes are NULL bytes (request id: 999 is reserved for split packet responses) if ((bodyTerminator != 0 || packetTerminator != 0) && (id == SourceRconUtil.RCON_TERMINATOR_RID)) { log.debug("Skipping {} bytes", in.readableBytes()); in.skipBytes(in.readableBytes()); return; } else if (bodyTerminator != 0 || packetTerminator != 0) { log.debug(" [ ] {} = NO (Actual: Body Terminator = {}, Packet Terminator = {})", desc, bodyTerminator, packetTerminator); in.resetReaderIndex(); return; } else { log.debug(" [x] {} = YES (Actual: Body Terminator = {}, Packet Terminator = {})", desc, bodyTerminator, packetTerminator); //All is good, skip the last two bytes if (in.readableBytes() >= 2) in.skipBytes(2); } //At this point, we can now construct a packet log.debug(" [x] Status: PASS (Size = {}, Id = {}, Type = {}, Remaining Bytes = {}, Body Size = {})", size, id, type, in.readableBytes(), bodyLength); log.debug(separator); //Reset the index index.set(0); //Construct the response packet and send to the next handlers SourceRconResponsePacket responsePacket; //Did we receive a terminator packet? if (this.terminatingPacketsEnabled && id == SourceRconUtil.RCON_TERMINATOR_RID && StringUtils.isBlank(body)) { responsePacket = new SourceRconTermResponsePacket(); } else { responsePacket = SourceRconPacketBuilder.getResponsePacket(type); } if (responsePacket != null) { responsePacket.setSize(size); responsePacket.setId(id); responsePacket.setType(type); responsePacket.setBody(body); log.debug( "Decode Complete. Passing response for request id : '{}' to the next handler. Remaining bytes ({})", id, in.readableBytes()); out.add(responsePacket); } }
From source file:com.ibm.crail.datanode.netty.CrailNettyUtils.java
License:Apache License
public static void showByteBufContent(ByteBuf buf, int offset, int bytes) { /* this dump the content of first bytes from the payload */ if (buf != null) { int ori_rindex = buf.readerIndex(); LOG.info("DUMP: TID:" + Thread.currentThread().getId() + " NettyByteBuf : " + buf); int min = (buf.capacity() - offset); if (min > bytes) min = bytes;/*from w w w. j a va 2 s . c o m*/ String str = "DUMP: TID:" + Thread.currentThread().getId() + " DUMP (" + offset + " ,+" + min + ") : "; //for loop update it to the end limit min += offset; for (int i = offset; i < min; i++) { //str += Character.toHexString(); str += Byte.toString(buf.getByte(i)) + " : "; if (i % 32 == 0) str += "\n"; } LOG.info(str); buf.readerIndex(ori_rindex); } else { LOG.info("DUMP : payload content is NULL"); } }
From source file:com.jamierf.jsonrpc.util.JsonObjectDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (state == ST_CORRUPTED) { in.skipBytes(in.readableBytes()); return;/*from ww w. j a v a 2s . c o m*/ } // index of next byte to process. int idx = this.idx; int wrtIdx = in.writerIndex(); if (wrtIdx > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. in.skipBytes(in.readableBytes()); reset(); throw new TooLongFrameException( "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded"); } for (/* use current idx */; idx < wrtIdx; idx++) { byte c = in.getByte(idx); if (state == ST_DECODING_NORMAL) { decodeByte(c, in, idx); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (openBraces == 0) { ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex()); if (json != null) { out.add(json); } // The JSON object/array was extracted => discard the bytes from // the input buffer. in.readerIndex(idx + 1); // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); } } else if (state == ST_DECODING_ARRAY_STREAM) { decodeByte(c, in, idx); if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) { // skip leading spaces. No range check is needed and the loop will terminate // because the byte at position idx is not a whitespace. for (int i = in.readerIndex(); Character.isWhitespace(in.getByte(i)); i++) { in.skipBytes(1); } // skip trailing spaces. int idxNoSpaces = idx - 1; while (idxNoSpaces >= in.readerIndex() && Character.isWhitespace(in.getByte(idxNoSpaces))) { idxNoSpaces--; } ByteBuf json = extractObject(ctx, in, in.readerIndex(), idxNoSpaces + 1 - in.readerIndex()); if (json != null) { out.add(json); } in.readerIndex(idx + 1); if (c == ']') { reset(); } } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c); if (state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket in.skipBytes(1); } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { in.skipBytes(1); } else { state = ST_CORRUPTED; throw new CorruptedFrameException( "invalid JSON received at byte position " + idx + ": " + ByteBufUtil.hexDump(in)); } } if (in.readableBytes() == 0) { this.idx = 0; } else { this.idx = idx; } }
From source file:com.jamierf.jsonrpc.util.JsonObjectDecoder.java
License:Apache License
private void decodeByte(byte c, ByteBuf in, int idx) { if ((c == '{' || c == '[') && !insideString) { openBraces++;//from w w w . jav a 2 s . co m } else if ((c == '}' || c == ']') && !insideString) { openBraces--; } else if (c == '"') { // start of a new JSON string. It's necessary to detect strings as they may // also contain braces/brackets and that could lead to incorrect results. if (!insideString) { insideString = true; // If the double quote wasn't escaped then this is the end of a string. } else if (in.getByte(idx - 1) != '\\') { insideString = false; } } }
From source file:com.minetats.mw.NamedPipe.java
License:Apache License
@Override public ByteBuf readChunk(ByteBufAllocator bba) throws Exception { chunks++;/* w w w.ja va 2 s . com*/ ByteBuf buf = bba.heapBuffer(chunkSize); boolean release = false; int read = 0; try { do { buf.writerIndex(buf.writerIndex() + read); read = file.read(buf.array(), buf.arrayOffset() + read, chunkSize - read); } while (read > 0); int index = buf.writerIndex() - 1; if (buf.getByte(index) == '\n' && buf.getByte(index - 1) == '\n') { endOfInput = true; System.out.println("endOfInput=" + endOfInput + ", read " + chunks + " chunks"); } return buf; } finally { if (release) { buf.release(); } } }
From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java
License:Apache License
private int lineLength(ByteBuf in) { int readableBytes = in.readableBytes(); if (readableBytes < 2) { return -1; }/*from w w w .j ava 2 s.co m*/ /* CR */ int length = in.bytesBefore(CR_BYTE); if (length < 0) { return -1; } if (readableBytes < length + 2) { return -1; } /* LF */ byte eolLF = in.getByte(in.readerIndex() + length + 1); if (eolLF != LF_BYTE) { throw new RuntimeException("Redis protocol exception; malformed end of line; byte=" + eolLF); } return length + 2; }