com.google.protobuf.Internal.isValidUtf8()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中com.google.protobuf.Internal.isValidUtf8()方法的一些代码示例,展示了Internal.isValidUtf8()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Internal.isValidUtf8()方法的具体详情如下:
包路径:com.google.protobuf.Internal
类名称:Internal
方法名:isValidUtf8

Internal.isValidUtf8介绍

[英]Helper called by generated code to determine if a byte array is a valid UTF-8 encoded string such that the original bytes can be converted to a String object and then back to a byte array round tripping the bytes without loss. More precisely, returns true whenever:

Arrays.equals(byteString.toByteArray(),

This method rejects "overlong" byte sequences, as well as 3-byte sequences that would map to a surrogate character, in accordance with the restricted definition of UTF-8 introduced in Unicode 3.1. Note that the UTF-8 decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but currently (2011) still accepts 3-byte surrogate character byte sequences.

See the Unicode Standard,
Table 3-6. UTF-8 Bit Distribution,
Table 3-7. Well Formed UTF-8 Byte Sequences.

As of 2011-02, this method simply returns the result of ByteString#isValidUtf8(). Calling that method directly is preferred.
[中]由生成的代码调用的Helper,用于确定字节数组是否是有效的UTF-8编码字符串,以便可以将原始字节转换为字符串对象,然后返回到字节数组,从而在不丢失字节的情况下来回转换字节。更准确地说,只要:

Arrays.equals(byteString.toByteArray(),

根据Unicode 3.1中引入的UTF-8的受限定义,该方法拒绝“过长”字节序列以及映射到代理字符的3字节序列。请注意,Oracle JDK中包含的UTF-8解码器已被修改为也拒绝“过长”字节序列,但目前(2011年)仍接受3字节代理字符字节序列。
参见Unicode标准,
表3-6UTF-8位分布
表3-7格式良好的UTF-8字节序列
自2011-02年起,此方法仅返回ByteString#isValidUtf8()的结果。最好直接调用该方法。

代码示例

代码示例来源:origin: com.google.protobuf/protobuf-java

@Override
public String get(int index) {
 Object o = list.get(index);
 if (o instanceof String) {
  return (String) o;
 } else if (o instanceof ByteString) {
  ByteString bs = (ByteString) o;
  String s = bs.toStringUtf8();
  if (bs.isValidUtf8()) {
   list.set(index, s);
  }
  return s;
 } else {
  byte[] ba = (byte[]) o;
  String s = Internal.toStringUtf8(ba);
  if (Internal.isValidUtf8(ba)) {
   list.set(index, s);
  }
  return s;
 }
}

代码示例来源:origin: pl.edu.icm.coansys.citations/core

public String getPages() {
  java.lang.Object ref = pages_;
  if (ref instanceof String) {
    return (String) ref;
  } else {
    com.google.protobuf.ByteString bs =
        (com.google.protobuf.ByteString) ref;
    String s = bs.toStringUtf8();
    if (com.google.protobuf.Internal.isValidUtf8(bs)) {
      pages_ = s;
    }
    return s;
  }
}

代码示例来源:origin: pl.edu.icm.coansys.citations/core

public String getKey() {
  java.lang.Object ref = key_;
  if (ref instanceof String) {
    return (String) ref;
  } else {
    com.google.protobuf.ByteString bs =
        (com.google.protobuf.ByteString) ref;
    String s = bs.toStringUtf8();
    if (com.google.protobuf.Internal.isValidUtf8(bs)) {
      key_ = s;
    }
    return s;
  }
}

代码示例来源:origin: com.senseidb/sensei-core

public String getClassname() {
 Object ref = classname_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   classname_ = s;
  }
  return s;
 }
}

代码示例来源:origin: com.senseidb/sensei-core

public String getClassname() {
 Object ref = classname_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   classname_ = s;
  }
  return s;
 }
}

代码示例来源:origin: com.opera/operadriver

public String getType() {
 java.lang.Object ref = type_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   type_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getTypeBytes() {

代码示例来源:origin: com.opera/operadriver

public String getNamePrefix() {
 java.lang.Object ref = namePrefix_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   namePrefix_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getNamePrefixBytes() {

代码示例来源:origin: com.opera/operadriver

public String getName() {
 java.lang.Object ref = name_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   name_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getNameBytes() {

代码示例来源:origin: com.opera/operadriver

public String getValue() {
 java.lang.Object ref = value_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   value_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getValueBytes() {

代码示例来源:origin: com.opera/operadriver

public String getEventType() {
 java.lang.Object ref = eventType_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   eventType_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getEventTypeBytes() {

代码示例来源:origin: com.opera/operadriver

public String getContext() {
 java.lang.Object ref = context_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   context_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getContextBytes() {

代码示例来源:origin: com.opera/operadriver

public String getType() {
 java.lang.Object ref = type_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   type_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getTypeBytes() {

代码示例来源:origin: com.opera/operadriver

public String getEventType() {
 java.lang.Object ref = eventType_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   eventType_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getEventTypeBytes() {

代码示例来源:origin: com.opera/operadriver

public String getValue() {
 java.lang.Object ref = value_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   value_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getValueBytes() {

代码示例来源:origin: com.opera/operadriver

public String getExcludePattern() {
 java.lang.Object ref = excludePattern_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   excludePattern_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getExcludePatternBytes() {

代码示例来源:origin: com.opera/operadriver

public String getDomain() {
 java.lang.Object ref = domain_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   domain_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getDomainBytes() {

代码示例来源:origin: com.opera/operadriver

public String getStr() {
 java.lang.Object ref = str_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   str_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getStrBytes() {

代码示例来源:origin: com.opera/operadriver

public String getHref() {
 java.lang.Object ref = href_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   href_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getHrefBytes() {

代码示例来源:origin: com.opera/operadriver

public String getName() {
 java.lang.Object ref = name_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   name_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getNameBytes() {

代码示例来源:origin: com.opera/operadriver

public String getValue() {
 java.lang.Object ref = value_;
 if (ref instanceof String) {
  return (String) ref;
 } else {
  com.google.protobuf.ByteString bs = 
    (com.google.protobuf.ByteString) ref;
  String s = bs.toStringUtf8();
  if (com.google.protobuf.Internal.isValidUtf8(bs)) {
   value_ = s;
  }
  return s;
 }
}
private com.google.protobuf.ByteString getValueBytes() {

相关文章