Example usage for io.netty.buffer ByteBuf writeMedium

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

Introduction

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

Prototype

public abstract ByteBuf writeMedium(int value);

Source Link

Document

Sets the specified 24-bit medium integer at the current writerIndex and increases the writerIndex by 3 in this buffer.

Usage

From source file:buildcraft.commander.ContainerZonePlan.java

License:Minecraft Mod Public

private void computeMap(int cx, int cz, int width, int height, int blocksPerPixel, EntityPlayer player) {
    final byte[] textureData = new byte[width * height];

    int startX = cx - width * blocksPerPixel / 2;
    int startZ = cz - height * blocksPerPixel / 2;

    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            int x = startX + i * blocksPerPixel;
            int z = startZ + j * blocksPerPixel;
            int ix = x - (map.chunkStartX << 4);
            int iz = z - (map.chunkStartZ << 4);

            if (ix >= 0 && iz >= 0 && ix < TileZonePlan.RESOLUTION && iz < TileZonePlan.RESOLUTION) {
                textureData[i + j * width] = map.colors[ix + iz * TileZonePlan.RESOLUTION];
            }//from  w  ww  . j  ava 2  s.  c o  m
        }
    }

    BuildCraftCore.instance.sendToPlayer(player, new PacketCommand(this, "receiveImage", new CommandWriter() {
        public void write(ByteBuf data) {
            data.writeMedium(textureData.length);
            data.writeBytes(textureData);
        }
    }));
}

From source file:buildcraft.robotics.gui.ContainerZonePlan.java

License:Minecraft Mod Public

private void computeMap(int cx, int cz, int width, int height, float blocksPerPixel, EntityPlayer player) {
    final byte[] textureData = new byte[width * height];

    MapWorld w = BuildCraftRobotics.manager.getWorld(map.getWorldObj());
    int startX = Math.round(cx - width * blocksPerPixel / 2);
    int startZ = Math.round(cz - height * blocksPerPixel / 2);
    int mapStartX = map.chunkStartX << 4;
    int mapStartZ = map.chunkStartZ << 4;

    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            int x = Math.round(startX + i * blocksPerPixel);
            int z = Math.round(startZ + j * blocksPerPixel);
            int ix = x - mapStartX;
            int iz = z - mapStartZ;

            if (ix >= 0 && iz >= 0 && ix < TileZonePlan.RESOLUTION && iz < TileZonePlan.RESOLUTION) {
                textureData[i + j * width] = (byte) w.getColor(x, z);
            }/* w w  w.j  a va2s . c  o  m*/
        }
    }

    final int len = MAX_PACKET_LENGTH;

    for (int i = 0; i < textureData.length; i += len) {
        final int pos = i;
        BuildCraftCore.instance.sendToPlayer(player,
                new PacketCommand(this, "receiveImage", new CommandWriter() {
                    public void write(ByteBuf data) {
                        data.writeMedium(textureData.length);
                        data.writeMedium(pos);
                        data.writeBytes(textureData, pos, Math.min(textureData.length - pos, len));
                    }
                }));
    }
}

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();// w  w  w  .j  ava  2 s .  c o  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.digitalpetri.opcua.stack.core.channel.headers.SecureMessageHeader.java

License:Apache License

public static void encode(SecureMessageHeader header, ByteBuf buffer) throws UaException {
    buffer.writeMedium(MessageType.toMediumInt(header.getMessageType()));
    buffer.writeByte(header.getChunkType());
    buffer.writeInt((int) header.getMessageSize());
    buffer.writeInt((int) header.getSecureChannelId());
}

From source file:com.digitalpetri.opcua.stack.core.channel.messages.TcpMessageEncoder.java

License:Apache License

