Jim*_*Jam 5 java inputstream channel fileinputstream kryo
当在RandomAccessFile上构造InputStream以便让Kryo从其反序列化对象时,无论是通过文件的Channel(提供良好的性能)还是通过FileDescriptor(提供出色的性能)构造中介的InputStream,似乎对性能都产生了巨大的影响。 :
RandomAccessFile ra = new RandomAccessFile(dataFile, "r");
Input input1 = new InputWithRandomAccessFile(Channels.newInputStream(ra.getChannel()), FILE_BUF_SIZE, ra);
Input input2 = new InputWithRandomAccessFile(new FileInputStream(ra.getFD()), FILE_BUF_SIZE, ra);
Run Code Online (Sandbox Code Playgroud)
InputWithRandomAccessFile是我自己的类,它扩展了Kryo的Input类,唯一的其他行为是,在调用#setPosition时,它会在R / A文件中寻找正确的位置。
从输入1读取3,000个固定大小的对象大约需要600毫秒,从输入2读取它大约需要16秒。