本文整理汇总了Java中io.netty.handler.stream.ChunkedStream类的典型用法代码示例。如果您正苦于以下问题:Java ChunkedStream类的具体用法?Java ChunkedStream怎么用?Java ChunkedStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChunkedStream类属于io.netty.handler.stream包,在下文中一共展示了ChunkedStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toContent
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException("unknown content type : " + content.getClass().getName());
}
示例2: toContent
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException(
"unknown content type : " + content.getClass().getName());
}
示例3: write
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
final InputStream is = inputStream;
if (future.isStreamWasAlreadyConsumed()) {
if (is.markSupported())
is.reset();
else {
LOGGER.warn("Stream has already been consumed and cannot be reset");
return;
}
} else {
future.setStreamWasAlreadyConsumed(true);
}
channel.write(new ChunkedStream(is), channel.newProgressivePromise()).addListener(
new ProgressListener(future.getAsyncHandler(), future, false, getContentLength()) {
public void operationComplete(ChannelProgressiveFuture cf) {
closeSilently(is);
super.operationComplete(cf);
}
});
channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
示例4: write
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
throws Exception {
// pass along if we don't want to handle the message
if (!(msg instanceof RpcRequestResponse)) {
ctx.write(msg, promise);
return;
}
RpcRequestResponse response = (RpcRequestResponse) msg;
writeHeader(ctx, response);
// write the rest of the stream to be handled by the chunked writer
if (response.getData() != null) {
//TODO support setting a chunking parameter
ctx.write(new ChunkedStream(response.getData()), ctx.voidPromise());
}
// flush the channel
ctx.flush();
}
示例5: write
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
/**
* Thread-safe since there is no shared instance state.
* Just prepend size to the message and stream it through
* a chunked stream and let the base method handle the actual
* chunking.
* <p>
* We do not need to tag the writes since the base class ChunkedWriteHandler
* serializes access to the channel and first write will complete before
* the second begins.
*/
@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
if (msg instanceof ByteBuf) {
final ByteBuf bf = (ByteBuf) msg;
if (bf.hasArray()) {
final byte[] data = bf.array();
final byte[] size = sizeAsByteArr(data.length);
final ByteBuf writeBuffer = Unpooled.wrappedBuffer(size, data);
final ByteBufCloseableStream stream = new ByteBufCloseableStream(writeBuffer);
final ChunkedStream chunkedStream = new ChunkedStream(
stream, NettyChannelInitializer.MAXFRAMELENGTH - 1024);
super.write(ctx, chunkedStream, promise);
} else {
super.write(ctx, msg, promise);
}
} else {
super.write(ctx, msg, promise);
}
}
示例6: postChunkedStreamRequest
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
private void postChunkedStreamRequest(HttpRequestProvider requestProvider, Channel channel, InputStream body) {
HttpRequest request = requestProvider.getHttpRequest(resource);
// don't accept FullHttpRequest here
if (request instanceof FullHttpRequest) {
throw new DockerClientException("fatal: request is instance of FullHttpRequest");
}
request.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
request.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
channel.write(request);
channel.write(new ChunkedStream(new BufferedInputStream(body, 1024 * 1024), 1024 * 1024));
channel.write(LastHttpContent.EMPTY_LAST_CONTENT);
channel.flush();
}
示例7: writeResponseBody
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
protected void writeResponseBody(Representation responseEntity) throws IOException {
try {
// Send the entity to the client
InputStream is = responseEntity.getStream();
getNettyChannel().write(new HttpChunkedInput(new ChunkedStream(is)));
getNettyChannel().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
} catch (IOException ioe) {
// The stream was probably already closed by the
// connector. Probably OK, low message priority.
getLogger().debug("Exception while writing the entity stream.", ioe);
}
}
示例8: channelRead0
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
if (!fullHttpRequest.getDecoderResult().isSuccess()) {
sendError(channelHandlerContext, BAD_REQUEST);
return;
}
MockHttpServletRequest servletRequest = createHttpServletRequest(fullHttpRequest);
MockHttpServletResponse servletResponse = new MockHttpServletResponse();
this.servlet.service(servletRequest, servletResponse);
HttpResponseStatus status = HttpResponseStatus.valueOf(servletResponse.getStatus());
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
for (String name : servletResponse.getHeaderNames()) {
for (String value : servletResponse.getHeaders(name)) {
response.headers().add(name, value);
}
}
// Write the initial line and the header.
channelHandlerContext.write(response);
InputStream contentStream = new ByteArrayInputStream(servletResponse.getContentAsByteArray());
// Write the content and flush it.
ChannelFuture writeFuture = channelHandlerContext.writeAndFlush(new ChunkedStream(contentStream));
writeFuture.addListener(ChannelFutureListener.CLOSE);
}
示例9: put
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void put(InputStream body, com.github.dockerjava.core.MediaType mediaType) {
HttpRequestProvider requestProvider = httpPutRequestProvider(null);
Channel channel = getChannel();
ResponseCallback<Void> resultCallback = new ResponseCallback<Void>();
HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback);
channel.pipeline().addLast(new ChunkedWriteHandler());
channel.pipeline().addLast(responseHandler);
HttpRequest request = requestProvider.getHttpRequest(resource);
// don't accept FullHttpRequest here
if (request instanceof FullHttpRequest) {
throw new DockerClientException("fatal: request is instance of FullHttpRequest");
}
request.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
request.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
request.headers().set(HttpHeaderNames.CONTENT_TYPE, mediaType.getMediaType());
channel.write(request);
channel.write(new ChunkedStream(new BufferedInputStream(body, 1024 * 1024)));
channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
resultCallback.awaitResult();
}
示例10: testChunkedStream
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Test
public void testChunkedStream() {
check(new HttpChunkedInput(new ChunkedStream(new ByteArrayInputStream(BYTES))));
}
示例11: channelRead0
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
if (!request.getDecoderResult().isSuccess()) {
sendError(ctx, BAD_REQUEST);
return;
}
if (request.getMethod() != GET) {
sendError(ctx, METHOD_NOT_ALLOWED);
return;
}
final String uri = request.getUri();
final String path = sanitizeUri(uri);
if (path == null) {
sendError(ctx, FORBIDDEN);
return;
}
// Cache Validation
String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE);
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
// Only compare up to the second because the datetime format we send to the client
// does not have milliseconds
long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
// we use the start time of the JVM as last modified date
long lastModifiedSeconds = ManagementFactory.getRuntimeMXBean().getStartTime();
if (ifModifiedSinceDateSeconds == lastModifiedSeconds) {
sendNotModified(ctx);
return;
}
}
ClassLoader classLoader = HttpClasspathServerHandler.class.getClassLoader();
InputStream stream = classLoader.getResourceAsStream(path);
if(stream == null) {
sendError(ctx, NOT_FOUND);
return;
}
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HttpHeaders.setContentLength(response, stream.available());
setContentTypeHeader(response, path);
setDateAndCacheHeaders(response);
if (HttpHeaders.isKeepAlive(request)) {
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
// Write the initial line and the header.
ctx.write(response);
// Write the content.
ChannelFuture sendFileFuture = ctx.write(new ChunkedStream(stream), ctx.newProgressivePromise());
// Write the end marker.
ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
// Decide whether to close the connection or not.
if (!HttpHeaders.isKeepAlive(request)) {
// Close the connection when the whole content is written out.
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
}
示例12: channelActive
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
//Write the content of the file via a ChunkedStream once the connection is established (we use a FileInputStream only for demo purposes, any InputStream works)
ctx.writeAndFlush(new ChunkedStream(new FileInputStream(file)));
}
示例13: buildBody
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
private HttpChunkedInput buildBody(UploadCommand msg) {
return new HttpChunkedInput(new ChunkedStream(msg.data()));
}
示例14: handle
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void handle(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
//Get the correct DynmapServer
for (DynmapServer dynmapServer : Main.getDynmapServers()) {
AbstractFile file = dynmapServer.getFile(request.getUri());
if (file.exists()) {
if (file.isHidden() || !file.exists()) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
}
if (!file.isFile()) {
HandlerUtil.sendError(ctx, FORBIDDEN);
return;
}
// Cache Validation
String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE);
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HandlerUtil.HTTP_DATE_FORMAT, Locale.US);
Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
// Only compare up to the second because the datetime format we send to the client
// does not have milliseconds
long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
long fileLastModifiedSeconds = file.lastModified() / 1000;
if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
HandlerUtil.sendNotModified(ctx);
return;
}
}
ReadableByteChannel channel = Channels.newChannel(file.getInputStream());
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HandlerUtil.setContentTypeHeader(response, file.getName());
HandlerUtil.setDateAndCacheHeaders(response, file.lastModified());
response.headers().set(CONTENT_LENGTH, file.length());
response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
response.headers().set(VARY, ACCEPT_ENCODING);
// Write the initial line and the header.
ctx.write(response);
// Write the content.
ctx.write(new ChunkedStream(file.getInputStream()));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
return;
}
}
HandlerUtil.sendError(ctx, NOT_FOUND);
}
示例15: handle
import io.netty.handler.stream.ChunkedStream; //导入依赖的package包/类
@Override
public void handle(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
//Get the correct DynmapServer
AbstractFile path = null;
String world = request.getUri().split("/")[3].replace("marker_", "").replace(".json", "");
for (DynmapServer dynmapServer : Main.getDynmapServers()) {
for (DynmapWorld dynmapWorld : dynmapServer.getWorlds()) {
if (dynmapWorld.getName().equals(world)) {
path = dynmapServer.getFile(request.getUri());
}
}
}
AbstractFile file = path;
if (file == null || file.isHidden() || !file.exists()) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
}
if (!file.isFile()) {
HandlerUtil.sendError(ctx, FORBIDDEN);
return;
}
// Cache Validation
String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE);
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HandlerUtil.HTTP_DATE_FORMAT, Locale.US);
Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
// Only compare up to the second because the datetime format we send to the client
// does not have milliseconds
long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
long fileLastModifiedSeconds = file.lastModified() / 1000;
if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
HandlerUtil.sendNotModified(ctx);
return;
}
}
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HandlerUtil.setContentTypeHeader(response, file.getName());
HandlerUtil.setDateAndCacheHeaders(response, file.lastModified());
response.headers().set(CONTENT_LENGTH, file.length());
response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
response.headers().set(VARY, ACCEPT_ENCODING);
// Write the initial line and the header.
ctx.write(response);
// Write the content.
ctx.write(new ChunkedStream(file.getInputStream()));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}