简体   繁体   English

为什么HttpStaticFileServer的netty示例使用RandomAccessFile?

[英]Why does netty example for HttpStaticFileServer use RandomAccessFile?

netty HttpStaticFileServerHandler示例使用RandomAccessFile有任何特定原因吗?

I think it so that they can demonstrate the use of ChunkedFile , which requires a RandomAccessFile . 我认为,这样他们可以演示ChunkedFile的使用,它需要RandomAccessFile And the reason for that seems to be that ChunkedFile needs to be able to get the file's length. 造成这种情况的原因似乎是ChunkedFile需要能够获取文件的长度。


If I am not using https, I need not use ChunkedFile . 如果我不使用https,则无需使用ChunkedFile So I can choose FileInputStream instead of RandomAccessFile which also returns a FileChannel . 因此,我可以选择FileInputStream而不是RandomAccessFile ,后者也会返回FileChannel But I cannot get fileLength from FileInputStream which is a required input for DefaultFileRegion . 但是我无法从FileInputStream获取fileLength,这是DefaultFileRegion的必需输入。 So I think it is necessary to use RandomAccessFile . 因此,我认为有必要使用RandomAccessFile

That's roughly correct. 大致正确。 However, you could potentially get the file length some other way; 但是,您可能可以通过其他方式获得文件长度。 eg using File.length() . 例如使用File.length()

Actually, it doesn't make much difference which way you get the FileChannel and the file length. 实际上,哪种方式获得FileChannel和文件长度并没有多大区别。

需要一个RAF支持零拷贝,该拷贝需要一个FileChannel,该文件通道是最经常从RAF获取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM