use of io.netty.buffer.ByteBuf in project elasticsearch by elastic.
the class ByteBufBytesReferenceTests method testSliceOnAdvancedBuffer.
public void testSliceOnAdvancedBuffer() throws IOException {
BytesReference bytesReference = newBytesReference(randomIntBetween(10, 3 * PAGE_SIZE));
BytesRef bytesRef = bytesReference.toBytesRef();
ByteBuf channelBuffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length);
int numBytesToRead = randomIntBetween(1, 5);
for (int i = 0; i < numBytesToRead; i++) {
channelBuffer.readByte();
}
BytesReference other = Netty4Utils.toBytesReference(channelBuffer);
BytesReference slice = bytesReference.slice(numBytesToRead, bytesReference.length() - numBytesToRead);
assertEquals(other, slice);
assertEquals(other.slice(3, 1), slice.slice(3, 1));
}
use of io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class HttpClientRequestImpl method handleNextRequest.
private void handleNextRequest(HttpClientResponse resp, HttpClientRequestImpl next, long timeoutMs) {
next.handler(respHandler);
next.exceptionHandler(exceptionHandler());
exceptionHandler(null);
next.endHandler(endHandler);
next.pushHandler = pushHandler;
next.followRedirects = followRedirects - 1;
next.written = written;
if (next.hostHeader == null) {
next.hostHeader = hostHeader;
}
if (headers != null && next.headers == null) {
next.headers().addAll(headers);
}
ByteBuf body;
switch(next.method) {
case GET:
body = null;
break;
case OTHER:
next.rawMethod = rawMethod;
body = null;
break;
default:
if (cachedChunks != null) {
body = cachedChunks;
} else {
body = null;
}
break;
}
cachedChunks = null;
Future<Void> fut = Future.future();
fut.setHandler(ar -> {
if (ar.succeeded()) {
if (timeoutMs > 0) {
next.setTimeout(timeoutMs);
}
next.write(body, true);
} else {
next.handleException(ar.cause());
}
});
if (exceptionOccurred != null) {
fut.fail(exceptionOccurred);
} else if (completed) {
fut.complete();
} else {
exceptionHandler(err -> {
if (!fut.isComplete()) {
fut.fail(err);
}
});
completionHandler = v -> {
if (!fut.isComplete()) {
fut.complete();
}
};
}
}
use of io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class HttpServerResponseImpl method end.
@Override
public void end(Buffer chunk) {
synchronized (conn) {
if (!chunked && !contentLengthSet()) {
headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(chunk.length()));
}
ByteBuf buf = chunk.getByteBuf();
end0(buf);
}
}
use of io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class ServerConnection method createNetSocket.
NetSocket createNetSocket() {
NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, server.getSslHelper(), metrics);
socket.metric(metric());
Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
connectionMap.put(channel, socket);
// Flush out all pending data
endReadAndFlush();
// remove old http handlers and replace the old handler with one that handle plain sockets
ChannelPipeline pipeline = channel.pipeline();
ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
if (compressor != null) {
pipeline.remove(compressor);
}
pipeline.remove("httpDecoder");
if (pipeline.get("chunkedWriter") != null) {
pipeline.remove("chunkedWriter");
}
channel.pipeline().replace("handler", "handler", new VertxNetHandler<NetSocketImpl>(channel, socket, connectionMap) {
@Override
public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
// remove from the real mapping
server.removeChannel(channel);
super.exceptionCaught(chctx, t);
}
@Override
public void channelInactive(ChannelHandlerContext chctx) throws Exception {
// remove from the real mapping
server.removeChannel(channel);
super.channelInactive(chctx);
}
@Override
public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
if (msg instanceof HttpContent) {
ReferenceCountUtil.release(msg);
return;
}
super.channelRead(chctx, msg);
}
@Override
protected void handleMsgReceived(Object msg) {
ByteBuf buf = (ByteBuf) msg;
conn.handleDataReceived(Buffer.buffer(buf));
}
});
// check if the encoder can be removed yet or not.
if (lastWriteFuture == null) {
channel.pipeline().remove("httpEncoder");
} else {
lastWriteFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
channel.pipeline().remove("httpEncoder");
}
});
}
return socket;
}
use of io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class FileStreamChannel method doWrite.
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
ByteBuf chunk;
while (!stream.isNotWritable() && (chunk = (ByteBuf) in.current()) != null) {
bytesWritten += chunk.readableBytes();
stream.writeData(chunk.retain(), false);
stream.handlerContext.flush();
in.remove();
}
}
Aggregations