本文整理汇总了Java中io.netty.buffer.ByteBufOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java ByteBufOutputStream类的具体用法?Java ByteBufOutputStream怎么用?Java ByteBufOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ByteBufOutputStream类属于io.netty.buffer包,在下文中一共展示了ByteBufOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encode
import io.netty.buffer.ByteBufOutputStream; //导入依赖的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);
}
}
}
示例2: getDino
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
private static FullHttpResponse getDino(XrpcRequest request, List<Dino> dinos) {
try {
DinoGetRequest getRequest =
DinoGetRequest.parseFrom(CodedInputStream.newInstance(request.getData().nioBuffer()));
Optional<Dino> dinoOptional =
dinos.stream().filter(xs -> xs.getName().equals(getRequest.getName())).findFirst();
if (dinoOptional.isPresent()) {
DinoGetReply getReply = DinoGetReply.newBuilder().setDino(dinoOptional.get()).build();
ByteBuf resp = request.getByteBuf();
resp.ensureWritable(CodedOutputStream.computeMessageSizeNoTag(getReply), true);
getReply.writeTo(new ByteBufOutputStream(resp));
return Recipes.newResponse(
HttpResponseStatus.OK,
request.getByteBuf().writeBytes(resp),
Recipes.ContentType.Application_Octet_Stream);
}
} catch (IOException e) {
return Recipes.newResponseBadRequest("Malformed GetDino Request: " + e.getMessage());
}
return Recipes.newResponseOk("Dino not Found");
}
示例3: writeNBTTagCompoundToBuffer
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
/**
* Writes a compressed NBTTagCompound to this buffer
*/
public void writeNBTTagCompoundToBuffer(NBTTagCompound nbt)
{
if (nbt == null)
{
this.writeByte(0);
}
else
{
try
{
CompressedStreamTools.write(nbt, new ByteBufOutputStream(this));
}
catch (IOException ioexception)
{
throw new EncoderException(ioexception);
}
}
}
示例4: processRequest
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
private HttpResponse processRequest(FullHttpRequest request) throws JsonProcessingException {
HttpResponse response;
ByteBuf responseContent = Unpooled.buffer();
HttpResponseStatus responseStatus = HttpResponseStatus.OK;
try (ByteBufOutputStream os = new ByteBufOutputStream(responseContent);
ByteBufInputStream is = new ByteBufInputStream(request.content().retain())){
int result = jsonRpcServer.handleRequest(is, os);
responseStatus = HttpResponseStatus.valueOf(DefaultHttpStatusCodeProvider.INSTANCE.getHttpStatusCode(result));
} catch (Exception e) {
LOGGER.error("Unexpected error", e);
responseContent = buildErrorContent(JSON_RPC_SERVER_ERROR_HIGH_CODE, HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase());
responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
} finally {
response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
responseStatus,
responseContent
);
}
return response;
}
示例5: write
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
@Override
public void write(RakNetByteBuf out) {
super.write(out);
RakNetOutputStream os = new RakNetOutputStream(new BufferedOutputStream(new DeflaterOutputStream(new ByteBufOutputStream(out))));
RakNetByteBuf payload = RakNetByteBuf.buffer();
body.write(payload);
try {
int bodySize = payload.readableBytes();
byte[] bytes = new byte[bodySize];
payload.readBytes(bytes);
os.writeUnsignedVarInt(bodySize);
os.write(bytes);
} catch (Exception ignored) {
} finally {
payload.release();
}
}
示例6: encode
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
@Override
protected void encode(ChannelHandlerContext ctx, T msg, ByteBuf out)
throws Exception {
int lengthIndex = out.writerIndex();
// length field, will be filled in later.
out.writeInt(0);
int startIndex = out.writerIndex();
ByteBufOutputStream os = new ByteBufOutputStream(out);
TCompactProtocol thriftProtocol =
new TCompactProtocol(new TIOStreamTransport(os));
msg.write(thriftProtocol);
os.close();
int endIndex = out.writerIndex();
// update the length field
int length = endIndex - startIndex;
out.setInt(lengthIndex, length);
}
示例7: writeNBTTagCompoundToBuffer
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
/**
* Writes a compressed NBTTagCompound to this buffer
*/
public PacketBuffer writeNBTTagCompoundToBuffer(@Nullable NBTTagCompound nbt)
{
if (nbt == null)
{
this.writeByte(0);
}
else
{
try
{
CompressedStreamTools.write(nbt, new ByteBufOutputStream(this));
}
catch (IOException ioexception)
{
throw new EncoderException(ioexception);
}
}
return this;
}
示例8: writeNBTTagCompoundToBuffer
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
public static void writeNBTTagCompoundToBuffer(ByteBuf buf, NBTTagCompound tag)
{
if (tag == null)
{
buf.writeByte(0);
return;
}
try
{
CompressedStreamTools.write(tag, new ByteBufOutputStream(buf));
}
catch (IOException ioexception)
{
ModLogger.error("IOException while trying to write a NBTTagCompound to ByteBuf");
throw new EncoderException(ioexception);
}
}
示例9: writeResourceReport
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
private void writeResourceReport(Channel channel) {
ByteBuf content = Unpooled.buffer();
Writer writer = new OutputStreamWriter(new ByteBufOutputStream(content), CharsetUtil.UTF_8);
try {
reportAdapter.toJson(resourceReport.get(), writer);
writer.close();
} catch (IOException e) {
LOG.error("error writing resource report", e);
writeAndClose(channel, new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR,
Unpooled.copiedBuffer(e.getMessage(), StandardCharsets.UTF_8)));
return;
}
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
HttpUtil.setContentLength(response, content.readableBytes());
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
channel.writeAndFlush(response);
}
示例10: toBytes
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
@Override
public void toBytes(ByteBuf buf)
{
ByteBufOutputStream output = new ByteBufOutputStream(buf);
try
{
output.writeUTF(this.filename);
output.writeInt(this.frames.size());
for (Frame frame : this.frames)
{
frame.toBytes(output);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例11: serialize
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
/**
* Serializes the passed object to the passed byte buffer or a new one if the passed one is null
* @param object The object to serialize
* @param buff The buffer to write to, or null to create a new one
* @return the written buffer
*/
public static ByteBuf serialize(final Object object, final ByteBuf buff) {
if (object == null) throw new IllegalArgumentException("Object was null");
final ByteBuf _buff = buff==null ? byteBufAllocator.buffer() : buff;
final OutputStream os = new ByteBufOutputStream(_buff);
try {
serialize(object, os);
os.flush();
os.close();
} catch (Exception ex) {
throw new RuntimeException("Failed to write object to buffer", ex);
} finally {
try { os.close(); } catch (Exception x) {/* No Op */}
}
return _buff;
}
示例12: serializeOffHeapLoopBack
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
/**
* Serializes the passed object to an off-heap buffer and returns an InputStream to read it back
* @param obj The object to serialize
* @return an InputStream to read back the JSON serialized object
*/
public static InputStream serializeOffHeapLoopBack(final Object obj) {
if(obj==null) throw new IllegalArgumentException("The passed object was null");
final ByteBuf cb = byteBufAllocator.buffer();
final OutputStream os = new ByteBufOutputStream(cb);
try {
serialize(obj, os);
os.flush();
os.close();
} catch (Exception ex) {
throw new RuntimeException("Failed to write object to buffer", ex);
}
return new ByteBufInputStream(cb) {
@Override
public void close() throws IOException {
super.close();
try { cb.release(); } catch (Exception x) {/* No Op */}
}
};
}
示例13: serializeAndGzip
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
/**
* Serializes and gzips the passed object to the passed byte buffer or a new one if the passed one is null
* @param object The object to serialize
* @param buff The buffer to write to, or null to create a new one
* @return the written buffer
*/
public static ByteBuf serializeAndGzip(final Object object, final ByteBuf buff) {
if (object == null) throw new IllegalArgumentException("Object was null");
final ByteBuf _buff = buff==null ? byteBufAllocator.buffer() : buff;
final OutputStream os = new ByteBufOutputStream(_buff);
try {
final GZIPOutputStream gos = new GZIPOutputStream(os);
serialize(object, gos);
gos.finish();
gos.flush();
gos.close();
os.flush();
os.close();
} catch (Exception ex) {
throw new RuntimeException("Failed to write object to buffer", ex);
} finally {
try { os.close(); } catch (Exception x) {/* No Op */}
}
return _buff;
}
示例14: getPayload
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
@Override
protected ByteBuf getPayload(ChannelHandlerContext ctx, Batch batch) throws IOException {
ByteBuf payload = super.getPayload(ctx, batch);
Deflater deflater = new Deflater();
ByteBufOutputStream output = new ByteBufOutputStream(ctx.alloc().buffer());
DeflaterOutputStream outputDeflater = new DeflaterOutputStream(output, deflater);
byte[] chunk = new byte[payload.readableBytes()];
payload.readBytes(chunk);
outputDeflater.write(chunk);
outputDeflater.close();
ByteBuf content = ctx.alloc().buffer();
content.writeByte(batch.getProtocol());
content.writeByte('C');
content.writeInt(output.writtenBytes());
content.writeBytes(output.buffer());
return content;
}
示例15: channelActive
import io.netty.buffer.ByteBufOutputStream; //导入依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext context) throws Exception {
ByteBuf buffer = context.alloc().ioBuffer(1024);
boolean success = false;
try {
ByteBufOutputStream out = new ByteBufOutputStream(buffer);
for (String path : myLockedPaths) out.writeUTF(path);
out.writeUTF(PATHS_EOT_RESPONSE);
out.close();
success = true;
}
finally {
if (!success) {
buffer.release();
}
}
context.writeAndFlush(buffer);
}