io.netty.buffer.ByteBuf.writeShort()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(317)

本文整理了Java中io.netty.buffer.ByteBuf.writeShort()方法的一些代码示例,展示了ByteBuf.writeShort()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.writeShort()方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:writeShort

ByteBuf.writeShort介绍

[英]Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2in this buffer. The 16 high-order bits of the specified value are ignored.
[中]在当前writerIndex处设置指定的16位短整数,并在此缓冲区中将writerIndex增加2。指定值的16个高位将被忽略。

代码示例

代码示例来源:origin: neo4j/neo4j

private void startNewChunk()
{
  currentChunkStartIndex = buffer.writerIndex();
  // write empty chunk header
  buffer.writeShort( 0 );
  chunkOpen = true;
}

代码示例来源:origin: wildfly/wildfly

@Override
public void write(final ByteBuf buffer) {
  buffer.writeByte(DataConstants.CHAR);
  buffer.writeShort((short) val);
}

代码示例来源:origin: mpusher/mpush

public void encodeBytes(ByteBuf body, byte[] field) {
  if (field == null || field.length == 0) {
    body.writeShort(0);
  } else if (field.length < Short.MAX_VALUE) {
    body.writeShort(field.length).writeBytes(field);
  } else {
    body.writeShort(Short.MAX_VALUE).writeInt(field.length - Short.MAX_VALUE).writeBytes(field);
  }
}

代码示例来源:origin: traccar/traccar

private ByteBuf encodeContent(int type, ByteBuf content) {
  ByteBuf buf = Unpooled.buffer();
  buf.writeShort(1 + content.readableBytes());
  buf.writeByte(100 + type);
  buf.writeBytes(content);
  buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.nioBuffer(2, buf.writerIndex() - 2)));
  return buf;
}

代码示例来源:origin: netty/netty

/**
   * Writes any remaining bits to the output {@link ByteBuf},
   * zero padding to a whole byte as required.
   */
  void flush(ByteBuf out) {
    final int bitCount = this.bitCount;

    if (bitCount > 0) {
      final long bitBuffer = this.bitBuffer;
      final int shiftToRight = 64 - bitCount;

      if (bitCount <= 8) {
        out.writeByte((int) (bitBuffer >>> shiftToRight << 8 - bitCount));
      } else if (bitCount <= 16) {
        out.writeShort((int) (bitBuffer >>> shiftToRight << 16 - bitCount));
      } else if (bitCount <= 24) {
        out.writeMedium((int) (bitBuffer >>> shiftToRight << 24 - bitCount));
      } else {
        out.writeInt((int) (bitBuffer >>> shiftToRight << 32 - bitCount));
      }
    }
  }
}

代码示例来源:origin: traccar/traccar

private static void sendResponse(Channel channel, ByteBuf buf) {
  if (channel != null) {
    ByteBuf response = Unpooled.buffer(4);
    response.writeByte('*');
    response.writeShort(buf.getUnsignedShort(buf.writerIndex() - 2));
    response.writeByte(buf.getUnsignedByte(buf.writerIndex() - 3));
    channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
  }
}

代码示例来源:origin: traccar/traccar

private void sendPhotoRequest(Channel channel, int pictureId) {
  ByteBuf photo = photos.get(pictureId);
  ByteBuf content = Unpooled.buffer();
  content.writeInt(pictureId);
  content.writeInt(photo.writerIndex());
  content.writeShort(Math.min(photo.writableBytes(), 1024));
  sendResponse(channel, false, MSG_X1_PHOTO_DATA, 0, content);
}

代码示例来源:origin: wildfly/wildfly

@Override
  protected void encode(ChannelHandlerContext ctx, Socks4CommandResponse msg, ByteBuf out) throws Exception {
    out.writeByte(0);
    out.writeByte(msg.status().byteValue());
    out.writeShort(msg.dstPort());
    out.writeBytes(msg.dstAddr() == null? IPv4_HOSTNAME_ZEROED
                      : NetUtil.createByteArrayFromIpAddressString(msg.dstAddr()));
  }
}

代码示例来源:origin: org.opendaylight.openflowjava/openflow-protocol-impl

@Override
public void serialize(Instruction instruction, ByteBuf outBuffer) {
  outBuffer.writeShort(getType());
  outBuffer.writeShort(InstructionConstants.STANDARD_INSTRUCTION_LENGTH);
  outBuffer.writeByte(((GotoTableCase) instruction.getInstructionChoice())
      .getGotoTable().getTableId());
  outBuffer.writeZero(InstructionConstants.PADDING_IN_GOTO_TABLE);
}

代码示例来源:origin: org.opendaylight.openflowjava/openflow-protocol-impl

private static void serializeQueueBody(final MultipartRequestBody multipartRequestBody, final ByteBuf output) {
  MultipartRequestQueueCase queueCase = (MultipartRequestQueueCase) multipartRequestBody;
  MultipartRequestQueue queue = queueCase.getMultipartRequestQueue();
  output.writeShort(queue.getPortNo().intValue());
  output.writeZero(PADING_IN_QUEUE_BODY);
  output.writeInt(queue.getQueueId().intValue());
}

代码示例来源:origin: atomix/atomix

/**
 * Writes the protocol version to the given context.
 *
 * @param context the context to which to write the version
 * @param version the version to write
 */
void writeProtocolVersion(ChannelHandlerContext context, ProtocolVersion version) {
 ByteBuf buffer = context.alloc().buffer(6);
 buffer.writeInt(preamble);
 buffer.writeShort(version.version());
 context.writeAndFlush(buffer);
}

代码示例来源:origin: org.opendaylight.bgpcep/pcep-spi

