com.google.common.base.Utf8.encodedLength()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(209)

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

Utf8.encodedLength介绍

[英]Returns the number of bytes in the UTF-8-encoded form of sequence. For a string, this method is equivalent to string.getBytes(UTF_8).length, but is more efficient in both time and space.
[中]返回UTF-8编码形式的序列字节数。对于字符串,此方法等效于字符串。getBytes(UTF_8)。长度,但在时间和空间上都更有效。

代码示例

代码示例来源:origin: google/error-prone

private boolean isValidTag(String tag) {
 return Utf8.encodedLength(tag) <= 23;
}

代码示例来源:origin: springside/springside4

/**
   * 计算字符串被UTF8编码后的字节数 via guava
   * 
   * @see Utf8#encodedLength(CharSequence)
   */
  public static int utf8EncodedLength(@Nullable CharSequence sequence) {
    if (StringUtils.isEmpty(sequence)) {
      return 0;
    }
    return Utf8.encodedLength(sequence);
  }
}

代码示例来源:origin: google/guava

public void testEncodedLength_validStrings() {
 assertEquals(0, Utf8.encodedLength(""));
 assertEquals(11, Utf8.encodedLength("Hello world"));
 assertEquals(8, Utf8.encodedLength("Résumé"));
 assertEquals(
   461,
   Utf8.encodedLength(
     "威廉·莎士比亞(William Shakespeare,"
       + "1564年4月26號—1616年4月23號[1])係隻英國嗰演員、劇作家同詩人,"
       + "有時間佢簡稱莎翁;中國清末民初哈拕翻譯做舌克斯毕、沙斯皮耳、筛斯比耳、"
       + "莎基斯庇尔、索士比尔、夏克思芘尔、希哀苦皮阿、叶斯壁、沙克皮尔、"
       + "狹斯丕爾。[2]莎士比亞編寫過好多作品,佢嗰劇作響西洋文學好有影響,"
       + "哈都拕人翻譯做好多話。"));
 // A surrogate pair
 assertEquals(4, Utf8.encodedLength(newString(MIN_HIGH_SURROGATE, MIN_LOW_SURROGATE)));
}

代码示例来源:origin: google/guava

public void testEncodedLength_validStrings2() {
 HashMap<Integer, Integer> utf8Lengths = new HashMap<>();
 utf8Lengths.put(0x00, 1);
 utf8Lengths.put(0x7f, 1);
 utf8Lengths.put(0x80, 2);
 utf8Lengths.put(0x7ff, 2);
 utf8Lengths.put(0x800, 3);
 utf8Lengths.put(MIN_SUPPLEMENTARY_CODE_POINT - 1, 3);
 utf8Lengths.put(MIN_SUPPLEMENTARY_CODE_POINT, 4);
 utf8Lengths.put(MAX_CODE_POINT, 4);
 Integer[] codePoints = utf8Lengths.keySet().toArray(new Integer[] {});
 StringBuilder sb = new StringBuilder();
 Random rnd = new Random();
 for (int trial = 0; trial < 100; trial++) {
  sb.setLength(0);
  int utf8Length = 0;
  for (int i = 0; i < 6; i++) {
   Integer randomCodePoint = codePoints[rnd.nextInt(codePoints.length)];
   sb.appendCodePoint(randomCodePoint);
   utf8Length += utf8Lengths.get(randomCodePoint);
   if (utf8Length != Utf8.encodedLength(sb)) {
    StringBuilder repro = new StringBuilder();
    for (int j = 0; j < sb.length(); j++) {
     repro.append(" ").append((int) sb.charAt(j)); // GWT compatible
    }
    assertEquals(repro.toString(), utf8Length, Utf8.encodedLength(sb));
   }
  }
 }
}

代码示例来源:origin: google/guava

private static void testEncodedLengthFails(String invalidString, int invalidCodePointIndex) {
 try {
  Utf8.encodedLength(invalidString);
  fail();
 } catch (IllegalArgumentException expected) {
  assertThat(expected)
    .hasMessageThat()
    .isEqualTo("Unpaired surrogate at index " + invalidCodePointIndex);
 }
}

代码示例来源:origin: org.apache.bookkeeper/stream-storage-common

@Override
public int getSerializedSize(String value) {
  return Utf8.encodedLength(value);
}

代码示例来源:origin: com.google.errorprone/error_prone_core

private boolean isValidTag(String tag) {
 return Utf8.encodedLength(tag) <= 23;
}