/**
 * Encode a simple UA TCP message./*w w w. j  av  a 2 s .  c  o m*/
 *
 * @param messageType    {@link MessageType#Hello}, {@link MessageType#Acknowledge}, or {@link MessageType#Error}.
 * @param messageEncoder a function that encodes the message payload.
 * @param buffer         the {@link ByteBuf} to encode into.
 */
private static ByteBuf encode(MessageType messageType, Consumer<ByteBuf> messageEncoder, ByteBuf buffer)
        throws UaException {
    buffer.writeMedium(MessageType.toMediumInt(messageType));
    buffer.writeByte('F');

    int lengthIndex = buffer.writerIndex();
    buffer.writeInt(0);

    int indexBefore = buffer.writerIndex();
    messageEncoder.accept(buffer);
    int indexAfter = buffer.writerIndex();
    int bytesWritten = indexAfter - indexBefore;

    buffer.writerIndex(lengthIndex);
    buffer.writeInt(8 + bytesWritten);
    buffer.writerIndex(indexAfter);

    return buffer;
}

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg;/*  ww  w . j a v a2  s  .  com*/
    System.out.println("channelRead:" + ByteBufUtil.hexDump((buf)));
    while (buf.readableBytes() > 0) {

        int payload = buf.readUnsignedMedium();
        int frameType = buf.readByte();
        Http2Flags flags = new Http2Flags(buf.readUnsignedByte());
        int streamId = readUnsignedInt(buf);
        ByteBuf payloadBuf = buf.readBytes(payload);
        ByteBuf copy = ctx.alloc().buffer();
        System.out.println("frame_type:" + frameType + "," + ByteBufUtil.hexDump((payloadBuf)));
        switch (frameType) {
        case Http2FrameTypes.SETTINGS:
            handleSettingFrame(ctx, flags);
            break;
        case Http2FrameTypes.WINDOW_UPDATE:
            handleWindowsUpdateFrame(ctx);
            break;
        case Http2FrameTypes.HEADERS:

            copy.writeMedium(payload);
            copy.writeByte(frameType);
            copy.writeByte(flags.value());
            copy.writeInt(streamId);
            copy.writeBytes(payloadBuf);
            forward(ctx, copy);
            break;
        case Http2FrameTypes.DATA:
            copy.writeMedium(payload);
            copy.writeByte(frameType);
            copy.writeByte(flags.value());
            copy.writeInt(streamId);
            copy.writeBytes(payloadBuf);
            forward(ctx, copy);
            break;
        default:
            break;

        }
    }

}

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java

License:Apache License

