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.addthis.hydra.task.source.SimpleMark.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] retBytes = null;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    try {/*w w w.j a  v  a  2 s. co  m*/
        byte[] valBytes = val.getBytes();
        Varint.writeUnsignedVarInt(valBytes.length, buffer);
        buffer.writeBytes(valBytes);
        Varint.writeUnsignedVarLong(index, buffer);
        buffer.writeByte(end ? 1 : 0);
        retBytes = new byte[buffer.readableBytes()];
        buffer.readBytes(retBytes);
    } finally {
        buffer.release();
    }
    return retBytes;
}

From source file:com.addthis.meshy.service.stream.StreamTarget.java

License:Apache License

private int doSendMore(SendWatcher sender) {
    if (log.isTraceEnabled()) {
        log.trace("{} sendMore for {} rem={} closed={} streamComplete={} fileIn={}", this, sender, sendRemain,
                closed, streamComplete, fileIn);
    }//w w  w . jav a  2  s .co  m
    try {
        if (maybeDropSend()) {
            if (StreamService.LOG_DROP_MORE || log.isDebugEnabled()) {
                log.debug("{} sendMore drop request sr={}, cl={}, co={}, fi={}", this, sendRemain, closed,
                        streamComplete, fileIn);
            }
            return -1;
        }
        byte[] next = fileIn.nextBytes(StreamService.READ_WAIT);
        if (next != null) {
            if (log.isTraceEnabled()) {
                log.trace("{} send add read={}", this, next.length);
            }
            StreamService.readBytes.addAndGet(next.length);
            ByteBuf buf = getSendBuffer(next.length + StreamService.STREAM_BYTE_OVERHEAD);
            buf.writeByte(StreamService.MODE_MORE);
            buf.writeBytes(next);
            int bytesSent = send(buf, sender);
            sendRemain.addAndGet(-bytesSent);
            sentBytes += bytesSent;
            if (log.isTraceEnabled()) {
                log.trace("{} read read={} sendRemain={}", this, next.length, sendRemain);
            }
            return bytesSent;
        } else if (fileIn.isEOF()) {
            if (log.isTraceEnabled()) {
                log.trace("{} read close on null. sendRemain={}", this, sendRemain);
            }
            modeClose();
            return -1;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("{} read timeout. sendRemain={}", this, sendRemain);
            }
            return 0;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return -1;
    }
}

From source file:com.addthis.meshy.SourceHandler.java

License:Apache License

private static ByteBuf allocateSendBuffer(ByteBufAllocator alloc, int type, int session, byte[] data) {
    ByteBuf sendBuffer = alloc.buffer(MESHY_BYTE_OVERHEAD + data.length);
    sendBuffer.writeInt(type);//from  ww w.j a va  2s  .co m
    sendBuffer.writeInt(session);
    sendBuffer.writeInt(data.length);
    sendBuffer.writeBytes(data);
    return sendBuffer;
}

From source file:com.alibaba.rocketmq.remoting.netty.NettyEncoder.java

License:Apache License

@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out) throws Exception {
    try {/*from ww w .j a va 2 s .c o m*/
        ByteBuffer header = remotingCommand.encodeHeader();
        out.writeBytes(header);
        byte[] body = remotingCommand.getBody();
        if (body != null) {
            out.writeBytes(body);
        }
    } catch (Exception e) {
        log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
        if (remotingCommand != null) {
            log.error(remotingCommand.toString());
        }
        RemotingUtil.closeChannel(ctx.channel());
    }
}

From source file:com.allanbank.mongodb.netty.ByteToMessageDecoderTest.java

License:Apache License

