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

The following are Jave code examples for showing how to use clear() 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: CentauriCloud   File: PacketDecoder.java   Source Code and License Vote up 6 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
	if (in.readableBytes() <= 0)
		return;

	Packet packet = null;
	byte packetId = in.readByte();

	if (packetId < 0) {
		in.clear();
		throw new DecoderException("WTF, why is the packet id lower than zero?!?! Id: " + packetId);
	}
	Class<? extends Packet> clazz = PacketManager.getInstance().getPacketClass(packetId);

	if (clazz != null)
		packet = clazz.newInstance();

	if (packet == null) {
		throw new DecoderException("Cannot find packet id: " + packetId);
	}

	packet.decode(in);
	out.add(packet);
}
 
Example 2
Project: pnc-repressurized   File: PacketDescription.java   Source Code and License Vote up 6 votes
public NBTTagCompound writeNBT(NBTTagCompound compound) {
    compound.setTag("Pos", NBTUtil.createPosTag(pos));
    compound.setInteger("SyncType", type.ordinal());
    compound.setInteger("Length", values.length);
    ByteBuf buf = Unpooled.buffer();
    NBTTagList list = new NBTTagList();
    for (int i = 0; i < types.length; i++) {
        NBTTagCompound element = new NBTTagCompound();
        element.setByte("Type", types[i]);
        buf.clear();
        PacketUpdateGui.writeField(buf, values[i], types[i]);
        element.setByteArray("Value", Arrays.copyOf(buf.array(), buf.writerIndex()));
        list.appendTag(element);
    }
    buf.release();
    compound.setTag("Data", list);
    compound.setTag("Extra", extraData);

    return compound;
}
 
Example 3
Project: neto   File: ProtocolUnificationHandler.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    // 프로토콜 확인을 위해서 5byte를 확인한다.
    if (in.readableBytes() < 5) {
        // 다음 inbound 시 버퍼에 이전에 전송된 내용이 쌓이므로 버퍼를 비운다.
        in.clear();
        return;
    }

    if (isSsl(in)) {
        enableSsl(ctx);
    } else {

        if (!isUnificationMode) {
            switchToBinary(ctx);
            return;
        }

        final int magicPacket1 = in.getUnsignedByte(in.readerIndex());
        final int magicPacket2 = in.getUnsignedByte(in.readerIndex() + 1);

        if (isHttp(magicPacket1, magicPacket2)) {
            switchToHttp(ctx);
        } else {
            switchToBinary(ctx);
        }
    }
}
 
Example 4
Project: echidna   File: HTTPDecoder.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf,
	List<Object> list) throws Exception {
	String rawData = byteBuf.toString(CharsetUtil.UTF_8);
	
	HTTPRequest request = HTTPRequestParser.parseRequest(rawData);
	
	list.add(request);
	
	byteBuf.clear();
}
 
Example 5
Project: L2J-Global   File: PacketEncoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(ChannelHandlerContext ctx, IOutgoingPacket packet, ByteBuf out)
{
	if (out.order() != _byteOrder)
	{
		out = out.order(_byteOrder);
	}
	
	try
	{
		if (packet.write(new PacketWriter(out)))
		{
			if (out.writerIndex() > _maxPacketSize)
			{
				throw new IllegalStateException("Packet (" + packet + ") size (" + out.writerIndex() + ") is bigger than the limit (" + _maxPacketSize + ")");
			}
		}
		else
		{
			// Avoid sending the packet
			out.clear();
		}
	}
	catch (Throwable e)
	{
		LOGGER.log(Level.WARNING, "Failed sending Packet(" + packet + ")", e);
		// Avoid sending the packet if some exception happened
		out.clear();
	}
}