Java Google Protobuf ByteString 与 Byte[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29018411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:12:18  来源:igfitidea点击:

Google Protobuf ByteString vs. Byte[]

javastringbytearrayprotocol-buffers

提问by Rahim Pirbhai

I am working with google protobuf in Java. I see that it is possible to serialize a protobuf message to String, byte[], ByteString, etc: (Source: https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite)

我正在 Java 中使用 google protobuf。我看到可以将 protobuf 消息序列化为 String、byte[]、ByteString 等:(来源:https: //developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf /消息精简版)

I don't know what a ByteString is. I got the following definition from the the protobuf API documentation (source: https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/ByteString): "Immutable sequence of bytes. Substring is supported by sharing the reference to the immutable underlying bytes, as with String."

我不知道 ByteString 是什么。我从 protobuf API 文档中得到以下定义(来源:https: //developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/ByteString):“不可变的字节序列。子字符串与 String 一样,通过共享对不可变底层字节的引用来支持。”

It is not clear to me how a ByteString is different from a String or byte[]. Can somebody please explain? Thanks.

我不清楚 ByteString 与 String 或 byte[] 有何不同。有人可以解释一下吗?谢谢。

采纳答案by Matt Ball

You can think of ByteStringas an immutable byte array. That's pretty much it. It's a byte[]which you can use in a protobuf. Protobuf does not let you use Java arrays because they're mutable.

您可以将其ByteString视为不可变的字节数组。差不多就是这样。byte[]您可以在 protobuf 中使用它。Protobuf 不允许您使用 Java 数组,因为它们是可变的。

ByteStringexists because Stringis not suitable for representing arbitrary sequences of bytes. Stringis specifically for character data.

ByteString存在是因为String不适合表示任意字节序列。String专门用于字符数据。

The protobuf MessageLite Interface provides toByteArray() and toByteString() methods. If ByteString is an immutable byte[], would the byte representation of a message represented by both ByteString and byte[] be the same?

protobuf MessageLite 接口提供 toByteArray() 和 toByteString() 方法。如果 ByteString 是不可变的 byte[],那么 ByteString 和 byte[] 表示的消息的字节表示是否相同?

Sort of. If you call toByteArray()you'll get the same value as if you were to call toByteString().toByteArray(). Compare the implementation of the two methods, in AbstractMessageLite:

有点。如果您调用,toByteArray()您将获得与调用相同的值toByteString().toByteArray()。比较两种方法的实现,在AbstractMessageLite

public ByteString toByteString() {
  try {
    final ByteString.CodedBuilder out =
      ByteString.newCodedBuilder(getSerializedSize());
    writeTo(out.getCodedOutput());
    return out.build();
  } catch (IOException e) {
    throw new RuntimeException(
      "Serializing to a ByteString threw an IOException (should " +
      "never happen).", e);
  }
}

public byte[] toByteArray() {
  try {
    final byte[] result = new byte[getSerializedSize()];
    final CodedOutputStream output = CodedOutputStream.newInstance(result);
    writeTo(output);
    output.checkNoSpaceLeft();
    return result;
  } catch (IOException e) {
    throw new RuntimeException(
      "Serializing to a byte array threw an IOException " +
      "(should never happen).", e);
  }
}

回答by Chris Thompson

A ByteStringgives you the ability to perform more operations on the underlying data without having to copy the data into a new structure. For instance, if you wanted to provide a subset of bytesin a byte[]to another method, you would need to supply it with a start index and an end index. You can also concatenate ByteStringswithout having to create a new data structure and manually copy the data.

AByteString使您能够对基础数据执行更多操作,而无需将数据复制到新结构中。例如,如果您想将bytesin a的子集提供byte[]给另一个方法,则需要为其提供开始索引和结束索引。您还可以进行连接,ByteStrings而无需创建新的数据结构并手动复制数据。

However, with a ByteStringyou can give the method a subset of that data without the method knowing anything about the underlying storage. Just like a a substring of a normal String.

但是,使用 aByteString您可以为该方法提供该数据的子集,而该方法无需了解有关底层存储的任何信息。就像普通字符串的子字符串一样。

A String is for representing text and is nota good way to store binary data (as not all binary data has a textual equivalent unless you encode it in a manner that does: e.g. hex or Base64).

字符串用于表示文本,并不是存储二进制数据的好方法(因为并非所有二进制数据都具有文本等效项,除非您以某种方式对其进行编码:例如十六进制或 Base64)。