/**
 * Test method for/* ww  w.j  av  a2  s.co  m*/
 * {@link ByteToMessageDecoder#extractFrame(ChannelHandlerContext, ByteBuf, int, int)}
 * .
 */
@Test
public void testExtractFrameChannelHandlerContextByteBufIntInt() {
    final ByteBuf buffer = ourAllocator.buffer();

    buffer.writeBytes(new byte[1000]);

    final ChannelHandlerContext mockContext = createMock(ChannelHandlerContext.class);

    replay(mockContext);

    final ByteToMessageDecoder decoder = new ByteToMessageDecoder(new StringDecoderCache());

    final ByteBuf result = decoder.extractFrame(mockContext, buffer, 100, 200);
    assertThat(result, instanceOf(SlicedByteBuf.class));

    assertThat(buffer.getByte(100), is((byte) 0));
    result.setByte(0, 1);
    assertThat(buffer.getByte(100), is((byte) 1));

    verify(mockContext);
}

From source file:com.almuradev.almurasdk.server.network.play.S00PacketPermissions.java

License:MIT License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeBytes(container.getBytes());
}

From source file:com.antsdb.saltedfish.server.mysql.Helper.java

License:Open Source License

static void writeResonpse(ChannelHandlerContext ctx, MysqlServerHandler handler, Object result, boolean text) {
    ByteBuf bufferArray = ctx.alloc().buffer();

    if (result == null) {
        PacketEncoder.writePacket(bufferArray, (byte) 1, () -> handler.packetEncoder.writeOKBody(bufferArray, 0,
                handler.session.getLastInsertId(), null, handler.session));
    } else if (result instanceof Cursor) {
        Helper.writeCursor(bufferArray, handler, (Cursor) result, text);
    } else if (result instanceof Integer) {
        Integer count = (Integer) result;
        PacketEncoder.writePacket(bufferArray, handler.getNextPacketSequence(), () -> handler.packetEncoder
                .writeOKBody(bufferArray, count, handler.session.getLastInsertId(), null, handler.session));
    } else {//  w  w  w. j a va 2s. c o m
        bufferArray.writeBytes(PacketEncoder.OK_PACKET);
    }
    // write preparedOk packet
    ctx.writeAndFlush(bufferArray);
}

From source file:com.antsdb.saltedfish.server.mysql.MysqlServerHandler.java

License:Open Source License

private void auth(ChannelHandlerContext ctx, AuthPacket request) {
    // logging in

    this.session = this.fish.getOrca().createSession(request.user);
    this.session.setCurrentNamespace(request.dbname);
    if (_log.isTraceEnabled()) {
        _log.debug("Connection user:" + request.user);
        _log.debug("Connection default schema:" + request.dbname);
    }//  w  w w .  jav  a2s .c  om
    preparedStmtHandler = new PreparedStmtHandler(this);
    queryHandler = new QueryHandler(this);

    // if client send client_secure_connection, send back auth ok to client

    if ((request.clientParam & CLIENT_SECURE_CONNECTION) != 0) {
        ByteBuf buf = ctx.alloc().buffer();
        buf.writeBytes(PacketEncoder.AUTH_OK_PACKET);
        ctx.writeAndFlush(buf);
    }
}

From source file:com.antsdb.saltedfish.server.mysql.MysqlServerHandler.java

License:Open Source License

private void init(ChannelHandlerContext ctx, InitPacket request) {
    VdmContext vdm = new VdmContext(session, 0);
    new Use(request.database).run(vdm, null);
    ByteBuf bufferArray = ctx.alloc().buffer();
    bufferArray.writeBytes(PacketEncoder.OK_PACKET);
    ctx.writeAndFlush(bufferArray);/*from  ww w.  j a  v  a  2 s .  c  o  m*/
}

From source file:com.antsdb.saltedfish.server.mysql.MysqlServerHandler.java

License:Open Source License

private void ping(ChannelHandlerContext ctx) {
    ByteBuf bufferArray = ctx.alloc().buffer();
    bufferArray.writeBytes(PacketEncoder.OK_PACKET);
    ctx.writeAndFlush(bufferArray);//from ww w .  j av  a  2s . c o  m
}