简体   繁体   English

是否对协议缓冲区进行了优化,以将java nio用于文件IO?

[英]Is protocol buffers optimized to use java nio for file IO?

I am working with protocol buffers and am wondering if the: 我正在使用协议缓冲区,想知道是否:

mergeDelimitedFrom(FileInputStream fis)

method is optimized to use java nio? 方法是否经过优化以使用Java Nio? I don't really feel like going to the src to find out... but maybe I will. 我真的不喜欢去src找出...但是也许我会。 I feel like it should or have the option to use both. 我觉得应该或可以选择两者都使用。 I am guessing that it isn't. 我猜不是。 If it doesn't - I guess you would have to parse the bytes yourself and handle the delimiter manually if you want nio? 如果不是,我想您需要亲自分析字节并手动处理定界符?

Not super confident with the nio api right now but don't you just call: 现在对Nio API不太自信,但您不只是打电话:

 getChannel()

on the FileInputStream to use nio, so hypothetically nio could be used because a FileInputStream is provided to the mergeDelimitedFrom method? 在FileInputStream上使用nio,因此可以假设使用nio,因为将FileInputStream提供给mergeDelimitedFrom方法?

Related post but more directed towards network IO: 相关文章,但更多针对网络IO:

Using Google Protocol Buffers with Java NIO? 将Google协议缓冲区与Java NIO一起使用?

I was thinking this code wasn't part of the generated code but I guess it is... 我以为该代码不是生成的代码的一部分,但我想它是...

MergeDelimitedFrom calls CodedInputStream.readRawVarint32 below: MergeDelimitedFrom调用下面的CodedInputStream.readRawVarint32:

public static int readRawVarint32(
  final int firstByte, final InputStream input) throws IOException {
if ((firstByte & 0x80) == 0) {
  return firstByte;
}

int result = firstByte & 0x7f;
int offset = 7;
for (; offset < 32; offset += 7) {
  final int b = input.read();
  if (b == -1) {
    throw InvalidProtocolBufferException.truncatedMessage();
  }
  result |= (b & 0x7f) << offset;
  if ((b & 0x80) == 0) {
    return result;
  }
}
// Keep reading up to 64 bits.
for (; offset < 64; offset += 7) {
  final int b = input.read();
  if (b == -1) {
    throw InvalidProtocolBufferException.truncatedMessage();
  }
  if ((b & 0x80) == 0) {
    return result;
  }
}
throw InvalidProtocolBufferException.malformedVarint();

} }

Looks like plain old java.io. 看起来像普通的旧java.io。

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

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