[英]Why does netty example for HttpStaticFileServer use 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 chooseFileInputStream
instead ofRandomAccessFile
which also returns aFileChannel
.因此,我可以选择
FileInputStream
而不是RandomAccessFile
,后者也会返回FileChannel
。 But I cannot get fileLength fromFileInputStream
which is a required input forDefaultFileRegion
.但是我无法从
FileInputStream
获取fileLength,这是DefaultFileRegion
的必需输入。 So I think it is necessary to useRandomAccessFile
.因此,我认为有必要使用
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.