本文整理汇总了Java中io.netty.buffer.ByteBufUtil.encodeString方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBufUtil.encodeString方法的具体用法?Java ByteBufUtil.encodeString怎么用?Java ByteBufUtil.encodeString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.ByteBufUtil
的用法示例。
在下文中一共展示了ByteBufUtil.encodeString方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serialize
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
public ByteBuf serialize(Registry registry, ByteBufAllocator bufAllocator, Object value) throws Exception {
Objects.requireNonNull(value);
KryoPool kryoPool = registry.get(KryoPool.class);
Kryo kryo = kryoPool.borrow();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Output output = new Output(stream);
kryo.writeClassAndObject(output, value);
output.close();
byte[] bytes = stream.toByteArray();
String encoded = ENCODER.encodeToString(bytes);
return ByteBufUtil.encodeString(bufAllocator, CharBuffer.wrap(encoded), CharsetUtil.UTF_8);
} catch (Exception ex) {
throw ex;
} finally {
kryoPool.release(kryo);
}
}
示例2: encode
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg,
List<Object> out) throws Exception {
if (msg.content() instanceof CharSequence) {
CharSequence payload = (CharSequence)msg.content();
if (payload.length() == 0) {
return;
}
AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop =
new DefaultAddressedEnvelope<Object, InetSocketAddress>(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(payload), charset), msg.recipient(), msg.sender());
out.add(addressedEnvelop);
}
}
示例3: encodeString
import io.netty.buffer.ByteBufUtil; //导入方法依赖的package包/类
private ByteBuf encodeString(final ByteBufAllocator bba, final String string) {
return ByteBufUtil.encodeString(bba, CharBuffer.wrap(string), Charset.forName("UTF-8"));
}