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

The following are Jave code examples for showing how to use writeZero() 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: fastdfs-spring-boot   File: FastdfsUtils.java   Source Code and License Vote up 5 votes
/**
 * 给 ByteBuf 写入定长字符串
 * <p>
 * 若字符串长度大于定长,则截取定长字节;若小于定长,则补零
 *
 * @param buf
 * @param content
 * @param length
 */
public static void writeFixLength(ByteBuf buf, String content, int length) {
    byte[] bytes = content.getBytes(CharsetUtil.UTF_8);
    int blen = bytes.length;
    int wlen = blen > length ? length : blen;
    buf.writeBytes(bytes, 0, wlen);
    if (wlen < length) {
        buf.writeZero(length - wlen);
    }
}
 
Example 2
Project: drift   File: HeaderMessageEncoding.java   Source Code and License Vote up 5 votes
private static ByteBuf getPadding(int headerSize)
{
    int paddingSize = 4 - headerSize % 4;
    ByteBuf padding = Unpooled.buffer(paddingSize);
    padding.writeZero(paddingSize);
    return padding;
}
 
Example 3
Project: neoscada   File: APDUEncoder.java   Source Code and License Vote up 5 votes
private void handleUFormat ( final UnnumberedControl msg, final ByteBuf out )
{
    out.ensureWritable ( 6 );
    out.writeByte ( Constants.START_BYTE );
    out.writeByte ( 4 );
    out.writeByte ( msg.getFunction ().getNumericValue () | 0x03 /* bits 1 and 2*/);
    out.writeZero ( 3 );
}
 
Example 4
Project: azeroth   File: FastdfsUtils.java   Source Code and License Vote up 5 votes
/**
 * 给 ByteBuf 写入定长字符串
 * <p>
 * 若字符串长度大于定长,则截取定长字节;若小于定长,则补零
 *
 * @param buf
 * @param content
 * @param length
 */
public static void writeFixLength(ByteBuf buf, String content, int length) {
    byte[] bytes = content.getBytes(CharsetUtil.UTF_8);
    int blen = bytes.length;
    int wlen = blen > length ? length : blen;
    buf.writeBytes(bytes, 0, wlen);
    if (wlen < length) {
        buf.writeZero(length - wlen);
    }
}
 
Example 5
Project: minebox   File: HandshakePhase.java   Source Code and License Vote up 4 votes
private String processOption(ChannelHandlerContext ctx, ByteBuf in) throws IOException {
     switch (currentOption) {
         case Protocol.NBD_OPT_EXPORT_NAME:
             CharSequence exportName = in.readCharSequence((int) currentOptionLen, Charset.forName("UTF-8"));

             if (!exportProvider.supportsClientFlags(clientFlags)) {
                 LOGGER.error("client flags {} not supported", clientFlags);
                 ctx.channel().close();
                 break;
             }

             long exportSize = 0;
             if ((exportSize = exportProvider.open(exportName)) < 0) {
                 // ep us unwilling to export this name
                 ctx.channel().close();
                 break;
             }

/* build response */
             ByteBuf resp = ctx.alloc().buffer(256);
             resp.writeLong(exportSize);
             short transmissionFlags =
                     Protocol.NBD_FLAG_HAS_FLAGS
                             | Protocol.NBD_FLAG_SEND_FLUSH
                             | Protocol.NBD_FLAG_SEND_TRIM;
             resp.writeShort(transmissionFlags);

             if ((clientFlags & Protocol.NBD_FLAG_NO_ZEROES) == 0) {
                 resp.writeZero(124);
             }
             ctx.writeAndFlush(resp);

             //FIXME: transfer any remaining bytes into the transmission phase!
         /* The NBD protocol has two phases: the handshake (HS_) and the transmission (TM_) */
             // Handshake complete, switch to transmission phase
             ctx.pipeline().addLast("transmission", new TransmissionPhase(config, exportProvider));
             ctx.pipeline().remove(this);
             return exportName.toString();

         default:
             sendHandshakeOptionHagglingReply(ctx, currentOption, Protocol.NBD_REP_ERR_UNSUP, null);
     }
     return null;
 }
 
Example 6
Project: tools   File: HandshakePhase.java   Source Code and License Vote up 4 votes
private String processOption(ChannelHandlerContext ctx, ByteBuf in) throws IOException {
     switch (currentOption) {
         case Protocol.NBD_OPT_EXPORT_NAME:
             CharSequence exportName = in.readCharSequence((int) currentOptionLen, Charset.forName("UTF-8"));

             if (!exportProvider.supportsClientFlags(clientFlags)) {
                 LOGGER.error("client flags {} not supported", clientFlags);
                 ctx.channel().close();
                 break;
             }

             long exportSize = 0;
             if ((exportSize = exportProvider.open(exportName)) < 0) {
                 // ep us unwilling to export this name
                 ctx.channel().close();
                 break;
             }

/* build response */
             ByteBuf resp = ctx.alloc().buffer(256);
             resp.writeLong(exportSize);
             short transmissionFlags =
                     Protocol.NBD_FLAG_HAS_FLAGS
                             | Protocol.NBD_FLAG_SEND_FLUSH
                             | Protocol.NBD_FLAG_SEND_TRIM;
             resp.writeShort(transmissionFlags);

             if ((clientFlags & Protocol.NBD_FLAG_NO_ZEROES) == 0) {
                 resp.writeZero(124);
             }
             ctx.writeAndFlush(resp);

             //FIXME: transfer any remaining bytes into the transmission phase!
         /* The NBD protocol has two phases: the handshake (HS_) and the transmission (TM_) */
             // Handshake complete, switch to transmission phase
             ctx.pipeline().addLast("transmission", new TransmissionPhase(config, exportProvider));
             ctx.pipeline().remove(this);
             return exportName.toString();

         default:
             sendHandshakeOptionHagglingReply(ctx, currentOption, Protocol.NBD_REP_ERR_UNSUP, null);
     }
     return null;
 }