本文整理匯總了Java中io.netty.buffer.ByteBufAllocator類的典型用法代碼示例。如果您正苦於以下問題:Java ByteBufAllocator類的具體用法?Java ByteBufAllocator怎麽用?Java ByteBufAllocator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ByteBufAllocator類屬於io.netty.buffer包,在下文中一共展示了ByteBufAllocator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addValueOverloads
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Test
public void addValueOverloads() {
byte[] value1 = new byte[64];
Arrays.fill(value1, (byte) 'b');
ByteBuf value2Buf = ByteBufAllocator.DEFAULT.buffer(14);
byte[] value2 = "This is a test".getBytes();
value2Buf.writeBytes(value2);
RtMessage value3Msg = RtMessage.builder().add(RtTag.PAD, new byte[12]).build();
ByteBuf value3Buf = RtWire.toWire(value3Msg);
byte[] value3 = new byte[value3Buf.readableBytes()];
value3Buf.readBytes(value3);
RtMessage msg = RtMessage.builder()
.add(RtTag.INDX, value1)
.add(RtTag.MAXT, value2Buf)
.add(RtTag.NONC, value3Msg)
.build();
assertArrayEquals(msg.get(RtTag.INDX), value1);
assertArrayEquals(msg.get(RtTag.MAXT), value2);
assertArrayEquals(msg.get(RtTag.NONC), value3);
}
示例2: writeInContext
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
protected void writeInContext() {
CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
for (;;) {
ByteBuf buf = writeQueue.poll();
if (buf == null) {
break;
}
writeQueueSize.decrementAndGet();
cbb.addComponent(true, buf);
if (cbb.numComponents() == cbb.maxNumComponents()) {
netSocket.write(Buffer.buffer(cbb));
cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
}
}
if (cbb.isReadable()) {
netSocket.write(Buffer.buffer(cbb));
}
}
示例3: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
public List<Object> encode(ByteBufAllocator alloc) {
ByteBuf meta = metadata(alloc);
ByteBuf head = alloc.buffer(FastdfsConstants.FDFS_HEAD_LEN);
head.writeLong(meta.readableBytes() + size);
head.writeByte(cmd());
head.writeByte(FastdfsConstants.ERRNO_OK);
CompositeByteBuf cbb = alloc.compositeBuffer();
cbb.addComponents(head, meta);
cbb.writerIndex(head.readableBytes() + meta.readableBytes());
List<Object> requests = new LinkedList<>();
requests.add(cbb);
requests.add(content);
return requests;
}
示例4: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Proto proto, List<Object> list) throws Exception {
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
if (proto.getBody() != null) {
byteBuf.writeInt(Proto.HEADER_LENGTH + proto.getBody().length);
byteBuf.writeShort(Proto.HEADER_LENGTH);
byteBuf.writeShort(Proto.VERSION);
byteBuf.writeInt(proto.getOperation());
byteBuf.writeInt(proto.getSeqId());
byteBuf.writeBytes(proto.getBody());
} else {
byteBuf.writeInt(Proto.HEADER_LENGTH);
byteBuf.writeShort(Proto.HEADER_LENGTH);
byteBuf.writeShort(Proto.VERSION);
byteBuf.writeInt(proto.getOperation());
byteBuf.writeInt(proto.getSeqId());
}
list.add(byteBuf);
logger.debug("encode: {}", proto);
}
示例5: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
protected void encode(ChannelHandlerContext ctx, Proto proto, List<Object> list) throws Exception {
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
if (proto.getBody() != null) {
byteBuf.writeInt(Proto.HEADER_LENGTH + proto.getBody().length);
byteBuf.writeShort(Proto.HEADER_LENGTH);
byteBuf.writeShort(Proto.VERSION);
byteBuf.writeInt(proto.getOperation());
byteBuf.writeInt(proto.getSeqId());
byteBuf.writeBytes(proto.getBody());
} else {
byteBuf.writeInt(Proto.HEADER_LENGTH);
byteBuf.writeShort(Proto.HEADER_LENGTH);
byteBuf.writeShort(Proto.VERSION);
byteBuf.writeInt(proto.getOperation());
byteBuf.writeInt(proto.getSeqId());
}
list.add(new BinaryWebSocketFrame(byteBuf));
logger.debug("encode: {}", proto);
}
示例6: hashToBase64
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
public static String hashToBase64(ByteBuf objectState) {
ByteBuffer bf = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes());
long h1 = LongHashFunction.farmUo().hashBytes(bf);
long h2 = LongHashFunction.xx().hashBytes(bf);
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
try {
buf.writeLong(h1).writeLong(h2);
ByteBuf b = Base64.encode(buf);
try {
String s = b.toString(CharsetUtil.UTF_8);
return s.substring(0, s.length() - 2);
} finally {
b.release();
}
} finally {
buf.release();
}
}
示例7: decode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
int decompressSize = buf.readInt();
ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
try {
LZ4SafeDecompressor decompressor = factory.safeDecompressor();
ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
int pos = outBuffer.position();
decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
int compressedLength = outBuffer.position() - pos;
out.writerIndex(compressedLength);
return innerCodec.getValueDecoder().decode(out, state);
} finally {
out.release();
}
}
示例8: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Override
public ByteBuf encode(Object in) throws IOException {
Kryo kryo = null;
ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
try {
ByteBufOutputStream baos = new ByteBufOutputStream(out);
Output output = new Output(baos);
kryo = kryoPool.get();
kryo.writeClassAndObject(output, in);
output.close();
return baos.buffer();
} catch (Exception e) {
out.release();
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RedissonKryoCodecException(e);
} finally {
if (kryo != null) {
kryoPool.yield(kryo);
}
}
}
示例9: encodeRequest
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
static ByteBuf encodeRequest(TProtocolFactory protocolFactory, ByteBufAllocator allocator, int sequenceId, MethodMetadata method, List<Object> parameters)
throws Exception
{
TChannelBufferOutputTransport transport = new TChannelBufferOutputTransport(allocator.buffer(1024));
TProtocolWriter protocol = protocolFactory.getProtocol(transport);
// Note that though setting message type to ONEWAY can be helpful when looking at packet
// captures, some clients always send CALL and so servers are forced to rely on the "oneway"
// attribute on thrift method in the interface definition, rather than checking the message
// type.
protocol.writeMessageBegin(new TMessage(method.getName(), method.isOneway() ? ONEWAY : CALL, sequenceId));
// write the parameters
ProtocolWriter writer = new ProtocolWriter(protocol);
writer.writeStructBegin(method.getName() + "_args");
for (int i = 0; i < parameters.size(); i++) {
Object value = parameters.get(i);
ParameterMetadata parameter = method.getParameters().get(i);
writer.writeField(parameter.getName(), parameter.getId(), parameter.getCodec(), value);
}
writer.writeStructEnd();
protocol.writeMessageEnd();
return transport.getOutputBuffer();
}
示例10: writeMessage
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
private void writeMessage(Packet packet) {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(64);
Codecs codecs = connectivityManager.getCodecRegistrar().getCodecs();
buf.writeByte(codecs.getId(packet));
if (packet instanceof SubClientPacket) {
SubClientPacket subClientPacket = (SubClientPacket) packet;
buf.writeByte(subClientPacket.getSenderSubClientId());
buf.writeByte(subClientPacket.getTargetSubClientId());
}
Codec<? extends Packet> codec = connectivityManager.getCodecRegistrar().getCodecs().getCodec(packet.getClass());
codec.encode(packet, buf);
ByteBufUtils.writeUnsignedVarInt(out, buf.readableBytes());
out.writeBytes(buf);
}
示例11: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Test
public void encode() {
// Given
PlayStatePacket expected = createDummyPlayStatePacket();
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
buf.markReaderIndex();
// When
subject.encode(expected, buf);
// Then
PlayStatePacket actual = subject.decode(buf);
assertThat(actual).isEqualTo(expected);
Truth.assertThat(actual.getPlayState()).isEqualTo(expected.getPlayState());
}
示例12: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
@Test
public void encode() {
// Given
LoginPacket expected = createDummyLoginPacket();
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
// When
subject.encode(expected, buf);
// Then
LoginPacket actual = subject.decode(buf);
assertThat(actual).isEqualTo(expected);
assertThat(actual.getClientNetworkVersion()).isEqualTo(expected.getClientNetworkVersion());
assertThat(actual.getConnectionInfo()).isEqualTo(expected.getConnectionInfo());
}
示例13: preEncode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
static ByteBuf preEncode(Object msg, Codec<Object> codec, ByteBufAllocator allocator) throws CodecException {
ByteBuf buf = null;
try {
buf = allocator.buffer();
ByteBufDataWriter writer = new ByteBufDataWriter(buf);
doEncode(msg, writer, codec);
return buf;
} catch (CodecException e) {
if (buf != null) {
buf.release();
}
throw e;
}
}
示例14: getEncodedTargetAddress
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
private ByteBuf getEncodedTargetAddress(ByteBufAllocator allocator, boolean resolve) throws ProxyConnectException {
InetSocketAddress remoteAddress = destinationAddress();
SocksAddressType remoteAddressType;
String remoteHost;
if (!resolve || remoteAddress.isUnresolved()) {
remoteAddressType = SocksAddressType.DOMAIN;
remoteHost = remoteAddress.getHostString();
} else {
remoteHost = remoteAddress.getAddress().getHostAddress();
if (NetUtil.isValidIpV4Address(remoteHost)) {
remoteAddressType = SocksAddressType.IPv4;
} else if (NetUtil.isValidIpV6Address(remoteHost)) {
remoteAddressType = SocksAddressType.IPv6;
} else {
throw new ProxyConnectException("unknown address type: " + StringUtil.simpleClassName(remoteHost));
}
}
int remotePort = remoteAddress.getPort();
SocksCmdRequest request = new SocksCmdRequest(SocksCmdType.UNKNOWN, remoteAddressType, remoteHost, remotePort);
return SSocksAddressEncoder.INSTANCE.encode(allocator, request);
}
示例15: encode
import io.netty.buffer.ByteBufAllocator; //導入依賴的package包/類
public ByteBuf encode(ByteBufAllocator allocator, SocksCmdRequest msg) {
if (LOG.isTraceEnabled()) {
LOG.trace("encode target address");
}
ByteBuf buf = allocator.directBuffer();
msg.encodeAsByteBuf(buf);
buf.skipBytes(3);
if (LOG.isTraceEnabled()) {
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(buf.readerIndex(), bytes);
}
return buf;
}