0
我通过Netty服务器(图像,html)从Android资产提供文件。 文本文件,一个HTML保存为MP3播放禁用压缩(我需要一个InputStream!)用Netty服务文件 - 响应被截断一个字节
我的管道正在寻找这样的:
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
}
使用该处理程序,我的截取至少有一个字节。如果我将ChunkedStream
更改为ChunkedNioFile
(因此使用is.getChannel()
而不是is
作为它的构造函数) - 一切正常。
请帮我理解ChunkedStream有什么问题。
我使用的'FileInputStream'是在Android核心的某处产生的。那么我如何进行单元测试呢?我认为它比Netty更适合Android。顺便说一句,'Channel'我从这个(相同的!)'FileInputStream'中工作正常。 – Olegas 2012-01-12 14:16:13
我会写一个单元测试,将一个字节序列写入一个文件。然后用AssetFileDescriptor打开文件,获取FileInputStream并从中读取所有字节。然后检查每个字节,如果它等于您之前写入文件的字节。你应该测试这个不同的FileInputStream.read(...)方法... – 2012-01-13 07:07:37
好主意,我会尝试它。谢谢。 – Olegas 2012-01-14 07:50:50