Example usage for io.netty.buffer ByteBuf writeByte

List of usage examples for io.netty.buffer ByteBuf writeByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf writeByte.

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

From source file:com.bluepowermod.part.tube.TubeStack.java

License:Open Source License

public void writeToPacket(ByteBuf buf) {

    ByteBufUtils.writeItemStack(buf, stack);
    buf.writeByte(heading.ordinal());
    buf.writeByte((byte) color.ordinal());
    buf.writeDouble(speed);//from w  w w  .java  2  s  .c  o m
    buf.writeDouble(progress);
}

From source file:com.book.netty5.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;/*from  ww  w  .j av a2  s . c  o  m*/
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else {
        sendBuf.writeInt(0);
    }
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);
}

From source file:com.buildria.mocking.stub.CallTest.java

License:Open Source License

@Test
public void testFromRequest() throws Exception {
    DefaultFullHttpRequest req = mock(DefaultFullHttpRequest.class);

    when(req.getUri()).thenReturn("/api/p?name=%E3%81%82");
    when(req.getMethod()).thenReturn(GET);

    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add("key", "value1");
    headers.add("key", "value2");
    when(req.headers()).thenReturn(headers);

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
    buf.writeByte((byte) 0xff);
    when(req.content()).thenReturn(buf);

    Call call = Call.fromRequest(req);/*  w  ww  .  j a va  2  s .c om*/

    assertThat(call.getPath(), is("/api/p"));
    assertThat(call.getParameters().size(), is(1));
    assertThat(call.getParameters().get(0).getName(), is("name"));
    assertThat(call.getParameters().get(0).getValue(), is("\u3042"));
    assertThat(call.getMethod(), is(equalToIgnoringCase("GET")));
    assertThat(call.getHeaders(), hasSize(2));
    assertThat(call.getHeaders().get(0).getName(), is("key"));
    assertThat(call.getHeaders().get(0).getValue(), is("value1"));
    assertThat(call.getHeaders().get(1).getName(), is("key"));
    assertThat(call.getHeaders().get(1).getValue(), is("value2"));
    assertThat(call.getBody()[0], is((byte) 0xff));
}

From source file:com.builtbroken.atomic.network.packet.PacketBase.java

/**
 * Called to write data without manually defining the write
 *
 * @param object - object to write/*from  w  w w.  j  a  va  2s. co m*/
 * @param buffer - location to write to
 */
protected void writeData(Object object, ByteBuf buffer) {
    if (object.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(object); i++) {
            writeData(Array.get(object, i), buffer);
        }
    } else if (object instanceof Collection) {
        for (Object o : (Collection) object) {
            writeData(o, buffer);
        }
    } else if (object instanceof Byte) {
        buffer.writeByte((Byte) object);
    } else if (object instanceof Integer) {
        buffer.writeInt((Integer) object);
    } else if (object instanceof Short) {
        buffer.writeShort((Short) object);
    } else if (object instanceof Long) {
        buffer.writeLong((Long) object);
    } else if (object instanceof Float) {
        buffer.writeFloat((Float) object);
    } else if (object instanceof Double) {
        buffer.writeDouble((Double) object);
    } else if (object instanceof Boolean) {
        buffer.writeBoolean((Boolean) object);
    } else if (object instanceof String) {
        ByteBufUtils.writeUTF8String(buffer, (String) object);
    } else if (object instanceof NBTTagCompound) {
        ByteBufUtils.writeTag(buffer, (NBTTagCompound) object);
    } else if (object instanceof ItemStack) {
        ByteBufUtils.writeItemStack(buffer, (ItemStack) object);
    } else if (object instanceof FluidTank) {
        ByteBufUtils.writeTag(buffer, ((FluidTank) object).writeToNBT(new NBTTagCompound()));
    } else if (object instanceof Pos) {
        ((Pos) object).writeByteBuf(buffer);
    } else if (object instanceof IByteBufWriter) {
        ((IByteBufWriter) object).writeBytes(buffer);
    } else if (object instanceof Enum) {
        buffer.writeInt(((Enum) object).ordinal());
    } else {
        throw new IllegalArgumentException("PacketBase: Unsupported write data type " + object);
    }
}

From source file:com.builtbroken.icbm.content.fragments.EntityFragment.java

