List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:com.jayqqaa12.jbase.tcp.netty.code.DreamJSONDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { while (true) { if (in.readableBytes() <= 4) { break; }//from w w w . ja va2s . co m in.markReaderIndex(); int length = in.readInt(); if (length <= 0) { throw new Exception("a negative length occurd while decode!"); } if (in.readableBytes() < length) { in.resetReaderIndex(); break; } byte[] msg = new byte[length]; in.readBytes(msg); out.add(new String(msg, "UTF-8")); } }
From source file:com.kradac.karview.netty.MyDecoder.java
@Override protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception { if (bb.readableBytes() < 2) { return;/* w ww . j ava2s . c o m*/ } bb.markReaderIndex(); int length = bb.readChar(); if (length > 150) { String data = ""; while (bb.isReadable()) { data += (char) bb.readByte(); } bb.clear(); if (data.contains("OK") || data.contains("handshake")) { if (data.contains("handshake")) { chc.channel().write("0%%at"); } if (data.contains("OK")) { System.out.println("Respuesta de Comando AT [" + data + "]"); } } else { System.err.println("Datos incorrectos enviados al Servidor [" + data + "]"); chc.channel().disconnect(); } } if (bb.readableBytes() < length - 2) { bb.resetReaderIndex(); return; } in.writeBytes(bb);//Escribimos los bytes in.discardReadBytes();// in.retain(); list.add(in); bb.clear();//vaciamos el byteBuf }
From source file:com.linecorp.armeria.client.endpoint.dns.DnsServiceEndpointGroup.java
License:Apache License
@Override ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception { final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder(); for (DnsRecord r : records) { if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.SRV) { continue; }/* www . j a va 2 s .com*/ final ByteBuf content = ((ByteBufHolder) r).content(); if (content.readableBytes() <= 6) { // Too few bytes warnInvalidRecord(DnsRecordType.SRV, content); continue; } content.markReaderIndex(); content.skipBytes(2); // priority unused final int weight = content.readUnsignedShort(); final int port = content.readUnsignedShort(); final Endpoint endpoint; try { final String target = stripTrailingDot(DefaultDnsRecordDecoder.decodeName(content)); endpoint = port > 0 ? Endpoint.of(target, port) : Endpoint.of(target); } catch (Exception e) { content.resetReaderIndex(); warnInvalidRecord(DnsRecordType.SRV, content); continue; } builder.add(endpoint.withWeight(weight)); } final ImmutableSortedSet<Endpoint> endpoints = builder.build(); if (logger().isDebugEnabled()) { logger().debug("{} Resolved: {} (TTL: {})", logPrefix(), endpoints.stream().map(e -> e.authority() + '/' + e.weight()).collect(Collectors.joining(", ")), ttl); } return endpoints; }
From source file:com.linecorp.armeria.client.endpoint.dns.DnsTextEndpointGroup.java
License:Apache License
@Override ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception { final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder(); for (DnsRecord r : records) { if (!(r instanceof DnsRawRecord) || r.type() != DnsRecordType.TXT) { continue; }// ww w .ja va2 s. c o m final ByteBuf content = ((ByteBufHolder) r).content(); if (!content.isReadable()) { // Missing length octet warnInvalidRecord(DnsRecordType.TXT, content); continue; } content.markReaderIndex(); final int txtLen = content.readUnsignedByte(); if (txtLen == 0) { // Empty content continue; } if (content.readableBytes() != txtLen) { // Mismatching number of octets content.resetReaderIndex(); warnInvalidRecord(DnsRecordType.TXT, content); continue; } final byte[] txt = new byte[txtLen]; content.readBytes(txt); final Endpoint endpoint; try { endpoint = mapping.apply(txt); } catch (Exception e) { content.resetReaderIndex(); warnInvalidRecord(DnsRecordType.TXT, content); continue; } if (endpoint != null) { if (endpoint.isGroup()) { logger().warn("{} Ignoring group endpoint: {}", logPrefix(), endpoint); } else { builder.add(endpoint); } } } final ImmutableSortedSet<Endpoint> endpoints = builder.build(); if (logger().isDebugEnabled()) { logger().debug("{} Resolved: {} (TTL: {})", logPrefix(), endpoints.stream().map(Object::toString).collect(Collectors.joining(", ")), ttl); } return endpoints; }
From source file:com.mobius.software.android.iotbroker.mqtt.parser.MQParser.java
License:Open Source License
public static ByteBuf next(ByteBuf buf) throws MalformedMessageException { buf.markReaderIndex(); MessageType type = MessageType.valueOf(((buf.readByte() >> 4) & 0xf)); switch (type) { case PINGREQ: case PINGRESP: case DISCONNECT: buf.resetReaderIndex();// w w w .j a v a2 s . c o m return Unpooled.buffer(2); default: LengthDetails length = decodeLength(buf); buf.resetReaderIndex(); if (length.getLength() == 0) return null; int result = length.getLength() + length.getSize() + 1; return result <= buf.readableBytes() ? Unpooled.buffer(result) : null; } }
From source file:com.mobius.software.mqtt.parser.MQParser.java
License:Open Source License
public static ByteBuf next(ByteBuf buf, int maxMessageSize) throws MalformedMessageException { buf.markReaderIndex(); MessageType type = MessageType.valueOf(((buf.readByte() >> 4) & 0xf)); if (type == null) { buf.resetReaderIndex();/*from w ww. j av a2 s . c om*/ throw new MalformedMessageException("invalid message type decoding"); } switch (type) { case PINGREQ: case PINGRESP: case DISCONNECT: buf.resetReaderIndex(); return Unpooled.buffer(2); default: LengthDetails length = LengthDetails.decode(buf); buf.resetReaderIndex(); if (length.getLength() == 0) return null; int result = length.getLength() + length.getSize() + 1; if (result > buf.readableBytes()) throw new MalformedMessageException("invalid length decoding for " + type + " result length:" + result + ", in buffer:" + buf.readableBytes()); if (result > maxMessageSize) throw new MalformedMessageException("message length exceeds limit " + maxMessageSize); return Unpooled.buffer(result); } }
From source file:com.mpush.netty.codec.PacketDecoder.java
License:Apache License
private void decodeFrames(ByteBuf in, List<Object> out) { if (in.readableBytes() >= Packet.HEADER_LEN) { //1.????.??frame,????,? in.markReaderIndex(); Packet packet = decodeFrame(in); if (packet != null) { out.add(packet);//from w w w . j a va 2s.c o m } else { //2.??frame,????,? in.resetReaderIndex(); } } }
From source file:com.myseti.framework.monitor.net.DecodeProtocol.java
@Override protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception { if (bb.readableBytes() < Utility.PACKAGE_LENGTH) { return;/*from www .j ava 2 s. c o m*/ } else { bb.markReaderIndex(); ByteBufInputStream stream = new ByteBufInputStream(bb); try { byte[] len = new byte[4]; stream.read(len, 0, 4); int length = com.myseti.framework.core.HexTools.byte2Int(len); System.out.print("" + length + "\n"); byte[] result = new byte[length - 4]; stream.read(result, 0, length - 4); ProtocolEntity entity = new ProtocolEntity(result); list.add(entity); System.out.print("DecodeProtocol active! add a new entity \n"); } catch (Exception ex) { System.out.print(ex.toString()); } } }
From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java
License:Apache License
void getFrames(ByteBuf in, List<byte[]> out) { while (true) { in.markReaderIndex(); final int stx = in.readerIndex(); if (hasFrame(in) == false) { in.resetReaderIndex();/*ww w . ja v a2 s.co m*/ break; } int length = in.readerIndex() - stx; byte[] readBytes = new byte[length]; in.resetReaderIndex(); in.readBytes(readBytes); out.add(readBytes); } }
From source file:com.newlandframework.avatarmq.netty.MessageObjectDecoder.java
License:Apache License
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < MessageObjectDecoder.MESSAGE_LENGTH) { return;/*from w w w . j av a 2 s .c o m*/ } in.markReaderIndex(); int messageLength = in.readInt(); if (messageLength < 0) { ctx.close(); } if (in.readableBytes() < messageLength) { in.resetReaderIndex(); return; } else { byte[] messageBody = new byte[messageLength]; in.readBytes(messageBody); try { Object obj = util.decode(messageBody); out.add(obj); } catch (IOException ex) { Logger.getLogger(MessageObjectDecoder.class.getName()).log(Level.SEVERE, null, ex); } } }