Java Code Examples for io.netty.buffer.ByteBuf.readCharSequence()

The following are Jave code examples for showing how to use readCharSequence() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: message-broker   File: AmqpDecoder.java   Source Code and License Vote up 7 votes
private void processProtocolInitFrame(ByteBuf buffer, List<Object> out) {
    if (buffer.readableBytes() >= 8) {
        CharSequence protocolName = buffer.readCharSequence(4, CharsetUtil.US_ASCII);
        buffer.skipBytes(1);
        byte majorVersion = buffer.readByte();
        byte minorVersion = buffer.readByte();
        byte revision = buffer.readByte();

        if (!AMQP_PROTOCOL_IDENTIFIER.equals(protocolName)) {
            out.add(new AmqpBadMessage(new IllegalArgumentException("Unknown protocol name " +
                                                                           protocolName.toString())));
            currentState = State.BAD_MESSAGE;
        }

        out.add(new ProtocolInitFrame(majorVersion, minorVersion, revision));
    }
}
 
Example 2
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 6 votes
@Override
public Number decodeText(int len, ByteBuf buff) {
  // Todo optimize that
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  BigDecimal big = new BigDecimal(cs.toString());
  // julien : that does not seem consistent to either return a Double or BigInteger
  if (big.scale() == 0) {
    return big.toBigInteger();
  } else {
    // we might loose precision here
    return big.doubleValue();
  }
}
 
Example 3
Project: redis-cluster-watchdog   File: ClusterMessageDecoder.java   Source Code and License Vote up 6 votes
protected ClusterMessage decode(ByteBuf in) {
    in.markReaderIndex();
    try {
        ClusterMessage hdr = new ClusterMessage();
        hdr.signature = (String) in.readCharSequence(4, UTF_8);
        hdr.length = in.readInt();
        if (in.readableBytes() < hdr.length - 8) {
            in.resetReaderIndex(); return null;
        }
        hdr.version = Version.valueOf(in.readUnsignedShort());
        if (hdr.version == PROTOCOL_V0) decodeMessageV0(hdr, in);
        else if (hdr.version == PROTOCOL_V1) decodeMessageV1(hdr, in);
        else throw new UnsupportedOperationException("version: " + hdr.version);
        return hdr;
    } catch (Exception e) {
        in.resetReaderIndex(); return null;
    }
}
 
Example 4
Project: RakNetty   File: LoginPacket.java   Source Code and License Vote up 5 votes
@Override
public void read(RakNetByteBuf in) {
    super.read(in);
    protocolVersion = in.readInt();
    edition = in.readUnsignedByte();

    ByteBuf buf = in.readBytes(
            in.readUnsignedVarInt()
    );

    chainData = buf.readCharSequence(buf.readIntLE(), Charsets.UTF_8);
    skinData = buf.readCharSequence(buf.readIntLE(), Charsets.UTF_8);
    buf.release();
}
 
Example 5
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 5 votes
@Override
public Object decodeText(int len, ByteBuf buff) {
  // Not sure this is correct
  // Try to do without the intermediary String
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return decodeJson(cs.toString());
}
 
Example 6
Project: minebox   File: HandshakePhase.java   Source Code and License Vote up 4 votes
private String processOption(ChannelHandlerContext ctx, ByteBuf in) throws IOException {
     switch (currentOption) {
         case Protocol.NBD_OPT_EXPORT_NAME:
             CharSequence exportName = in.readCharSequence((int) currentOptionLen, Charset.forName("UTF-8"));

             if (!exportProvider.supportsClientFlags(clientFlags)) {
                 LOGGER.error("client flags {} not supported", clientFlags);
                 ctx.channel().close();
                 break;
             }

             long exportSize = 0;
             if ((exportSize = exportProvider.open(exportName)) < 0) {
                 // ep us unwilling to export this name
                 ctx.channel().close();
                 break;
             }

/* build response */
             ByteBuf resp = ctx.alloc().buffer(256);
             resp.writeLong(exportSize);
             short transmissionFlags =
                     Protocol.NBD_FLAG_HAS_FLAGS
                             | Protocol.NBD_FLAG_SEND_FLUSH
                             | Protocol.NBD_FLAG_SEND_TRIM;
             resp.writeShort(transmissionFlags);

             if ((clientFlags & Protocol.NBD_FLAG_NO_ZEROES) == 0) {
                 resp.writeZero(124);
             }
             ctx.writeAndFlush(resp);

             //FIXME: transfer any remaining bytes into the transmission phase!
         /* The NBD protocol has two phases: the handshake (HS_) and the transmission (TM_) */
             // Handshake complete, switch to transmission phase
             ctx.pipeline().addLast("transmission", new TransmissionPhase(config, exportProvider));
             ctx.pipeline().remove(this);
             return exportName.toString();

         default:
             sendHandshakeOptionHagglingReply(ctx, currentOption, Protocol.NBD_REP_ERR_UNSUP, null);
     }
     return null;
 }
 
