List of usage examples for io.netty.buffer ByteBufInputStream ByteBufInputStream
public ByteBufInputStream(ByteBuf buffer)
From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java
License:Apache License
/** * Deserializes the KvState request failure. * * @param buf Buffer to deserialize (expected to be positioned after header) * @return Deserialized KvStateRequestFailure *///from w w w.j a v a 2s. com public static KvStateRequestFailure deserializeKvStateRequestFailure(ByteBuf buf) throws IOException, ClassNotFoundException { long requestId = buf.readLong(); Throwable cause; try (ByteBufInputStream bbis = new ByteBufInputStream(buf); ObjectInputStream in = new ObjectInputStream(bbis)) { cause = (Throwable) in.readObject(); } return new KvStateRequestFailure(requestId, cause); }
From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java
License:Apache License
/** * Deserializes the KvState request failure. * * @param buf Buffer to deserialize (expected to be positioned after header) * @return Deserialized KvStateRequestFailure * @throws IOException Serialization failure are forwarded * @throws ClassNotFoundException If Exception type can not be loaded *///from w w w.j a v a 2s . c o m public static Throwable deserializeServerFailure(ByteBuf buf) throws IOException, ClassNotFoundException { try (ByteBufInputStream bbis = new ByteBufInputStream(buf); ObjectInputStream in = new ObjectInputStream(bbis)) { return (Throwable) in.readObject(); } }
From source file:org.apache.giraph.comm.netty.handler.SaslClientHandler.java
License:Apache License
/** * Decode the message read by handler/*from w ww. j av a2 s .c o m*/ * * @param ctx channel handler context * @param msg message to decode into a writable request * @return decoded writablerequest object * @throws Exception */ protected WritableRequest decode(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof ByteBuf)) { throw new IllegalStateException("decode: Got illegal message " + msg); } // Decode msg into an object whose class C implements WritableRequest: // C will be either SaslTokenMessage or SaslComplete. // // 1. Convert message to a stream that can be decoded. ByteBuf buf = (ByteBuf) msg; ByteBufInputStream inputStream = new ByteBufInputStream(buf); // 2. Get first byte: message type: int enumValue = inputStream.readByte(); RequestType type = RequestType.values()[enumValue]; if (LOG.isDebugEnabled()) { LOG.debug("decode: Got a response of type " + type + " from server:" + ctx.channel().remoteAddress()); } // 3. Create object of the type determined in step 2. Class<? extends WritableRequest> writableRequestClass = type.getRequestClass(); WritableRequest serverResponse = ReflectionUtils.newInstance(writableRequestClass, conf); // 4. Deserialize the inputStream's contents into the newly-constructed // serverResponse object. try { serverResponse.readFields(inputStream); } catch (IOException e) { LOG.error("decode: Exception when trying to read server response: " + e); } ReferenceCountUtil.release(buf); // serverResponse can now be used in the next stage in pipeline. return serverResponse; }
From source file:org.apache.giraph.utils.RequestUtils.java
License:Apache License
/** * decodeWritableRequest based on predicate * * @param buf ByteBuf//from w ww . j ava2s. co m * @param request writableRequest * @return properly initialized writableRequest * @throws IOException */ public static WritableRequest decodeWritableRequest(ByteBuf buf, WritableRequest request) throws IOException { ByteBufInputStream input = new ByteBufInputStream(buf); request.readFields(input); return request; }
From source file:org.apache.hadoop.hbase.ipc.AsyncServerResponseHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf inBuffer = (ByteBuf) msg;//from ww w . ja v a 2 s . c o m ByteBufInputStream in = new ByteBufInputStream(inBuffer); int totalSize = inBuffer.readableBytes(); try { // Read the header RPCProtos.ResponseHeader responseHeader = RPCProtos.ResponseHeader.parseDelimitedFrom(in); int id = responseHeader.getCallId(); AsyncCall call = channel.removePendingCall(id); if (call == null) { // So we got a response for which we have no corresponding 'call' here on the client-side. // We probably timed out waiting, cleaned up all references, and now the server decides // to return a response. There is nothing we can do w/ the response at this stage. Clean // out the wire of the response so its out of the way and we can get other responses on // this connection. int readSoFar = IPCUtil.getTotalSizeWhenWrittenDelimited(responseHeader); int whatIsLeftToRead = totalSize - readSoFar; // This is done through a Netty ByteBuf which has different behavior than InputStream. // It does not return number of bytes read but will update pointer internally and throws an // exception when too many bytes are to be skipped. inBuffer.skipBytes(whatIsLeftToRead); return; } if (responseHeader.hasException()) { RPCProtos.ExceptionResponse exceptionResponse = responseHeader.getException(); RemoteException re = createRemoteException(exceptionResponse); if (exceptionResponse.getExceptionClassName().equals(FatalConnectionException.class.getName())) { channel.close(re); } else { call.setFailed(re); } } else { Message value = null; // Call may be null because it may have timedout and been cleaned up on this side already if (call.responseDefaultType != null) { Message.Builder builder = call.responseDefaultType.newBuilderForType(); builder.mergeDelimitedFrom(in); value = builder.build(); } CellScanner cellBlockScanner = null; if (responseHeader.hasCellBlockMeta()) { int size = responseHeader.getCellBlockMeta().getLength(); byte[] cellBlock = new byte[size]; inBuffer.readBytes(cellBlock, 0, cellBlock.length); cellBlockScanner = channel.client.createCellScanner(cellBlock); } call.setSuccess(value, cellBlockScanner); } } catch (IOException e) { // Treat this as a fatal condition and close this connection channel.close(e); } finally { inBuffer.release(); } }
From source file:org.apache.hadoop.hbase.ipc.NettyRpcDuplexHandler.java
License:Apache License
private void readResponse(ChannelHandlerContext ctx, ByteBuf buf) throws IOException { int totalSize = buf.readInt(); ByteBufInputStream in = new ByteBufInputStream(buf); ResponseHeader responseHeader = ResponseHeader.parseDelimitedFrom(in); int id = responseHeader.getCallId(); if (LOG.isTraceEnabled()) { LOG.trace("got response header " + TextFormat.shortDebugString(responseHeader) + ", totalSize: " + totalSize + " bytes"); }/* w w w . j ava 2 s . c o m*/ RemoteException remoteExc; if (responseHeader.hasException()) { ExceptionResponse exceptionResponse = responseHeader.getException(); remoteExc = IPCUtil.createRemoteException(exceptionResponse); if (IPCUtil.isFatalConnectionException(exceptionResponse)) { // Here we will cleanup all calls so do not need to fall back, just return. exceptionCaught(ctx, remoteExc); return; } } else { remoteExc = null; } Call call = id2Call.remove(id); if (call == null) { // So we got a response for which we have no corresponding 'call' here on the client-side. // We probably timed out waiting, cleaned up all references, and now the server decides // to return a response. There is nothing we can do w/ the response at this stage. Clean // out the wire of the response so its out of the way and we can get other responses on // this connection. int readSoFar = IPCUtil.getTotalSizeWhenWrittenDelimited(responseHeader); int whatIsLeftToRead = totalSize - readSoFar; if (LOG.isDebugEnabled()) { LOG.debug("Unknown callId: " + id + ", skipping over this response of " + whatIsLeftToRead + " bytes"); } return; } if (remoteExc != null) { call.setException(remoteExc); return; } Message value; if (call.responseDefaultType != null) { Builder builder = call.responseDefaultType.newBuilderForType(); builder.mergeDelimitedFrom(in); value = builder.build(); } else { value = null; } CellScanner cellBlockScanner; if (responseHeader.hasCellBlockMeta()) { int size = responseHeader.getCellBlockMeta().getLength(); // Maybe we could read directly from the ByteBuf. // The problem here is that we do not know when to release it. byte[] cellBlock = new byte[size]; buf.readBytes(cellBlock); cellBlockScanner = cellBlockBuilder.createCellScanner(this.codec, this.compressor, cellBlock); } else { cellBlockScanner = null; } call.setResponse(value, cellBlockScanner); }
From source file:org.apache.olingo.netty.server.core.ODataNettyHandlerImpl.java
License:Apache License
/** * Extract the information part of Netty Request and fill OData Request * @param odRequest/* w w w. ja v a2s. co m*/ * @param httpRequest * @param split * @param contextPath * @return * @throws ODataLibraryException */ private ODataRequest fillODataRequest(final ODataRequest odRequest, final HttpRequest httpRequest, final int split, final String contextPath) throws ODataLibraryException { final int requestHandle = debugger.startRuntimeMeasurement("ODataHttpHandlerImpl", "fillODataRequest"); try { ByteBuf byteBuf = ((HttpContent) httpRequest).content(); ByteBufInputStream inputStream = new ByteBufInputStream(byteBuf); odRequest.setBody(inputStream); odRequest.setProtocol(httpRequest.protocolVersion().text()); odRequest.setMethod(extractMethod(httpRequest)); int innerHandle = debugger.startRuntimeMeasurement("ODataNettyHandlerImpl", "copyHeaders"); copyHeaders(odRequest, httpRequest); debugger.stopRuntimeMeasurement(innerHandle); innerHandle = debugger.startRuntimeMeasurement("ODataNettyHandlerImpl", "fillUriInformation"); fillUriInformationFromHttpRequest(odRequest, httpRequest, split, contextPath); debugger.stopRuntimeMeasurement(innerHandle); return odRequest; } finally { debugger.stopRuntimeMeasurement(requestHandle); } }
From source file:org.apache.qpid.jms.message.facade.test.JmsTestBytesMessageFacade.java
License:Apache License
@Override public InputStream getInputStream() throws JMSException { if (bytesOut != null) { throw new IllegalStateException("Body is being written to, cannot perform a read."); }//from ww w . j a v a 2 s . c o m if (bytesIn == null) { // Duplicate the content buffer to allow for getBodyLength() validity. bytesIn = new ByteBufInputStream(content.duplicate()); } return bytesIn; }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpJmsBytesMessageFacade.java
License:Apache License
@Override public InputStream getInputStream() throws JMSException { if (bytesOut != null) { throw new IllegalStateException("Body is being written to, cannot perform a read."); }/*from w ww. j a va 2s . c o m*/ if (bytesIn == null) { Binary body = getBinaryFromBody(); // Duplicate the content buffer to allow for getBodyLength() validity. bytesIn = new ByteBufInputStream( Unpooled.wrappedBuffer(body.getArray(), body.getArrayOffset(), body.getLength())); } return bytesIn; }
From source file:org.apache.tajo.ws.rs.netty.NettyRestHandlerContainer.java
License:Apache License
protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { URI baseUri = getBaseUri(ctx, request); URI requestUri = baseUri.resolve(request.getUri()); ByteBuf responseContent = PooledByteBufAllocator.DEFAULT.buffer(); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseContent);//from ww w . ja va 2 s. co m NettyRestResponseWriter responseWriter = new NettyRestResponseWriter(ctx, response); ContainerRequest containerRequest = new ContainerRequest(baseUri, requestUri, request.getMethod().name(), getSecurityContext(), new MapPropertiesDelegate()); containerRequest.setEntityStream(new ByteBufInputStream(request.content())); HttpHeaders httpHeaders = request.headers(); for (String headerName : httpHeaders.names()) { List<String> headerValues = httpHeaders.getAll(headerName); containerRequest.headers(headerName, headerValues); } containerRequest.setWriter(responseWriter); try { applicationHandler.handle(containerRequest); } finally { responseWriter.releaseConnection(); } }