简体   繁体   English

Java FileChannel与Clojure中的ByteBuffer?

[英]Java FileChannel with ByteBuffer in Clojure?

I want to use the following code in Clojure to quickly read a file into memory: 我想在Clojure中使用以下代码将文件快速读取到内存中:

FileInputStream f = new FileInputStream( name );
FileChannel ch = f.getChannel( );
byte[] barray = new byte[SIZE];
ByteBuffer bb = ByteBuffer.wrap( barray );
long checkSum = 0L;
int nRead;

while ((nRead = ch.read(bb)) != -1) {
    for (int i = 0; i < nRead; i++)
        checkSum += barray[i];

    bb.clear( );
}

Code from: FileChannel with array ByteBuffer and byte array access 来自以下内容的代码: 具有数组ByteBuffer和字节数组访问的FileChannel

Calling the Java class constructors and dot-methods is straightforward enough, but will using Clojure's while and for cause a significant performance penalty? 调用Java类的构造函数和点方法很简单,但是使用Clojure的whilefor会导致性能下降吗? Is there anything wrong with just trying to convert Clojure the most direct way? 仅尝试以最直接的方式转换Clojure有什么问题吗?

You'll have to use dotimes instead of for and loop..recur instead of while . 您必须使用dotimes代替forloop..recur代替while

However if you are really after performance, instead of reading, I would memory-map the FileChannel and create a LongBuffer from the ByteBuffer so as to read bytes eight by eight. 但是,如果您真正追求性能,而不是读取,我将对FileChannel内存映射,并从ByteBuffer创建一个LongBuffer以便以八乘八的方式读取字节。

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

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