代码示例来源:origin: google/closure-templates

/** Returns an {@link Expression} that can load the given String constant. */
public static Expression constant(final String value) {
 checkNotNull(value);
 checkArgument(
   Utf8.encodedLength(value) <= MAX_CONSTANT_STRING_LENGTH,
   "String is too long when encoded in utf8");
 return stringConstant(value);
}

代码示例来源:origin: xuminwlt/j360-dubbo-app-all

/**
   * 计算字符串被UTF8编码后的字节数 via guava
   * 
   * @see Utf8#encodedLength(CharSequence)
   */
  public static int utf8EncodedLength(@Nullable CharSequence sequence) {
    if (StringUtils.isEmpty(sequence)) {
      return 0;
    }
    return Utf8.encodedLength(sequence);
  }
}

代码示例来源:origin: io.springside/springside-utils

/**
   * 计算字符串被UTF8编码后的字节数 via guava
   * 
   * @see Utf8#encodedLength(CharSequence)
   */
  public static int utf8EncodedLength(@Nullable CharSequence sequence) {
    if (StringUtils.isEmpty(sequence)) {
      return 0;
    }
    return Utf8.encodedLength(sequence);
  }
}

代码示例来源:origin: DarLiner/vjtools

/**
   * 计算字符串被UTF8编码后的字节数 via guava
   * 
   * @see Utf8#encodedLength(CharSequence)
   */
  public static int utf8EncodedLength(@Nullable CharSequence sequence) {
    if (StringUtils.isEmpty(sequence)) {
      return 0;
    }
    return Utf8.encodedLength(sequence);
  }
}

代码示例来源:origin: com.google.template/soy

/** Returns an {@link Expression} that can load the given String constant. */
public static Expression constant(final String value) {
 checkNotNull(value);
 checkArgument(
   Utf8.encodedLength(value) <= MAX_CONSTANT_STRING_LENGTH,
   "String is too long when encoded in utf8");
 return stringConstant(value);
}

代码示例来源:origin: com.google.guava/guava-tests

public void testEncodedLength_validStrings() {
 assertEquals(0, Utf8.encodedLength(""));
 assertEquals(11, Utf8.encodedLength("Hello world"));
 assertEquals(8, Utf8.encodedLength("Résumé"));
 assertEquals(461, Utf8.encodedLength("威廉·莎士比亞(William Shakespeare,"
   + "1564年4月26號—1616年4月23號[1])係隻英國嗰演員、劇作家同詩人,"
   + "有時間佢簡稱莎翁;中國清末民初哈拕翻譯做舌克斯毕、沙斯皮耳、筛斯比耳、"
   + "莎基斯庇尔、索士比尔、夏克思芘尔、希哀苦皮阿、叶斯壁、沙克皮尔、"
   + "狹斯丕爾。[2]莎士比亞編寫過好多作品,佢嗰劇作響西洋文學好有影響,"
   + "哈都拕人翻譯做好多話。"));
 // A surrogate pair
 assertEquals(4, Utf8.encodedLength(newString(MIN_HIGH_SURROGATE, MIN_LOW_SURROGATE)));
}

代码示例来源:origin: mqtt-bee/mqtt-bee

private static @NotNull Iterable<Arguments> stringWithGivenLengthProvider(final int length) {
  final List<Arguments> arguments = new ArrayList<>();
  final char[] chars = new char[length];
  Arrays.fill(chars, 'a'); // each 'a' encoded in UTF-8 is one byte
  final String maxStringOfOneByteChars = new String(chars);
  // only characters with a one byte representation in UTF-8, i.e. U+0000 to U+007F
  arguments.add(Arguments.of(maxStringOfOneByteChars, 2 + Utf8.encodedLength(maxStringOfOneByteChars)));
  // include character with two byte representation in UTF-8, i.e. U+0080 to U+07FF
  final String maxStringIncludingTwoByteChar = '\u0080' + maxStringOfOneByteChars.substring(2);
  arguments.add(
      Arguments.of(maxStringIncludingTwoByteChar, 2 + Utf8.encodedLength(maxStringIncludingTwoByteChar)));
  // include character with three byte representation in UTF-8, i.e. U+0800 to U+FFFF
  final String maxStringIncludingThreeByteChar = '\u0800' + maxStringOfOneByteChars.substring(3);
  arguments.add(
      Arguments.of(maxStringIncludingThreeByteChar, 2 + Utf8.encodedLength(maxStringIncludingThreeByteChar)));
  return arguments;
}