private void readFrame(final ChannelHandlerContext ctx, ByteBuf buf) {
    if (first) {//  ww w.  ja  va  2s .co  m
        try {
            readClientPrefaceString(buf);
        } catch (Http2Exception e) {
            e.printStackTrace();
        }
        first = false;
    }

    while (buf.readableBytes() > 0) {

        int payload = buf.readUnsignedMedium();
        int frameType = buf.readByte();
        Http2Flags flags = new Http2Flags(buf.readUnsignedByte());
        int streamId = readUnsignedInt(buf);
        ByteBuf payloadBuf = buf.readBytes(payload);
        ByteBuf copy = ctx.alloc().buffer();
        switch (frameType) {
        case Http2FrameTypes.SETTINGS:
            handleSettingFrame(ctx, flags);
            break;
        case Http2FrameTypes.WINDOW_UPDATE:
            handleWindowsUpdateFrame(ctx);
            break;
        case Http2FrameTypes.HEADERS:

            copy.writeMedium(payload);
            copy.writeByte(frameType);
            copy.writeByte(flags.value());
            copy.writeInt(streamId);
            copy.writeBytes(payloadBuf);
            handleHeaderFrame(ctx, copy, streamId);
            break;
        case Http2FrameTypes.DATA:
            copy.writeMedium(payload);
            copy.writeByte(frameType);
            copy.writeByte(flags.value());
            copy.writeInt(streamId);
            copy.writeBytes(payloadBuf);
            handleDataFrame(ctx, copy, streamId);
            break;
        default:
            break;

        }
    }
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static void putLengthCodedLong(ByteBuf cb, long length) {
    if (length <= LEN_CODED_8_BITS) {
        cb.writeByte((int) length); // length is 1 byte
    } else if (length <= UNSIGNED_SHORT_MAX) {
        cb.writeByte(LEN_CODED_16_BITS); // length is 2 bytes
        cb.writeShort((int) length);
    } else if (length <= UNSIGNED_MEDIUM_MAX) {
        cb.writeByte(LEN_CODED_24_BITS); // length is 3 bytes
        cb.writeMedium((int) length);
    } else {/*from   w w w.j a v a2  s . co m*/
        cb.writeByte(LEN_CODED_64_BITS); // length is 8 bytes
        cb.writeLong(length);
    }
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPAuthenticateV10MessageMessage.java

License:Open Source License

public static void write(ByteBuf out, String userName, String userPassword, String salt, Charset charset,
        int mysqlCharsetID, int capabilitiesFlag) {
    ByteBuf leBuf = out.order(ByteOrder.LITTLE_ENDIAN);

    int payloadSizeIndex = leBuf.writerIndex();
    leBuf.writeMedium(0);
    leBuf.writeByte(1);/*ww  w .  ja v  a2 s .  co  m*/
    int payloadStartIndex = leBuf.writerIndex();
    leBuf.writeInt(capabilitiesFlag);
    leBuf.writeInt(MAX_PACKET_SIZE);
    //      leBuf.writeByte(serverGreeting.getServerCharsetId());
    leBuf.writeByte(mysqlCharsetID);
    leBuf.writeZero(23);
    leBuf.writeBytes(userName.getBytes(charset));
    leBuf.writeZero(1);

    if ((capabilitiesFlag & ClientCapabilities.CLIENT_SECURE_CONNECTION) > 0) {

        byte[] securePassword = computeSecurePassword(userPassword, salt);
        leBuf.writeByte(securePassword.length);
        leBuf.writeBytes(securePassword);
    } else {
        leBuf.writeBytes(userPassword.getBytes(charset));
        leBuf.writeZero(1);
    }

    leBuf.setMedium(payloadSizeIndex, leBuf.writerIndex() - payloadStartIndex);
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPServerGreetingRequestMessage.java

License:Open Source License

public static void write(ChannelHandlerContext ctx, int connectionId, String salt, int serverCapabilities,
        String serverVersion, byte serverCharSet, String pluginData) {
    ByteBuf out = ctx.channel().alloc().heapBuffer(50).order(ByteOrder.LITTLE_ENDIAN);

    String scrambleBuffer1st = salt.substring(0, 8);
    String scrambleBuffer2nd = salt.substring(8) + '\0';
    Integer scrambleBufferSize = scrambleBuffer1st.length() + scrambleBuffer2nd.length();

    ByteBuf serverCapabilitiesBuf = ctx.channel().alloc().heapBuffer(4).order(ByteOrder.LITTLE_ENDIAN);
    try {/*from   w  ww. j av  a  2s .  c  o  m*/
        serverCapabilitiesBuf.writeInt(serverCapabilities);
        out.writeMedium(0);
        out.writeByte(0);
        out.writeByte(MYSQL_PROTOCOL_VERSION);

        out.writeBytes(serverVersion.getBytes());
        out.writeZero(1);
        out.writeInt(connectionId);
        out.writeBytes(scrambleBuffer1st.getBytes()); // Salt
        out.writeZero(1);
        out.writeByte(serverCapabilitiesBuf.getByte(0));
        out.writeByte(serverCapabilitiesBuf.getByte(1));

        out.writeByte(serverCharSet);
        out.writeShort(MyProtocolDefs.SERVER_STATUS_AUTOCOMMIT);
        out.writeByte(serverCapabilitiesBuf.getByte(2));
        out.writeByte(serverCapabilitiesBuf.getByte(3));
        out.writeByte(scrambleBufferSize.byteValue());
        out.writeZero(10); // write 10 unused bytes
        out.writeBytes(scrambleBuffer2nd.getBytes()); // Salt

        out.writeBytes(pluginData.getBytes()); // payload
        out.writeZero(1);

        out.setMedium(0, out.writerIndex() - 4);

        ctx.channel().writeAndFlush(out);
    } finally {
        serverCapabilitiesBuf.release();
    }
}