List of usage examples for io.netty.buffer ByteBufOutputStream ByteBufOutputStream
public ByteBufOutputStream(ByteBuf buffer)
From source file:io.pravega.shared.protocol.netty.CommandEncoder.java
License:Open Source License
@SneakyThrows(IOException.class) private void writeMessage(AppendBlock block, ByteBuf out) { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.writeInt(block.getType().getCode()); bout.write(LENGTH_PLACEHOLDER);/*from w ww . jav a 2 s. c o m*/ block.writeFields(bout); bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize + currentBlockSize); }
From source file:io.pravega.shared.protocol.netty.CommandEncoder.java
License:Open Source License
@SneakyThrows(IOException.class) private int writeMessage(WireCommand msg, ByteBuf out) { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.writeInt(msg.getType().getCode()); bout.write(LENGTH_PLACEHOLDER);//from w w w.j a v a2s.c om msg.writeFields(bout); bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize); return endIdx - startIdx; }
From source file:io.reactivex.netty.protocol.http.server.DefaultErrorResponseGenerator.java
License:Apache License
@Override public void updateResponse(HttpServerResponse<O> response, Throwable error) { response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); response.getHeaders().set(HttpHeaders.Names.CONTENT_TYPE, "text/html"); ByteBuf buffer = response.getChannelHandlerContext().alloc().buffer(1024);// 1KB initial length. PrintStream printStream = null; try {//from w w w .j av a 2 s. c o m printStream = new PrintStream(new ByteBufOutputStream(buffer)); error.printStackTrace(printStream); String errorPage = ERROR_HTML_TEMPLATE.replace(STACKTRACE_TEMPLATE_VARIABLE, buffer.toString(Charset.defaultCharset())); response.writeString(errorPage); } finally { ReferenceCountUtil.release(buffer); if (null != printStream) { try { printStream.flush(); printStream.close(); } catch (Exception e) { logger.error("Error closing stream for generating error response stacktrace. This is harmless.", e); } } } }
From source file:io.soliton.protobuf.json.HttpJsonRpcClient.java
License:Apache License
@Override public <O extends Message> ListenableFuture<O> encodeMethodCall(final ClientMethod<O> method, Message input) { clientLogger.logMethodCall(method);//from ww w. j a va 2 s . c o m final JsonResponseFuture<O> responseFuture = handler.newProvisionalResponse(method); JsonObject request = new JsonRpcRequest(method.serviceName(), method.name(), new JsonPrimitive(responseFuture.requestId()), Messages.toJson(input)).toJson(); ByteBuf requestBuffer = Unpooled.buffer(); JsonWriter writer = new JsonWriter( new OutputStreamWriter(new ByteBufOutputStream(requestBuffer), Charsets.UTF_8)); GSON.toJson(request, writer); try { writer.flush(); } catch (IOException ioe) { // Deliberately ignored, as this doesn't involve any I/O } String host = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress(); QueryStringEncoder encoder = new QueryStringEncoder(rpcPath); encoder.addParam("pp", "0"); HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, encoder.toString(), requestBuffer); httpRequest.headers().set(HttpHeaders.Names.HOST, host); httpRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, JsonRpcProtocol.CONTENT_TYPE); httpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, requestBuffer.readableBytes()); channel.writeAndFlush(httpRequest).addListener(new GenericFutureListener<ChannelFuture>() { public void operationComplete(ChannelFuture future) { if (!future.isSuccess()) { clientLogger.logLinkError(method, future.cause()); handler.finish(responseFuture.requestId()); responseFuture.setException(future.cause()); } } }); return responseFuture; }
From source file:io.soliton.protobuf.json.JsonRpcCallback.java
License:Apache License
@Override public void onSuccess(JsonRpcResponse response) { ByteBuf responseBuffer = Unpooled.buffer(); JsonWriter writer = new JsonWriter( new OutputStreamWriter(new ByteBufOutputStream(responseBuffer), Charsets.UTF_8)); (prettyPrint ? GSON_PP : GSON).toJson(response.toJson(), writer); try {//from w w w . ja v a2 s.c o m writer.flush(); } catch (IOException ioe) { // Deliberately ignored, no I/O is involved } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseBuffer); httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, responseBuffer.readableBytes()); channel.writeAndFlush(httpResponse); }
From source file:io.soliton.protobuf.quartz.QuartzClientHandler.java
License:Apache License
/** * {@inheritDoc}/*from w ww. j a v a2 s . c om*/ */ @Override public HttpRequest convertRequest(Envelope request) { ByteBuf requestBuffer = Unpooled.buffer(request.getSerializedSize()); try { OutputStream outputStream = new ByteBufOutputStream(requestBuffer); request.writeTo(outputStream); outputStream.flush(); } catch (IOException e) { // deliberately ignored, as the underlying operation doesn't involve I/O } String host = ((InetSocketAddress) channel().remoteAddress()).getAddress().getHostAddress(); String uriPath = String.format("%s%s/%s", path, request.getService(), request.getMethod()); FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, new QueryStringEncoder(uriPath).toString(), requestBuffer); httpRequest.headers().set(HttpHeaders.Names.HOST, host); httpRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); httpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, requestBuffer.readableBytes()); httpRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, QuartzProtocol.CONTENT_TYPE); return httpRequest; }
From source file:io.soliton.protobuf.quartz.QuartzServerHandler.java
License:Apache License
/** * {@inheritDoc}/*from w ww . j a va2 s . co m*/ */ @Override protected HttpResponse convertResponse(Envelope response) { ByteBuf responseBuffer = Unpooled.buffer(); try { OutputStream outputStream = new ByteBufOutputStream(responseBuffer); response.writeTo(outputStream); outputStream.flush(); } catch (IOException e) { // Deliberately ignored, as the underlying operation doesn't involve I/O } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseBuffer); httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, responseBuffer.readableBytes()); httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, QuartzProtocol.CONTENT_TYPE); return httpResponse; }
From source file:io.techcode.logbulk.pipeline.output.SyslogOutput.java
License:Open Source License
@Override public Handler<JsonObject> encoder() { return data -> { Buffer buf = Buffer.buffer(); // Add facility buf.appendString("<"); String facility = mapping.get(SyslogHeader.FACILITY); String severity = mapping.get(SyslogHeader.SEVERITY); if (mapping.containsKey(SyslogHeader.FACILITY) && mapping.containsKey(SyslogHeader.SEVERITY) && data.containsKey(facility) && data.containsKey(severity)) { int acc = data.getInteger(facility) * 8 + data.getInteger(severity); buf.appendString(String.valueOf(acc)); }//from ww w . j av a2 s. co m buf.appendString(">1 "); // Add timestamp populate(buf, SyslogHeader.TIMESTAMP, data); // Add hostname populate(buf, SyslogHeader.HOST, data); // Add application populate(buf, SyslogHeader.APPLICATION, data); // Add processus populate(buf, SyslogHeader.PROCESSUS, data); // Add msg id populate(buf, SyslogHeader.ID, data); // Add structured data populate(buf, SyslogHeader.DATA, data); // Add message String message = mapping.get(SyslogHeader.MESSAGE); if (mapping.containsKey(SyslogHeader.MESSAGE)) { if (data.containsKey(message)) { buf.appendString(String.valueOf(data.getValue(message))); } else { try { Json.mapper.writeValue(new ByteBufOutputStream(buf.getByteBuf()), data.getMap()); } catch (Exception e) { throw new EncodeException("Failed to encode as JSON: " + e.getMessage()); } } } getConnection().write(framing(buf)); }; }
From source file:me.bigteddy98.mcproxy.protocol.packet.PacketDataWrapper.java
License:Open Source License
public void writeItemStack(ItemStack stack) { if (stack == null) { this.writeShort(-1); return;// w w w . j av a 2 s . c o m } this.writeShort(stack.getBlockId()); this.writeByte(stack.getAmount()); this.writeShort(stack.getDamage()); if (stack.getCompoundTag() == null) { this.writeByte(0); return; } try (NBTOutputStream outStream = new NBTOutputStream( new DataOutputStream(new ByteBufOutputStream(this.buffer)))) { outStream.writeTag(stack.getCompoundTag()); } catch (Exception e) { e.printStackTrace(); } }
From source file:mod.rankshank.arbitraria.common.network.packet.PacketOccurrence.java
@Override public void toBytes(ByteBuf buf) { DataOutputStream data = new DataOutputStream(new ByteBufOutputStream(buf)); try {//w w w . j av a 2s . c o m data.writeByte(flavor); switch (flavor) { case 0: CompressedStreamTools.write(occurrence.writeToNBT(), data); break; case 1: data.writeUTF(id.toString()); break; case 2: NBTTagCompound compound = new NBTTagCompound(); NBTTagList list = new NBTTagList(); for (CurrentOccurrence write : occurrences) list.appendTag(write.writeToNBT()); compound.setTag("data", list); CompressedStreamTools.write(compound, data); break; default: break; } } catch (IOException e) { Arbitraria.error(new Throwable("Issue writing CurrentOccurrence packet"), e); } }