本文整理匯總了Java中io.netty.buffer.ByteBuf.markReaderIndex方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteBuf.markReaderIndex方法的具體用法?Java ByteBuf.markReaderIndex怎麽用?Java ByteBuf.markReaderIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.buffer.ByteBuf
的用法示例。
在下文中一共展示了ByteBuf.markReaderIndex方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
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;
}
}
示例2: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
//獲取消息頭所標識的消息體字節數組長度
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if (dataLength < 0) {
ctx.close();
}
//若當前可以獲取到的字節數小於實際長度,則直接返回,直到當前可以獲取到的字節數等於實際長度
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
//讀取完整的消息體字節數組
byte[] data = new byte[dataLength];
in.readBytes(data);
//將字節數組反序列化為java對象(SerializerEngine參考序列化與反序列化章節)
Object obj = SerializerEngine.deserialize(data, genericClass, serializeType.getCode());
out.add(obj);
}
示例3: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
protected void decode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf, List<Object> paramList)
throws Exception {
paramByteBuf.markReaderIndex();
byte[] arrayOfByte = new byte[5];
for(int i = 0; i < arrayOfByte.length; i++) {
if(!paramByteBuf.isReadable()) {
paramByteBuf.resetReaderIndex();
return;
}
arrayOfByte[i] = paramByteBuf.readByte();
if(arrayOfByte[i] >= 0) {
int j = CodedInputStream.newInstance(arrayOfByte, 0, i + 1).readRawVarint32();
if(j < 0) {
throw new CorruptedFrameException("negative length: " + j);
}
if(paramByteBuf.readableBytes() < j) {
paramByteBuf.resetReaderIndex();
return;
}
paramList.add(paramByteBuf.readBytes(j));
return;
}
}
throw new CorruptedFrameException("length wider than 32-bit");
}
示例4: encode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Test
public void encode() {
// Given
PlayStatePacket expected = createDummyPlayStatePacket();
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
buf.markReaderIndex();
// When
subject.encode(expected, buf);
// Then
PlayStatePacket actual = subject.decode(buf);
assertThat(actual).isEqualTo(expected);
Truth.assertThat(actual.getPlayState()).isEqualTo(expected.getPlayState());
}
示例5: putBytesReverse
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
/**
* Puts the bytes from the specified buffer into this packet's buffer, in reverse.
*
* @param buffer The source {@link ByteBuf}.
*/
public void putBytesReverse(ByteBuf buffer) {
byte[] bytes = new byte[buffer.readableBytes()];
buffer.markReaderIndex();
try {
buffer.readBytes(bytes);
} finally {
buffer.resetReaderIndex();
}
putBytesReverse(bytes);
}
示例6: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
in.markReaderIndex();
byte[] lengthBytes = new byte[3];
for (int i = 0; i < 3; i++)
{
if (!in.isReadable())
{
in.resetReaderIndex();
return;
}
lengthBytes[i] = in.readByte();
if (lengthBytes[i] >= 0)
{
ByteBuf buffer = Unpooled.wrappedBuffer(lengthBytes);
try
{
int packetLength = readVarInt(buffer);
if (in.readableBytes() < packetLength)
{
in.resetReaderIndex();
return;
}
out.add(in.readBytes(packetLength));
}
finally
{
buffer.release();
}
return;
}
}
}
示例7: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (!ctx.channel().isOpen()) {
if (in.readableBytes() > 0) {
logger.info("Channel is closed, discarding remaining {} byte(s) in buffer.", in.readableBytes());
}
in.skipBytes(in.readableBytes());
return;
}
in.markReaderIndex();
/**
* a variable-width message length can be up to five bytes in length. read bytes until we have a length.
*/
final byte[] buf = new byte[5];
int length = 0;
for (int i = 0; i < buf.length; i++) {
if (!in.isReadable()) {
in.resetReaderIndex();
return;
}
buf[i] = in.readByte();
if (buf[i] >= 0) {
length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();
if (length < 0) {
throw new CorruptedFrameException("negative length: " + length);
}
if (length == 0) {
throw new CorruptedFrameException("Received a message of length 0.");
}
if (in.readableBytes() < length) {
in.resetReaderIndex();
return;
} else {
// complete message in buffer.
break;
}
}
}
final ByteBuf frame = in.slice(in.readerIndex(), length);
try {
final InboundRpcMessage message = decodeMessage(ctx, frame, length);
if (message != null) {
out.add(message);
}
} finally {
in.skipBytes(length);
}
}
示例8: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
Stopwatch watch = Stopwatch.createStarted();
// Input input = new Input(new ByteBufInputStream(in));
// Object result = kryo.readClassAndObject(input);
// out.add(result);
// input.close();
if (in.readableBytes() < HEAD_LENGTH) { //這個HEAD_LENGTH是我們用於表示頭長度的字節數。 由於Encoder中我們傳的是一個int類型的值,所以這裏HEAD_LENGTH的值為4.
return;
}
in.markReaderIndex(); //我們標記一下當前的readIndex的位置
int dataLength = in.readInt(); // 讀取傳送過來的消息的長度。ByteBuf 的readInt()方法會讓他的readIndex增加4
if (dataLength < 0) { // 我們讀到的消息體長度為0,這是不應該出現的情況,這裏出現這情況,關閉連接。
ctx.close();
}
if (in.readableBytes() < dataLength) { //讀到的消息體長度如果小於我們傳送過來的消息長度,則resetReaderIndex. 這個配合markReaderIndex使用的。把readIndex重置到mark的地方
in.resetReaderIndex();
return;
}
byte[] body = new byte[dataLength]; //傳輸正常
in.readBytes(body);
RemotingProtocol o = convertToObject(body); //將byte數據轉化為我們需要的對象
out.add(o);
if (logger.isDebugEnabled())
logger.debug("kyro decoder use:" + watch.toString());
}
示例9: ByteBufStreamInput
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
ByteBufStreamInput(ByteBuf buffer, int length) {
if (length > buffer.readableBytes()) {
throw new IndexOutOfBoundsException();
}
this.buffer = buffer;
startIndex = buffer.readerIndex();
endIndex = startIndex + length;
buffer.markReaderIndex();
}
示例10: parseFrame
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
private void parseFrame(ByteBuf buffer, List<Object> out) throws Exception {
buffer.markReaderIndex();
if (buffer.readableBytes() > FRAME_SIZE_WITHOUT_PAYLOAD) {
byte type = buffer.readByte();
int channel = buffer.readShort();
long payloadSize = buffer.readInt();
long estimatedRemainingSize = payloadSize + 1;
if (buffer.readableBytes() < estimatedRemainingSize) {
buffer.resetReaderIndex();
return;
}
GeneralFrame frame = null;
switch (type) {
case 1: // Method
short amqpClass = buffer.readShort();
short amqpMethod = buffer.readShort();
AmqMethodBodyFactory factory = methodRegistry.getFactory(amqpClass, amqpMethod);
frame = factory.newInstance(buffer, channel, payloadSize);
break;
case 2: // Header
frame = HeaderFrame.parse(buffer, channel);
break;
case 3: // Body
frame = ContentFrame.parse(buffer, channel, payloadSize);
break;
case 4: // Heartbeat
throw new Exception("Method Not implemented");
}
byte frameEnd = buffer.readByte();
if (frameEnd != (byte) GeneralFrame.FRAME_END) {
throw new Exception("Invalid AMQP frame");
}
out.add(frame);
}
}
示例11: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
// System.out.println(in.order() + "in.readableBytes()" + in.readableBytes());
// byte[] bs = new byte[in.readableBytes()];
// in.readBytes(bs);
// String fullHexDump = ByteUtils.fullHexDump(bs);
// System.out.println(fullHexDump);
// System.out.println(new String(bs));
// in.resetReaderIndex();
// in.markReaderIndex();
// byte[] bs=new byte[in.capacity()];
// in.readBytes(bs);
//// System.out.println(in.readIntLE());
// ByteUtils.fullHexDump(bs);
// in.resetReaderIndex();
int dataLength = in.readInt();
// System.out.println(dataLength + "======");
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
int opcode = in.readInt();
// System.out.println("opcode=" + opcode);
byte[] decoded = new byte[dataLength - 4];
in.readBytes(decoded);
BaseByteArrayPacket messagePack = new BaseByteArrayPacket(opcode, decoded);
out.add(messagePack);
}
示例12: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list)
throws Exception {
if (byteBuf.readableBytes() < BASE_LENGTH) {
logger.error("[error] >>> head length error");
return;
}
byteBuf.markReaderIndex();
CommonProtocol protocol = new CommonProtocol();
protocol.setMagic(byteBuf.readShort());
protocol.setVersion(byteBuf.readShort());
protocol.setType(byteBuf.readShort());
if(protocol.getType() != 1) {
//read len error
if (byteBuf.readableBytes() < 4) {
byteBuf.resetReaderIndex();
return;
}
protocol.setLen(byteBuf.readInt());
//read body error
if (byteBuf.readableBytes() < protocol.getLen()) {
byteBuf.resetReaderIndex();
return;
}
byte[] data = new byte[protocol.getLen()];
byteBuf.readBytes(data);
protocol.setBody(data);
}
list.add(protocol);
}
示例13: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> out) throws Exception {
if(in.readableBytes() < 4){
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if(in.readableBytes() < dataLength){
in.resetReaderIndex();
return;
}
byte[] data = new byte[dataLength];
in.readBytes(data);
Kryo kryo = null;
try{
kryo = pool.borrow();
Input input = new Input(data);
Object obj = kryo.readObject(input,this.clazz);
out.add(obj);
}catch(Exception e){
LOG.warn("MessageDecoder happen exception.",e);
}finally{
if(kryo != null){
pool.release(kryo);
}
}
}
示例14: decode
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
protected void decode(ChannelHandlerContext p_decode_1_, ByteBuf p_decode_2_, List<Object> p_decode_3_) throws Exception
{
p_decode_2_.markReaderIndex();
byte[] abyte = new byte[3];
for (int i = 0; i < abyte.length; ++i)
{
if (!p_decode_2_.isReadable())
{
p_decode_2_.resetReaderIndex();
return;
}
abyte[i] = p_decode_2_.readByte();
if (abyte[i] >= 0)
{
PacketBuffer packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(abyte));
try
{
int j = packetbuffer.readVarIntFromBuffer();
if (p_decode_2_.readableBytes() >= j)
{
p_decode_3_.add(p_decode_2_.readBytes(j));
return;
}
p_decode_2_.resetReaderIndex();
}
finally
{
packetbuffer.release();
}
return;
}
}
throw new CorruptedFrameException("length wider than 21-bit");
}
示例15: channelRead
import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
public void channelRead(ChannelHandlerContext p_channelRead_1_, Object p_channelRead_2_) throws Exception
{
ByteBuf bytebuf = (ByteBuf)p_channelRead_2_;
bytebuf.markReaderIndex();
boolean flag = true;
try
{
if (bytebuf.readUnsignedByte() == 254)
{
InetSocketAddress inetsocketaddress = (InetSocketAddress)p_channelRead_1_.channel().remoteAddress();
MinecraftServer minecraftserver = this.networkSystem.getServer();
int i = bytebuf.readableBytes();
switch (i)
{
case 0:
LOGGER.debug("Ping: (<1.3.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
String s2 = String.format("%s\u00a7%d\u00a7%d", new Object[] {minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s2));
break;
case 1:
if (bytebuf.readUnsignedByte() != 1)
{
return;
}
LOGGER.debug("Ping: (1.4-1.5.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
String s = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s));
break;
default:
boolean flag1 = bytebuf.readUnsignedByte() == 1;
flag1 = flag1 & bytebuf.readUnsignedByte() == 250;
flag1 = flag1 & "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() * 2).array(), Charsets.UTF_16BE));
int j = bytebuf.readUnsignedShort();
flag1 = flag1 & bytebuf.readUnsignedByte() >= 73;
flag1 = flag1 & 3 + bytebuf.readBytes(bytebuf.readShort() * 2).array().length + 4 == j;
flag1 = flag1 & bytebuf.readInt() <= 65535;
flag1 = flag1 & bytebuf.readableBytes() == 0;
if (!flag1)
{
return;
}
LOGGER.debug("Ping: (1.6) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
String s1 = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
ByteBuf bytebuf1 = this.getStringBuffer(s1);
try
{
this.writeAndFlush(p_channelRead_1_, bytebuf1);
}
finally
{
bytebuf1.release();
}
}
bytebuf.release();
flag = false;
return;
}
}
catch (RuntimeException var21)
{
return;
}
finally
{
if (flag)
{
bytebuf.resetReaderIndex();
p_channelRead_1_.channel().pipeline().remove("legacy_query");
p_channelRead_1_.fireChannelRead(p_channelRead_2_);
}
}
}