Example 7
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public Float decodeText(int len, ByteBuf buff) {
  // Todo optimize that
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return Float.parseFloat(cs.toString());
}
 
Example 8
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public Double decodeText(int len, ByteBuf buff) {
  // Todo optimize that
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return Double.parseDouble(cs.toString());
}
 
Example 9
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public LocalDate decodeText(int len, ByteBuf buff) {
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return LocalDate.parse(cs);
}
 
Example 10
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public LocalTime decodeText(int len, ByteBuf buff) {
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return LocalTime.parse(cs);
}
 
Example 11
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public OffsetTime decodeText(int len, ByteBuf buff) {
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return OffsetTime.parse(cs, TIMETZ_FORMAT);
}
 
Example 12
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public LocalDateTime decodeText(int len, ByteBuf buff) {
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return LocalDateTime.parse(cs, TIMESTAMP_FORMAT);
}
 
Example 13
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public OffsetDateTime decodeText(int len, ByteBuf buff) {
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return OffsetDateTime.parse(cs, TIMESTAMPTZ_FORMAT);
}
 
Example 14
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 4 votes
@Override
public Object decodeText(int len, ByteBuf buff) {
  // Try to do without the intermediary String
  CharSequence cs = buff.readCharSequence(len, StandardCharsets.UTF_8);
  return decodeJson(cs.toString());
}
 
Example 15
Project: tools   File: HandshakePhase.java   Source Code and License Vote up 4 votes
private String processOption(ChannelHandlerContext ctx, ByteBuf in) throws IOException {
     switch (currentOption) {
         case Protocol.NBD_OPT_EXPORT_NAME:
             CharSequence exportName = in.readCharSequence((int) currentOptionLen, Charset.forName("UTF-8"));

             if (!exportProvider.supportsClientFlags(clientFlags)) {
                 LOGGER.error("client flags {} not supported", clientFlags);
                 ctx.channel().close();
                 break;
             }

             long exportSize = 0;
             if ((exportSize = exportProvider.open(exportName)) < 0) {
                 // ep us unwilling to export this name
                 ctx.channel().close();
                 break;
             }

/* build response */
             ByteBuf resp = ctx.alloc().buffer(256);
             resp.writeLong(exportSize);
             short transmissionFlags =
                     Protocol.NBD_FLAG_HAS_FLAGS
                             | Protocol.NBD_FLAG_SEND_FLUSH
                             | Protocol.NBD_FLAG_SEND_TRIM;
             resp.writeShort(transmissionFlags);

             if ((clientFlags & Protocol.NBD_FLAG_NO_ZEROES) == 0) {
                 resp.writeZero(124);
             }
             ctx.writeAndFlush(resp);

             //FIXME: transfer any remaining bytes into the transmission phase!
         /* The NBD protocol has two phases: the handshake (HS_) and the transmission (TM_) */
             // Handshake complete, switch to transmission phase
             ctx.pipeline().addLast("transmission", new TransmissionPhase(config, exportProvider));
             ctx.pipeline().remove(this);
             return exportName.toString();

         default:
             sendHandshakeOptionHagglingReply(ctx, currentOption, Protocol.NBD_REP_ERR_UNSUP, null);
     }
     return null;
 }