Java Code Examples for io.netty.buffer.ByteBuf#getByte()
The following examples show how to use
io.netty.buffer.ByteBuf#getByte() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonObjectDecoder.java From reactive-ipc-jvm with Apache License 2.0 | 6 votes |
private void decodeByte(byte c, ByteBuf in, int idx) { if ((c == '{' || c == '[') && !insideString) { openBraces++; } 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; } } }
Example 2
Source File: DrillStringUtils.java From Bats with Apache License 2.0 | 6 votes |
/** * parsing a hex encoded binary string and write to an output buffer. * * This function does not modify the {@code readerIndex} and {@code writerIndex} * of the byte buffer. * * @return Index in the byte buffer just after the last written byte. */ public static int parseBinaryString(ByteBuf str, int strStart, int strEnd, ByteBuf out) { int dstEnd = 0; for (int i = strStart; i < strEnd; i++) { byte b = str.getByte(i); if (b == '\\' && strEnd > i+3 && (str.getByte(i+1) == 'x' || str.getByte(i+1) == 'X')) { // ok, take next 2 hex digits. byte hd1 = str.getByte(i+2); byte hd2 = str.getByte(i+3); if (isHexDigit(hd1) && isHexDigit(hd2)) { // [a-fA-F0-9] // turn hex ASCII digit -> number b = (byte) ((toBinaryFromHex(hd1) << 4) + toBinaryFromHex(hd2)); i += 3; // skip 3 } } out.setByte(dstEnd++, b); } return dstEnd; }
Example 3
Source File: ServerRequest.java From java-dcp-client with Apache License 2.0 | 6 votes |
public static void handleServerRequest(ChannelHandlerContext ctx, ByteBuf msg, BucketConfigSink bucketConfigSink) { if (!isServerRequest(msg)) { throw new IllegalArgumentException("expected a server request but got message with magic " + msg.getByte(0)); } switch (msg.getByte(1)) { case MessageUtil.CLUSTERMAP_CHANGE_NOTIFICATION_OPCODE: handleConfigChangeNotification(ctx, msg, bucketConfigSink); break; case MessageUtil.AUTHENTICATE_OPCODE: case MessageUtil.ACTIVE_EXTERNAL_USERS_OPCODE: // Couchbase Server doesn't expect a response to these log.warn("Ignoring unexpected server request: {}", MessageUtil.getShortOpcodeName(msg)); break; default: log.warn("Ignoring unrecognized server request: {}", MessageUtil.getShortOpcodeName(msg)); break; } }
Example 4
Source File: StringFunctionUtil.java From dremio-oss with Apache License 2.0 | 6 votes |
public static int parseBinaryStringNoFormat(ByteBuf str, int strStart, int strEnd, ByteBuf out, FunctionErrorContext errCtx) { int dstEnd = 0; if(((strStart - strEnd) % 2) != 0){ throw errCtx.error() .message("Failure parsing hex string, length was not a multiple of two.") .build(); } for (int i = strStart; i < strEnd; i+=2) { byte b1 = str.getByte(i); byte b2 = str.getByte(i+1); if(isHexDigit(b1) && isHexDigit(b2)){ byte finalByte = (byte) ((toBinaryFromHex(b1) << 4) + toBinaryFromHex(b2)); out.setByte(dstEnd++, finalByte); }else{ throw errCtx.error() .message("Failure parsing hex string, one or more bytes was not a valid hex value.") .build(); } } return dstEnd; }
Example 5
Source File: IrisUpnpServer.java From arcusplatform with Apache License 2.0 | 5 votes |
@Override public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception { DatagramPacket packet = (DatagramPacket)msg; if (packet == null) { return; } try { ByteBuf content = packet.content(); if (log.isTraceEnabled()) { log.trace("recv upnp message: {}", content.toString(StandardCharsets.UTF_8)); } if (content.readableBytes() > 5 && content.getByte(content.readerIndex()) == 'H' && content.getByte(content.readerIndex()+1) == 'T' && content.getByte(content.readerIndex()+2) == 'T' && content.getByte(content.readerIndex()+3) == 'P' && content.getByte(content.readerIndex()+4) == '/') { handleResponse(ctx, packet); } else { handleRequest(ctx, packet); } } catch (Throwable th) { log.debug("error processing upnp packet:", th); } finally { if (packet.refCnt() > 0) { packet.release(packet.refCnt()); } } }
Example 6
Source File: DoubleSchema.java From pulsar with Apache License 2.0 | 5 votes |
@Override public Double decode(ByteBuf byteBuf) { if (null == byteBuf) { return null; } validate(byteBuf); long value = 0; for (int i = 0; i < 8; i ++) { value <<= 8; value |= byteBuf.getByte(i) & 0xFF; } return Double.longBitsToDouble(value); }
Example 7
Source File: BloomFilter.java From Distributed-KV with Apache License 2.0 | 5 votes |
@Override public boolean keyMayMatch(ByteBuf key, ByteBuf filter) { // filter信息的末尾是bitset大小信息 int retSize = filter.readableBytes() - Integer.BYTES; int bitsetSize = filter.getInt(filter.readerIndex() + retSize); // 实际有效filter信息 ByteBuf realFilter = filter.slice(filter.readerIndex(), retSize); // TODO 可优化. 建立byte数组,盛放key和附加k byte[] data = new byte[key.readableBytes() + Integer.BYTES]; // 映射k次 for (int x = 0; x < k; x++) { // 将key中的字节读入数组 key.slice().readBytes(data, 0, key.readableBytes()); // 将k组装到key的末尾 data[key.readableBytes()] = ((k >> 24) & 0xff); data[key.readableBytes() + 1] = ((k >> 16) & 0xff); data[key.readableBytes() + 2] = ((k >> 8) & 0xff); data[key.readableBytes() + 3] = (k & 0xff); // 生成hash数据 long hash = createHash(data); hash = hash % (long) bitsetSize; // 根据hash数据和filter信息,替代bitset来检验key的存在 int index = Math.abs((int) hash); int i = index / 8; int j = 7 - index % 8; boolean ret = (realFilter.getByte(i) & (1 << j)) >> j == 1 ? true : false; if (!ret) return false; } return true; }
Example 8
Source File: MSSQLDataTypeCodec.java From vertx-sql-client with Apache License 2.0 | 5 votes |
private static BigInteger readUnsignedInt96LE(ByteBuf buffer) { byte[] result = new byte[12]; int readerIndex = buffer.readerIndex(); for (int i = 0; i < 12; i++) { result[i] = buffer.getByte(readerIndex + 11 - i); } buffer.skipBytes(12); return new BigInteger(result); }
Example 9
Source File: CrlfTerminatingChunkedStream.java From NioSmtpClient with Apache License 2.0 | 5 votes |
private boolean isTerminatedWithCrLf(ByteBuf chunk) { int length = chunk.readableBytes(); return length >= 2 && chunk.getByte(length - 2) == CR && chunk.getByte(length - 1) == LF; }
Example 10
Source File: PduCodec.java From herddb with Apache License 2.0 | 5 votes |
public static boolean readInludeTransactionLog(Pdu pdu) { ByteBuf buffer = pdu.buffer; return buffer.getByte(VERSION_SIZE + FLAGS_SIZE + TYPE_SIZE + MSGID_SIZE) == 1; }
Example 11
Source File: HAProxyMessageDecoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private static boolean match(byte[] prefix, ByteBuf buffer, int idx) { for (int i = 0; i < prefix.length; i++) { final byte b = buffer.getByte(idx + i); if (b != prefix[i]) { return false; } } return true; }
Example 12
Source File: HttpFrameDecoder.java From netty-http2 with Apache License 2.0 | 5 votes |
/** * Reads the HTTP/2 Frame Header and sets the length, type, flags, and streamId member variables. * * @param buffer input buffer containing the entire 9-octet header */ private void readFrameHeader(ByteBuf buffer) { int frameOffset = buffer.readerIndex(); length = getUnsignedMedium(buffer, frameOffset); type = buffer.getUnsignedByte(frameOffset + 3); flags = buffer.getByte(frameOffset + 4); streamId = getUnsignedInt(buffer, frameOffset + 5); buffer.skipBytes(HTTP_FRAME_HEADER_SIZE); }
Example 13
Source File: TagsMetadata.java From spring-cloud-rsocket with Apache License 2.0 | 5 votes |
protected static String decodeString(ByteBuf byteBuf, AtomicInteger offset) { int length = byteBuf.getByte(offset.get()); int index = offset.addAndGet(Byte.BYTES); String s = byteBuf.toString(index, length, StandardCharsets.UTF_8); offset.addAndGet(length); return s; }
Example 14
Source File: MysqlServerState.java From antsdb with GNU Lesser General Public License v3.0 | 5 votes |
public PacketType getPacketType(ByteBuf packet) { if (packet == authPacket) { return PacketType.FISH_AUTH; } if (isLargePacket) { int sequence = packet.getByte(3) & 0xff; if (sequence > 0) { return PacketType.VERY_LARGE_PACKET; } } PacketType type = PacketType.valueOf(packet.getByte(4) & 0xff); return type; }
Example 15
Source File: HttpPostMultipartRequestDecoder.java From dorado with Apache License 2.0 | 5 votes |
/** * Read one line up to the CRLF or LF * * @return the String from one line * @throws NotEnoughDataDecoderException Need more chunks and reset the * {@code readerIndex} to the previous * value */ private static String readLineStandard(ByteBuf undecodedChunk, Charset charset) { int readerIndex = undecodedChunk.readerIndex(); try { ByteBuf line = buffer(64); while (undecodedChunk.isReadable()) { byte nextByte = undecodedChunk.readByte(); if (nextByte == HttpConstants.CR) { // check but do not changed readerIndex nextByte = undecodedChunk.getByte(undecodedChunk.readerIndex()); if (nextByte == HttpConstants.LF) { // force read undecodedChunk.readByte(); return line.toString(charset); } else { // Write CR (not followed by LF) line.writeByte(HttpConstants.CR); } } else if (nextByte == HttpConstants.LF) { return line.toString(charset); } else { line.writeByte(nextByte); } } } catch (IndexOutOfBoundsException e) { undecodedChunk.readerIndex(readerIndex); throw new NotEnoughDataDecoderException(e); } undecodedChunk.readerIndex(readerIndex); throw new NotEnoughDataDecoderException(); }
Example 16
Source File: DataTypeCodec.java From vertx-sql-client with Apache License 2.0 | 5 votes |
private static Object defaultDecodeText(int index, int len, ByteBuf buff) { // decode unknown text values as text or as an array if it begins with `{` if (len > 1 && buff.getByte(index) == '{') { return textDecodeArray(STRING_ARRAY_FACTORY, DataType.TEXT, index, len, buff); } return textdecodeTEXT(index, len, buff); }
Example 17
Source File: ServerConnectionDecoder.java From spring-boot-protocol with Apache License 2.0 | 4 votes |
private void decodeHandshake(ChannelHandlerContext ctx,ByteBuf packet, int sequenceId,List<Object> out, int protocolVersion) { if (protocolVersion < MINIMUM_SUPPORTED_PROTOCOL_VERSION) { throw new CodecException("Unsupported version of MySQL"); } ServerHandshakePacket.Builder builder = ServerHandshakePacket.builder(); builder.sequenceId(sequenceId) .protocolVersion(protocolVersion) .serverVersion(CodecUtils.readNullTerminatedString(packet)) .connectionId(packet.readIntLE()) .addAuthData(packet, Constants.AUTH_PLUGIN_DATA_PART1_LEN); packet.skipBytes(1); // Skip auth plugin data terminator builder.addCapabilities(CodecUtils.toEnumSet(CapabilityFlags.class, packet.readUnsignedShortLE())); if (packet.isReadable()) { MysqlCharacterSet characterSet = MysqlCharacterSet.findById(packet.readByte()); builder.characterSet(characterSet) .addServerStatus(CodecUtils.readShortEnumSet(packet, ServerStatusFlag.class)) .addCapabilities( CodecUtils.toEnumSet(CapabilityFlags.class, packet.readUnsignedShortLE() << Short.SIZE)); if (builder.hasCapability(CapabilityFlags.CLIENT_SECURE_CONNECTION)) { int authDataLen = packet.readByte(); packet.skipBytes(Constants.HANDSHAKE_RESERVED_BYTES); // Skip reserved bytes int readableBytes = Math.max(Constants.AUTH_PLUGIN_DATA_PART2_MIN_LEN, authDataLen - Constants.AUTH_PLUGIN_DATA_PART1_LEN); builder.addAuthData(packet, readableBytes); if (builder.hasCapability(CapabilityFlags.CLIENT_PLUGIN_AUTH) && packet.isReadable()) { int len = packet.readableBytes(); if (packet.getByte(packet.readerIndex() + len - 1) == 0) { len--; } builder.authPluginName(CodecUtils.readFixedLengthString(packet, len, CharsetUtil.UTF_8)); packet.skipBytes(1); } } } ServerHandshakePacket handshake = builder.build(); out.add(handshake); }
Example 18
Source File: JsonRpcDecoder.java From ovsdb with Eclipse Public License 1.0 | 4 votes |
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out) throws IOException { LOG.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(), recordsRead, lastRecordBytes); if (lastRecordBytes == 0) { if (buf.readableBytes() < 4) { return; //wait for more data } skipSpaces(buf); byte[] buff = new byte[4]; buf.getBytes(buf.readerIndex(), buff); ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4); JsonEncoding jsonEncoding = strapper.detectEncoding(); if (!JsonEncoding.UTF8.equals(jsonEncoding)) { throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported"); } } int index = lastRecordBytes + buf.readerIndex(); for (; index < buf.writerIndex(); index++) { switch (buf.getByte(index)) { case '{': if (!inS) { leftCurlies++; } break; case '}': if (!inS) { rightCurlies++; } break; case '"': if (buf.getByte(index - 1) != '\\') { inS = !inS; } break; default: break; } if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) { ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex()); JsonParser jp = JSON_FACTORY.createParser((InputStream) new ByteBufInputStream(slice)); JsonNode root = jp.readValueAsTree(); out.add(root); leftCurlies = 0; rightCurlies = 0; lastRecordBytes = 0; recordsRead++; break; } /* * Changing this limit to being a warning, we do not wish to "break" in scale environment * and currently this limits the ovs of having only around 50 ports defined... * I do acknowledge the fast that this might be risky in case of huge amount of strings * in which the controller can crash with an OOM, however seems that we need a really huge * ovs to reach that limit. */ //We do not want to issue a log message on every extent of the buffer //hence logging only once if (index - buf.readerIndex() >= maxFrameLength && !maxFrameLimitWasReached) { maxFrameLimitWasReached = true; LOG.warn("***** OVSDB Frame limit of {} bytes has been reached! *****", this.maxFrameLength); } } // end of stream, save the incomplete record index to avoid reexamining the whole on next run if (index >= buf.writerIndex()) { lastRecordBytes = buf.readableBytes(); } }
Example 19
Source File: ChunkDecoder.java From opc-ua-stack with Apache License 2.0 | 4 votes |
private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers) throws UaException { CompositeByteBuf composite = BufferUtil.compositeBuffer(); int signatureSize = delegate.getSignatureSize(channel); int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); boolean encrypted = delegate.isEncryptionEnabled(channel); boolean signed = delegate.isSigningEnabled(channel); for (ByteBuf chunkBuffer : chunkBuffers) { char chunkType = (char) chunkBuffer.getByte(3); chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE); delegate.readSecurityHeader(channel, chunkBuffer); if (encrypted) { decryptChunk(delegate, channel, chunkBuffer); } int encryptedStart = chunkBuffer.readerIndex(); chunkBuffer.readerIndex(0); if (signed) { delegate.verifyChunk(channel, chunkBuffer); } int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0; int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize; chunkBuffer.readerIndex(encryptedStart); SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer); long sequenceNumber = sequenceHeader.getSequenceNumber(); lastRequestId = sequenceHeader.getRequestId(); if (lastSequenceNumber == -1) { lastSequenceNumber = sequenceNumber; } else { if (lastSequenceNumber + 1 != sequenceNumber) { String message = String.format("expected sequence number %s but received %s", lastSequenceNumber + 1, sequenceNumber); logger.error(message); logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex())); throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message); } lastSequenceNumber = sequenceNumber; } ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex()); if (chunkType == 'A') { ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer); throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason()); } composite.addComponent(bodyBuffer); composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes()); } return composite.order(ByteOrder.LITTLE_ENDIAN); }
Example 20
Source File: HandshakePacket.java From Mycat-Balance with Apache License 2.0 | 4 votes |
@Override public HandshakePacket decodeBody(ByteBuf byteBuf, MysqlHeader mysqlHeader) throws DecodeException { this.setMysqlHeader(mysqlHeader); int _index = byteBuf.readerIndex(); int index = _index; protocolVersion = byteBuf.getByte(index++); int len = 0; while (byteBuf.getByte(index+len) != 0) { len++; } versionInfo = new byte[len]; byteBuf.getBytes(index, versionInfo, 0, len); index += len; index++; threadId = byteBuf.getInt(index); index+=4; encrypt1 = new byte[8]; byteBuf.getBytes(index, encrypt1, 0, 8); index += 8; fix1 = byteBuf.getByte(index++); serverProp1 = new byte[2]; byteBuf.getBytes(index, serverProp1, 0, 2); index += 2; charset = byteBuf.getByte(index++); serverStatus = new byte[2]; byteBuf.getBytes(index, serverStatus, 0, 2); index += 2; serverProp2 = new byte[2]; byteBuf.getBytes(index, serverProp2, 0, 2); index += 2; fix2 = byteBuf.getByte(index++); // byte10 = new byte[10]; // byteBuf.getBytes(index, byte10, 0, 10); index += 10; len = 0; while (byteBuf.getByte(index + len) != 0) { len++; } encrypt2 = new byte[len]; byteBuf.getBytes(index, encrypt2, 0, len); index += len; index++; len = 0; while (byteBuf.getByte(index + len) != 0) { len++; } authPluginName = new byte[len]; byteBuf.getBytes(index, authPluginName, 0, len); index += len; index++; byteBuf.readerIndex(index); return this; }