使用 http 协议查找(跳过)输入流的最快方法
我正在制作某种下载服务,它能够恢复之前的部分下载。我目前正在使用像这样的跳过方法,
long skipped = 0;
while (skipped < track.getCacheFile().length()){
skipped += is.skip(track.getCacheFile().length()-skipped);
}
我刚刚做了一个测试,在输入流中跳过 45 mb 花了大约 57 秒。我很好奇某些本机代码是如何做到这一点的,例如,媒体播放器可以立即寻找远程流的任何部分。我意识到我无法访问相同的库,但是我可以实现类似的目标吗?
顺便说一句,那个测试是在 wifi 上进行的。在普通数据网络上显然要慢得多。
更新:非常简单(感谢下面)
if (track.getCacheFile().length() > 0){
request.setHeader("Range","bytes="+track.getCacheFile().length()+"-");
}
I am making a download service of sorts, and it has the ability to resume a previous partial download. I am currently using the skip method like this
long skipped = 0;
while (skipped < track.getCacheFile().length()){
skipped += is.skip(track.getCacheFile().length()-skipped);
}
I just did a test and it took about 57 seconds seconds to skip 45 mb in an inputstream. I am curious how certain native code does this, for instance, the mediaplayer can seek to any part of a remote stream instantaneously. I realize that I do not have access to the same libraries, but can I achieve something similar.
btw, that test was on wifi. It is obviously much slower on normal data networks.
Update: very simple (thanks to below)
if (track.getCacheFile().length() > 0){
request.setHeader("Range","bytes="+track.getCacheFile().length()+"-");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 http 作为启动输入流的协议,您可以尝试 RANGE 标头。
看看这里:
http://www.west-wind.com/Weblog/帖子/244.aspx
If you are using http as a protocol to initiate your inputstream, you may try the RANGE header.
Take a look here :
http://www.west-wind.com/Weblog/posts/244.aspx
跳过方法的问题是,即使跳过数据,您也必须读取数据,因此您需要接收它们。最好的解决方案可能是仅向服务器请求您想要的部分。
The problem with the skip method is that you have to read the data even if you skip them so you need to receive them. The best solution is probably to request the server only the part you want.
你可以这样做:
然后当你需要跳过时,你实际上通过 HTTP 重新连接到新的偏移量。工作快速可靠,比使用输入流的跳过要好得多。
You can do it like this:
Then when you need to skip, you actually do a reconnect via HTTP to the new offset. Works quick and reliable, much better than using the inputstream's skip.