@Override
public void writeSpawnData(ByteBuf buffer) {
    if (fragmentType != null) {
        buffer.writeByte(fragmentType.ordinal());
    } else {//from   w  w  w  . j a v  a  2 s. co  m
        buffer.writeByte(-1);
    }
    if (fragmentMaterial != null) {
        buffer.writeInt(Block.getIdFromBlock(fragmentMaterial));
    } else {
        buffer.writeByte(-1);
    }
    if (renderShape != null) {
        buffer.writeBoolean(true);
        renderShape.writeBytes(buffer);
    } else {
        buffer.writeBoolean(false);
    }
}

From source file:com.builtbroken.icbm.content.launcher.launcher.standard.TileStandardLauncher.java

@Override
public void writeDescPacket(ByteBuf buf) {
    if (getMissileItem() != null) {
        buf.writeByte(0);
        ByteBufUtils.writeItemStack(buf, getMissileItem());
    } else if (isCrafting) {
        if (recipe != null) {
            buf.writeByte(1);/*from  w  w w.  j  a v  a 2 s.  c o m*/
            recipe.writeBytes(buf);
        } else {
            buf.writeByte(2);
        }
    } else {
        buf.writeByte(3);
    }
}

From source file:com.cc.nettytest.proxy.encoder.CCLengthFieldPrepender.java

License:Apache License

@Override
public void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {

    int length = lengthIncludesLengthFieldLength ? msg.readableBytes() + lengthFieldLength
            : msg.readableBytes();//from   w ww  . j a v a  2 s .  co  m
    switch (lengthFieldLength) {
    case 1:
        if (length >= 256) {
            throw new IllegalArgumentException("length does not fit into a byte: " + length);
        }
        out.writeByte((byte) length);
        break;
    case 2:
        if (length >= 65536) {
            throw new IllegalArgumentException("length does not fit into a short integer: " + length);
        }
        out.writeShort((short) length);
        break;
    case 3:
        if (length >= 16777216) {
            throw new IllegalArgumentException("length does not fit into a medium integer: " + length);
        }
        out.writeMedium(length);
        break;
    case 4:
        out.writeInt(ByteBufUtil.swapInt((int) length)); //SWAP FOR UIMANAGER
        break;
    case 8:
        out.writeLong(length);
        break;
    default:
        throw new Error("should not reach here");
    }

    out.writeBytes(msg, msg.readerIndex(), msg.readableBytes());
}

From source file:com.chat.common.netty.handler.encode.ProtobufVarint32LengthFieldPrepender.java

License:Apache License

/**
 * Writes protobuf varint32 to (@link ByteBuf).
 * @param out to be written to//  w w  w.j  a v a 2 s  . c  o  m
 * @param value to be written
 * @throws IOException
 */
static void writeRawVarint32(ByteBuf out, int value) throws IOException {
    while (true) {
        if ((value & ~0x7F) == 0) {
            out.writeByte(value);
            return;
        } else {
            out.writeByte((value & 0x7F) | 0x80);
            value >>>= 7;
        }
    }
}

From source file:com.cloudhopper.smpp.pdu.BaseBind.java

License:Apache License

@Override
public void writeBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    ChannelBufferUtil.writeNullTerminatedString(buffer, this.systemId);
    ChannelBufferUtil.writeNullTerminatedString(buffer, this.password);
    ChannelBufferUtil.writeNullTerminatedString(buffer, this.systemType);
    buffer.writeByte(this.interfaceVersion);
    ChannelBufferUtil.writeAddress(buffer, this.addressRange);
}

From source file:com.cloudhopper.smpp.pdu.BaseSm.java

License:Apache License

@Override
public void writeBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    ChannelBufferUtil.writeNullTerminatedString(buffer, this.serviceType);
    ChannelBufferUtil.writeAddress(buffer, this.sourceAddress);
    ChannelBufferUtil.writeAddress(buffer, this.destAddress);
    buffer.writeByte(this.esmClass);
    buffer.writeByte(this.protocolId);
    buffer.writeByte(this.priority);
    ChannelBufferUtil.writeNullTerminatedString(buffer, this.scheduleDeliveryTime);
    ChannelBufferUtil.writeNullTerminatedString(buffer, this.validityPeriod);
    buffer.writeByte(this.registeredDelivery);
    buffer.writeByte(this.replaceIfPresent);
    buffer.writeByte(this.dataCoding);
    buffer.writeByte(this.defaultMsgId);
    buffer.writeByte((byte) getShortMessageLength());
    if (this.shortMessage != null) {
        buffer.writeBytes(this.shortMessage);
    }/*from ww w  .j av  a 2  s.c  o m*/
}