Google Protobuf ByteString vs. Byte [] [英] Google Protobuf ByteString vs. Byte[]

查看:645
本文介绍了Google Protobuf ByteString vs. Byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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如何与String或byte []不同。
有人可以解释一下吗?
谢谢。

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

推荐答案

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

You can think of ByteString as 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 因为 String 不适合表示任意字节序列。 String 专门用于字符数据。

ByteString exists because String is not suitable for representing arbitrary sequences of bytes. String is specifically for character data.


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

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?

排序。如果你调用 toByteArray(),你将获得与调用 toByteString()。toByteArray()。比较两种方法的实现,在 AbstractMessageLite

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:

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);
  }
}

这篇关于Google Protobuf ByteString vs. Byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