代码示例来源:origin: com.google.guava/guava-tests

private static void testEncodedLengthFails(String invalidString, int invalidCodePointIndex) {
 try {
  Utf8.encodedLength(invalidString);
  fail();
 } catch (IllegalArgumentException expected) {
  assertThat(expected).hasMessage("Unpaired surrogate at index " + invalidCodePointIndex);
 }
}

代码示例来源:origin: logzio/sawmill

@Override
public ProcessResult process(Doc doc) {
  String sourceAsJsonString = JsonUtils.toJsonString(doc.getSource());
  doc.addField(targetField, Utf8.encodedLength(sourceAsJsonString));
  return ProcessResult.success();
}

代码示例来源:origin: georocket/georocket

/**
  * Make chunk and meta data
  * @param pos the position of the end of the chunk to create
  * @return the chunk and meta data
  */
 protected Result<JsonChunkMeta> makeResult(int pos) {
  resultsCreated = true;
  String chunk = window.getChars(mark, pos);
  window.advanceTo(pos);
  JsonChunkMeta meta = new JsonChunkMeta(lastFieldName, 0, Utf8.encodedLength(chunk));
  return new Result<>(chunk, meta);
 }
}

代码示例来源:origin: google/closure-templates

/**
 * Returns an {@link Expression} that can load the given String constant.
 *
 * <p>Unlike {@link #constant(String)} this can handle strings larger than 65K bytes.
 */
public static Expression constant(String value, ClassFieldManager manager) {
 int encodedLength = Utf8.encodedLength(value);
 if (encodedLength <= MAX_CONSTANT_STRING_LENGTH) {
  return stringConstant(value);
 }
 // else it is too big for a single constant pool entry so split it into a small number of
 // entries and generate a static final field to hold the cat'ed value.
 int startIndex = 0;
 Expression stringExpression = null;
 int length = value.length();
 do {
  int endIndex = offsetOf65KUtf8Bytes(value, startIndex, length);
  // N.B. we may end up splitting the string at a surrogate pair, but the class format uses
  // modified utf8 which is forgiving about such things.
  Expression substringConstant = stringConstant(value.substring(startIndex, endIndex));
  startIndex = endIndex;
  if (stringExpression == null) {
   stringExpression = substringConstant;
  } else {
   stringExpression = stringExpression.invoke(MethodRef.STRING_CONCAT, substringConstant);
  }
 } while (startIndex < length);
 FieldRef fieldRef = manager.addStaticField(LARGE_STRING_CONSTANT_NAME, stringExpression);
 return fieldRef.accessor();
}

代码示例来源:origin: com.google.template/soy

/**
 * Returns an {@link Expression} that can load the given String constant.
 *
 * <p>Unlike {@link #constant(String)} this can handle strings larger than 65K bytes.
 */
public static Expression constant(String value, ClassFieldManager manager) {
 int encodedLength = Utf8.encodedLength(value);
 if (encodedLength <= MAX_CONSTANT_STRING_LENGTH) {
  return stringConstant(value);
 }
 // else it is too big for a single constant pool entry so split it into a small number of
 // entries and generate a static final field to hold the cat'ed value.
 int startIndex = 0;
 Expression stringExpression = null;
 int length = value.length();
 do {
  int endIndex = offsetOf65KUtf8Bytes(value, startIndex, length);
  // N.B. we may end up splitting the string at a surrogate pair, but the class format uses
  // modified utf8 which is forgiving about such things.
  Expression substringConstant = stringConstant(value.substring(startIndex, endIndex));
  startIndex = endIndex;
  if (stringExpression == null) {
   stringExpression = substringConstant;
  } else {
   stringExpression = stringExpression.invoke(MethodRef.STRING_CONCAT, substringConstant);
  }
 } while (startIndex < length);
 FieldRef fieldRef = manager.addStaticField(LARGE_STRING_CONSTANT_NAME, stringExpression);
 return fieldRef.accessor();
}

代码示例来源:origin: mqtt-bee/mqtt-bee

@Test
void encodedLength_fromString() {
  final String simple = "abcdef";
  final int expectedLength = 2 + Utf8.encodedLength(simple);
  final MqttUtf8StringImpl string = MqttUtf8StringImpl.of(simple);
  assertNotNull(string);
  assertEquals(expectedLength, string.encodedLength());
}

相关文章