本文整理匯總了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"));
}