Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

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

License:Apache License

private void readFrame(final ChannelHandlerContext ctx, ByteBuf buf) {
    if (first) {/*www .ja  va2s  .  c  om*/
        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.newlandframework.rpc.serialize.hessian.HessianCodecUtil.java

License:Apache License

public void encode(final ByteBuf out, final Object message) throws IOException {
    try {/*from w w w.ja v a 2 s  .c o  m*/
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        HessianSerialize hessianSerialization = pool.borrow();
        hessianSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
        pool.restore(hessianSerialization);
    } finally {
        closer.close();
    }
}

From source file:com.newlandframework.rpc.serialize.kryo.KryoCodecUtil.java

License:Apache License

public void encode(final ByteBuf out, final Object message) throws IOException {
    try {//w w w .  j  a  va2  s  . c  om
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        KryoSerialize kryoSerialization = new KryoSerialize(pool);
        kryoSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
    } finally {
        closer.close();
    }
}

From source file:com.newlandframework.rpc.serialize.protostuff.ProtostuffCodecUtil.java

License:Apache License

public void encode(final ByteBuf out, final Object message) throws IOException {
    try {// w  w w . j av  a 2  s  .co m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        ProtostuffSerialize protostuffSerialization = pool.borrow();
        protostuffSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
        pool.restore(protostuffSerialization);
    } finally {
        closer.close();
    }
}

From source file:com.ning.http.client.providers.netty_4.BodyChunkedInput.java

License:Open Source License

@Override
public boolean readChunk(ByteBuf b) throws Exception {
    ByteBuffer buffer = peekNextChunk();
    if (buffer == null || buffer == EOF) {
        return false;
    }/*ww w  . java  2  s.  com*/
    nextChunk = null;

    b.writeBytes(buffer);
    return true;
}

From source file:com.noahkurrack.onenine.network.MessageSingleParticleEvent.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {

    buf.writeInt(particleName.length());
    buf.writeBytes(particleName.getBytes());
    buf.writeDouble(xCoord);// ww  w  . j ava 2s.  com
    buf.writeDouble(yCoord);
    buf.writeDouble(zCoord);
    buf.writeDouble(xVelocity);
    buf.writeDouble(yVelocity);
    buf.writeDouble(zVelocity);

}

From source file:com.ogarproject.ogar.server.net.packet.Packet.java

License:Open Source License

public static void writeUTF8(ByteBuf out, String s) {
    out.writeBytes(s.getBytes(Charsets.UTF_8));
    out.writeByte(0);
}

From source file:com.ogarproject.ogar.server.net.packet.universal.PacketOMPMessage.java

License:Open Source License

@Override
public void writeData(ByteBuf buf) {
    if (data.length > 32767) {
        throw new MalformedPacketException("Exceeded max OMP message size (" + data.length + " > 32767)");
    }//from  w  w  w.j  a va  2s .  com

    writeUTF8(buf, channel);
    buf.writeBytes(data);
}

From source file:com.openddal.server.mysql.MySQLAuthenticator.java

License:Apache License

@Override
public void onConnected(Channel channel) {
    ByteBuf out = channel.alloc().buffer();
    Handshake handshake = new Handshake();
    handshake.protocolVersion = MySQLProtocolServer.PROTOCOL_VERSION;
    handshake.serverVersion = MySQLProtocolServer.SERVER_VERSION;
    handshake.connectionId = connIdGenerator.incrementAndGet();
    handshake.challenge1 = getRandomString(8);
    handshake.capabilityFlags = Flags.CLIENT_BASIC_FLAGS;
    handshake.characterSet = MySQLCharsets.getIndex(MySQLProtocolServer.DEFAULT_CHARSET);
    handshake.statusFlags = Flags.SERVER_STATUS_AUTOCOMMIT;
    handshake.challenge2 = getRandomString(12);
    handshake.authPluginDataLength = 21;
    handshake.authPluginName = "mysql_native_password";
    // Remove some flags from the reply
    handshake.removeCapabilityFlag(Flags.CLIENT_COMPRESS);
    handshake.removeCapabilityFlag(Flags.CLIENT_IGNORE_SPACE);
    handshake.removeCapabilityFlag(Flags.CLIENT_LOCAL_FILES);
    handshake.removeCapabilityFlag(Flags.CLIENT_SSL);
    handshake.removeCapabilityFlag(Flags.CLIENT_TRANSACTIONS);
    handshake.removeCapabilityFlag(Flags.CLIENT_RESERVED);
    handshake.removeCapabilityFlag(Flags.CLIENT_REMEMBER_OPTIONS);

    // handshake = Handshake.loadFromPacket(packet);
    MySQLSession temp = new MySQLSession();
    temp.setHandshake(handshake);//from  w ww.j a  va 2s  .  c  o  m
    channel.attr(TMP_SESSION_KEY).set(temp);
    out.writeBytes(handshake.toPacket());
    channel.writeAndFlush(out);
}

From source file:com.openddal.server.mysql.MySQLAuthenticator.java

License:Apache License

/**
 * @param channel//from   w ww.ja va  2  s .  c  om
 * @param buf
 * @return
 */
private void success(Channel channel) {
    ByteBuf out = channel.alloc().buffer();
    OK ok = new OK();
    ok.sequenceId = 2;
    ok.setStatusFlag(Flags.SERVER_STATUS_AUTOCOMMIT);
    out.writeBytes(ok.toPacket());
    channel.writeAndFlush(out);
}