我已经经由Netty的服务器提供来自Android的资产文件(图像,HTML)。 文本文件,一个HTML保存为MP3播放以禁用压缩(我需要一个InputStream!)

我的管道正在寻找这样的:

    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());

    pipeline.addLast("handler", new AssetsServerHandler(context));

我的处理程序是:

public class AssetsServerHandler extends SimpleChannelUpstreamHandler {

    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

        // some checks

        final FileInputStream is;
        final AssetFileDescriptor afd;
        try {
            afd = assetManager.openFd(path);
            is = afd.createInputStream();   
        } catch(IOException exc) {
            sendError(ctx, NOT_FOUND);
            return;
        }

        final long fileLength = afd.getLength();

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentLength(response, fileLength);

        final Channel ch = e.getChannel();
        final ChannelFuture future;
        ch.write(response);
        future = ch.write(new ChunkedStream(is));
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                future.getChannel().close();
            }
        });
        if (!isKeepAlive(request)) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
    // other stuff
}

使用该处理程序我有我的resposes由至少一个字节截断。如果我改变ChunkedStreamChunkedNioFile(因此使用is.getChannel()代替is作为构造它) - 一切完美。

请,帮助我了解什么是错的ChunkedStream。

有帮助吗?

解决方案

您的代码看起来我的权利。是否AssetFileDescriptor的返回的FileInputStream包含“所有字节”?你可以使用单元测试检查。如果在它没有错误,然后在网状的一个bug。我大量使用ChunkInputStream的和从未有过这样的问题还没有,但也许这真的取决于InputStream的性质。

将是很好,如果你可以写一个测试用例,并在网状的github上打开一个问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top