public static void formatTlv(final int type,final ByteBuf body, final ByteBuf out) {
  out.writeShort(type);
  out.writeShort(body.writerIndex());
  out.writeBytes(body);
  out.writeZero(getPadding(HEADER_SIZE + body.writerIndex(), PADDED_TO));
}

代码示例来源:origin: org.apache.cassandra/cassandra-all

public static void writeString(String str, ByteBuf cb)
{
  int writerIndex = cb.writerIndex();
  cb.writeShort(0);
  int lengthBytes = ByteBufUtil.writeUtf8(cb, str);
  cb.setShort(writerIndex, lengthBytes);
}

代码示例来源:origin: traccar/traccar

private ByteBuf encodeContent(int type, ByteBuf content) {
  ByteBuf buf = Unpooled.buffer();
  buf.writeByte('S');
  buf.writeByte('S');
  buf.writeShort(2 + 2 + 2 + content.readableBytes() + 4 + 2 + 2);
  buf.writeShort(type);
  buf.writeBytes(content);
  buf.writeInt(0x0B);
  buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.nioBuffer()));
  buf.writeByte('\r');
  buf.writeByte('\n');
  return buf;
}

代码示例来源:origin: redisson/redisson

/**
   * Writes any remaining bits to the output {@link ByteBuf},
   * zero padding to a whole byte as required.
   */
  void flush(ByteBuf out) {
    final int bitCount = this.bitCount;

    if (bitCount > 0) {
      final long bitBuffer = this.bitBuffer;
      final int shiftToRight = 64 - bitCount;

      if (bitCount <= 8) {
        out.writeByte((int) (bitBuffer >>> shiftToRight << 8 - bitCount));
      } else if (bitCount <= 16) {
        out.writeShort((int) (bitBuffer >>> shiftToRight << 16 - bitCount));
      } else if (bitCount <= 24) {
        out.writeMedium((int) (bitBuffer >>> shiftToRight << 24 - bitCount));
      } else {
        out.writeInt((int) (bitBuffer >>> shiftToRight << 32 - bitCount));
      }
    }
  }
}

代码示例来源:origin: wildfly/wildfly

@Override
public void write(final ByteBuf buffer) {
  buffer.writeByte(DataConstants.SHORT);
  buffer.writeShort(val);
}

代码示例来源:origin: traccar/traccar

@Override
protected Object decode(
    Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
  ByteBuf buf = (ByteBuf) msg;
  if (buf.getUnsignedByte(buf.readerIndex()) == 0xF8) {
    if (channel != null) {
      ByteBuf response = Unpooled.buffer();
      response.writeByte(0xF8);
      response.writeByte(DATA_GPS);
      response.writeByte(0xFE);
      response.writeShort(buf.getShort(response.writerIndex() - 1 - 2));
      response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer(1, 4)));
      response.writeByte(0xF8);
      channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
    return decodeBinary(channel, remoteAddress, buf);
  } else {
    if (channel != null) {
      channel.writeAndFlush(new NetworkMessage(Unpooled.copiedBuffer(String.format("*TS01,ACK:%04X#",
          Checksum.crc16(Checksum.CRC16_XMODEM, buf.nioBuffer(1, buf.writerIndex() - 2))),
          StandardCharsets.US_ASCII), remoteAddress));
    }
    return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII));
  }
}

代码示例来源:origin: qunarcorp/qmq

@Override
protected void encode(ChannelHandlerContext ctx, Datagram msg, ByteBuf out) throws Exception {
  int start = out.writerIndex();
  int headerStart = start + RemotingHeader.LENGTH_FIELD;
  out.ensureWritable(RemotingHeader.LENGTH_FIELD);
  out.writerIndex(headerStart);
  final RemotingHeader header = msg.getHeader();
  encodeHeader(header, out);
  int headerSize = out.writerIndex() - headerStart;
  msg.writeBody(out);
  int end = out.writerIndex();
  int total = end - start - RemotingHeader.TOTAL_SIZE_LEN;
  out.writerIndex(start);
  out.writeInt(total);
  out.writeShort((short) headerSize);
  out.writerIndex(end);
}

代码示例来源:origin: org.opendaylight.openflowplugin/openflowjava-extension-nicira

static void serializeLearnHeader(final ByteBuf outBuffer, ActionLearn action) {
  outBuffer.writeShort(action.getNxActionLearn().getIdleTimeout().shortValue());
  outBuffer.writeShort(action.getNxActionLearn().getHardTimeout().shortValue());
  outBuffer.writeShort(action.getNxActionLearn().getPriority().shortValue());
  outBuffer.writeLong(action.getNxActionLearn().getCookie().longValue());
  outBuffer.writeShort(action.getNxActionLearn().getFlags().shortValue());
  outBuffer.writeByte(action.getNxActionLearn().getTableId().byteValue());
  outBuffer.writeZero(1);
  outBuffer.writeShort(action.getNxActionLearn().getFinIdleTimeout().shortValue());
  outBuffer.writeShort(action.getNxActionLearn().getFinHardTimeout().shortValue());
}

代码示例来源:origin: org.opendaylight.openflowplugin/openflowjava-extension-nicira

@Override
public void serialize(final Action input, final ByteBuf outBuffer) {
  ActionOutputReg action = ((ActionOutputReg) input.getActionChoice());
  serializeHeader(LENGTH, SUBTYPE, outBuffer);
  outBuffer.writeShort(action.getNxActionOutputReg().getNBits().shortValue());
  outBuffer.writeInt(action.getNxActionOutputReg().getSrc().intValue());
  outBuffer.writeShort(action.getNxActionOutputReg().getMaxLen().shortValue());
  outBuffer.writeZero(6);
}

相关文章

ByteBuf类方法