本文整理汇总了Java中io.netty.buffer.ByteBuf类的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf类的具体用法?Java ByteBuf怎么用?Java ByteBuf使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ByteBuf类属于io.netty.buffer包,在下文中一共展示了ByteBuf类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testDeserialize
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@Test
public void testDeserialize() throws Exception {
ByteBuf buf = Unpooled.buffer(16);
buf.writeInt(0);
buf.writeInt(1);
buf.writeLong(2);
buf.writeLong(3);
buf.writeInt(4);
buf.writeInt(3);
buf.writeInt(-1);
buf.writeInt(-2);
buf.writeInt(-3);
serverDenseIntRow.deserialize(buf);
assertEquals(serverDenseIntRow.getRowId(), 0);
assertEquals(serverDenseIntRow.getClock(), 1);
assertEquals(serverDenseIntRow.getStartCol(), 2);
assertEquals(serverDenseIntRow.getEndCol(), 3);
assertEquals(serverDenseIntRow.getRowVersion(), 4);
assertEquals(serverDenseIntRow.getData().get(0), -1, 0.0);
assertEquals(serverDenseIntRow.getData().get(1), -2, 0.0);
assertEquals(serverDenseIntRow.getData().get(2), -3, 0.0);
}
示例2: startBodyParse
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@Timed
void startBodyParse(ByteBuf in) throws UnknownFetchItemTypeException, IOException {
skipControlCharacters(in);
char c = ((char) in.readUnsignedByte());
//String bodySection = ""; At some point we will need to actually store the body section that is being parsed below
if (c != '[') {
// This is effectively BODYSTRUCTURE which is not yet supported
lineParser.parse(in);
checkpoint(State.RESET);
throw new UnknownFetchItemTypeException("BODYSTRUCTURE");
} else {
c = ((char) in.readUnsignedByte());
while (c != ']') { // Skip characters within "[]"
c = ((char) in.readUnsignedByte());
}
}
checkpoint(State.FETCH_BODY);
}
示例3: encode
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
/**
* <b>一定要 {@link ByteBuf#release()}
*/
public static ByteBuf encode(RemotingCommand cmd) {
int reqId = cmd.getOpaque();
byte[] body = cmd.getBody();
HashMap<String, String> msgs = cmd.getExtFields();
byte[] append = JsonUtil.toBytes(msgs);
int initialCapacity = 4 + 4 // total size+reqId
+ 4 + body.length // body
+ 4 + append.length;// apend
ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(initialCapacity);
buf.writeInt(initialCapacity);
buf.writeInt(reqId);
buf.writeInt(body.length);
buf.writeBytes(body);
buf.writeInt(append.length);
buf.writeBytes(append);
return buf;
}
示例4: fromBytes
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void fromBytes(ByteBuf buf) {
short len = buf.readShort();
byte[] compressedBody = new byte[len];
for (short i = 0; i < len; i++) {
compressedBody[i] = buf.readByte();
}
try {
ObjectInputStream obj = new ObjectInputStream(new GZIPInputStream(new ByteArrayInputStream(compressedBody)));
values = (Map<String, Object>) obj.readObject();
obj.close();
}
catch (Exception e) {
Throwables.propagate(e);
}
}
示例5: getJson
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
public static String getJson(Object msg) {
String json;
try {
ByteBuf buf = (ByteBuf) msg;
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
json = new String(bytes);
} finally {
ReferenceCountUtil.release(msg);
}
return json;
}
示例6: setData
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
public void setData(ByteBuf buff) {
if (data == null) {
data = getByteBuf();
}
data.writeBytes(buff);
}
示例7: encode
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@Override
public void encode ( final ProtocolOptions options, final ByteBuf out )
{
EncodeHelper.encodeHeader ( this, options, this.values.size (), this.header, out );
this.startAddress.encode ( options, out );
for ( final Value<Float> value : this.values )
{
TypeHelper.encodeFloatValue ( options, out, value, false );
}
}
示例8: toBytes
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@Override
public void toBytes(ByteBuf buf)
{
buf.writeByte(windowId);
buf.writeByte(tank);
NBTTagCompound fluidNbt = fluid == null ? null : fluid.writeToNBT(new NBTTagCompound());
ByteBufUtils.writeTag(buf, fluidNbt);
}
示例9: decode
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 8) {
return;
}
short dataLen = in.getShort(in.readerIndex() + 6);
if (in.readableBytes() < dataLen) {
return;
}
ByteBuf msg = in.readRetainedSlice(8 + dataLen);
out.add(msg);
}
示例10: add
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
public RtMessageBuilder add(RtTag tag, RtMessage msg) {
checkNotNull(tag, "tag must be non-null");
checkNotNull(msg, "msg must be non-null");
ByteBuf encoded = RtWire.toWire(msg, allocator);
return add(tag, encoded);
}
示例11: cacheRemainByte
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
/**
* 缓存可读数据中的非完整帧,拼接为完整帧后,执行正常操作
*
* @param msg 可读数据
* @return 缓存为完整帧
*/
private boolean cacheRemainByte(ByteBuf msg) {
while (msg.isReadable()) {
bytes[index] = msg.readByte();
index++;
if (index >= 13) {
index = 0;
return true;
}
}
return false;
}
示例12: encode
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
private static void encode(String portal, int rowCount, ByteBuf out) {
int pos = out.writerIndex();
out.writeByte(EXECUTE);
out.writeInt(0);
if (portal != null) {
out.writeCharSequence(portal, StandardCharsets.UTF_8);
}
out.writeByte(0);
out.writeInt(rowCount); // Zero denotes "no limit" maybe for ReadStream<Row>
out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
示例13: writeValue
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
public static void writeValue(ByteBuffer bytes, ByteBuf cb)
{
if (bytes == null)
{
cb.writeInt(-1);
return;
}
int remaining = bytes.remaining();
cb.writeInt(remaining);
if (remaining > 0)
cb.writeBytes(bytes.duplicate());
}
示例14: testBuffers
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
private void testBuffers(ByteBuf[] buffers, SerializedField.Builder[] fields) {
int i = 0;
for (SerializedField.Builder field : fields) {
i += fieldBuffersCount(field, buffers, i, buffers.length);
}
while (i < buffers.length && buffers[i].readableBytes() == 0) {
++ i;
}
assertEquals(buffers.length, i);
for (ByteBuf byteBuf : buffers) {
byteBuf.release();
}
}
示例15: channelRead0
import io.netty.buffer.ByteBuf; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, FrameMajor msg) throws Exception {
FrameMajorHeader head = msg.getHead();
ByteBuf byteBuf = msg.getData();
while (byteBuf.readableBytes() >= FrameSetting.SUB_FRAME_HEAD_LENGTH) {
int subMsgId = byteBuf.readByte() & 0xFF;
byte[] data = new byte[byteBuf.readShort()];
byteBuf.readBytes(data);
server.decodeAndHuntMessage(head, subMsgId, data, ctx.channel());
}
}