Java 类com.google.protobuf.ByteString.Output 实例源码

项目:vsminecraft    文件:ByteStringTest.java   
public void testNewOutput_ArrayWrite() throws IOException {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {128, 256, length / 2, length - 1, length, length + 1,
                       2 * length, 3 * length};
  int[] writeSizes = {1, 4, 5, 7, 23, bytes.length};

  for (int bufferSize : bufferSizes) {
    for (int writeSize : writeSizes) {
      // Test writing the entire output writeSize bytes at a time.
      ByteString.Output output = ByteString.newOutput(bufferSize);
      for (int i = 0; i < length; i += writeSize) {
        output.write(bytes, i, Math.min(writeSize, length - i));
      }
      ByteString byteString = output.toByteString();
      assertTrue("String built from newOutput() must contain the expected bytes",
          isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
    }
  }
}
项目:vsminecraft    文件:ByteStringTest.java   
public void testNewOutput_WriteChar() throws IOException {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};
  for (int bufferSize : bufferSizes) {
    ByteString.Output output = ByteString.newOutput(bufferSize);
    for (byte byteValue : bytes) {
      output.write(byteValue);
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_ArrayWrite() throws IOException {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {128, 256, length / 2, length - 1, length, length + 1,
                       2 * length, 3 * length};
  int[] writeSizes = {1, 4, 5, 7, 23, bytes.length};

  for (int bufferSize : bufferSizes) {
    for (int writeSize : writeSizes) {
      // Test writing the entire output writeSize bytes at a time.
      ByteString.Output output = ByteString.newOutput(bufferSize);
      for (int i = 0; i < length; i += writeSize) {
        output.write(bytes, i, Math.min(writeSize, length - i));
      }
      ByteString byteString = output.toByteString();
      assertTrue("String built from newOutput() must contain the expected bytes",
          isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
    }
  }
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_WriteChar() throws IOException {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};
  for (int bufferSize : bufferSizes) {
    ByteString.Output output = ByteString.newOutput(bufferSize);
    for (byte byteValue : bytes) {
      output.write(byteValue);
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_ArrayWrite() {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {128, 256, length / 2, length - 1, length, length + 1,
                       2 * length, 3 * length};
  int[] writeSizes = {1, 4, 5, 7, 23, bytes.length};

  for (int bufferSize : bufferSizes) {
    for (int writeSize : writeSizes) {
      // Test writing the entire output writeSize bytes at a time.
      ByteString.Output output = ByteString.newOutput(bufferSize);
      for (int i = 0; i < length; i += writeSize) {
        output.write(bytes, i, Math.min(writeSize, length - i));
      }
      ByteString byteString = output.toByteString();
      assertTrue("String built from newOutput() must contain the expected bytes",
          isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
    }
  }
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_WriteChar() {
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};
  for (int bufferSize : bufferSizes) {
    ByteString.Output output = ByteString.newOutput(bufferSize);
    for (byte byteValue : bytes) {
      output.write(byteValue);
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
项目:OpenYOLO-Android    文件:ProtoListUtil.java   
/**
 * Creates a {@link ByteString} by serializing the list of protos. Use
 * {@link #readMessageList(ByteString, Parser)} to deserialize.
 */
public static <T extends MessageLite> ByteString writeMessageList(List<T> protos) {
    Output output = ByteString.newOutput();
    try {
        writeMessageListTo(output, protos);
    } catch (IOException ex) {
        throw new IllegalStateException("Unable to write protobufs to memory");
    }

    return output.toByteString();
}
项目:vsminecraft    文件:ByteStringTest.java   
public void testNewOutput_InitialCapacity() throws IOException {
  byte[] bytes = getTestBytes();
  ByteString.Output output = ByteString.newOutput(bytes.length + 100);
  output.write(bytes);
  ByteString byteString = output.toByteString();
  assertTrue(
      "String built from newOutput(int) must contain the expected bytes",
      isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
}
项目:vsminecraft    文件:ByteStringTest.java   
public void testNewOutput_Mixed() throws IOException {
  Random rng = new Random(1);
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};

  for (int bufferSize : bufferSizes) {
    // Test writing the entire output using a mixture of write sizes and
    // methods;
    ByteString.Output output = ByteString.newOutput(bufferSize);
    int position = 0;
    while (position < bytes.length) {
      if (rng.nextBoolean()) {
        int count = 1 + rng.nextInt(bytes.length - position);
        output.write(bytes, position, count);
        position += count;
      } else {
        output.write(bytes[position]);
        position++;
      }
      assertEquals("size() returns the right value", position, output.size());
      assertTrue("newOutput() substring must have correct bytes",
          isArrayRange(output.toByteString().toByteArray(),
              bytes, 0, position));
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
项目:vsminecraft    文件:ByteStringTest.java   
public void testNewOutput_Mutating() throws IOException {
  Output os = ByteString.newOutput(5);
  os.write(new byte[] {1, 2, 3, 4, 5});
  EvilOutputStream eos = new EvilOutputStream();
  os.writeTo(eos);
  byte[] capturedArray = eos.capturedArray;
  ByteString byteString = os.toByteString();
  byte[] oldValue = byteString.toByteArray();
  Arrays.fill(capturedArray, (byte) 0);
  byte[] newValue = byteString.toByteArray();
  assertTrue("Output must not provide access to the underlying byte array",
      Arrays.equals(oldValue, newValue));
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_InitialCapacity() throws IOException {
  byte[] bytes = getTestBytes();
  ByteString.Output output = ByteString.newOutput(bytes.length + 100);
  output.write(bytes);
  ByteString byteString = output.toByteString();
  assertTrue(
      "String built from newOutput(int) must contain the expected bytes",
      isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_Mixed() throws IOException {
  Random rng = new Random(1);
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};

  for (int bufferSize : bufferSizes) {
    // Test writing the entire output using a mixture of write sizes and
    // methods;
    ByteString.Output output = ByteString.newOutput(bufferSize);
    int position = 0;
    while (position < bytes.length) {
      if (rng.nextBoolean()) {
        int count = 1 + rng.nextInt(bytes.length - position);
        output.write(bytes, position, count);
        position += count;
      } else {
        output.write(bytes[position]);
        position++;
      }
      assertEquals("size() returns the right value", position, output.size());
      assertTrue("newOutput() substring must have correct bytes",
          isArrayRange(output.toByteString().toByteArray(),
              bytes, 0, position));
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_Mutating() throws IOException {
  Output os = ByteString.newOutput(5);
  os.write(new byte[] {1, 2, 3, 4, 5});
  EvilOutputStream eos = new EvilOutputStream();
  os.writeTo(eos);
  byte[] capturedArray = eos.capturedArray;
  ByteString byteString = os.toByteString();
  byte[] oldValue = byteString.toByteArray();
  Arrays.fill(capturedArray, (byte) 0);
  byte[] newValue = byteString.toByteArray();
  assertTrue("Output must not provide access to the underlying byte array",
      Arrays.equals(oldValue, newValue));
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_InitialCapacity() throws IOException {
  byte[] bytes = getTestBytes();
  ByteString.Output output = ByteString.newOutput(bytes.length + 100);
  output.write(bytes);
  ByteString byteString = output.toByteString();
  assertTrue(
      "String built from newOutput(int) must contain the expected bytes",
      isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_Mixed() {
  Random rng = new Random(1);
  byte[] bytes = getTestBytes();
  int length = bytes.length;
  int[] bufferSizes = {0, 1, 128, 256, length / 2,
                       length - 1, length, length + 1,
                       2 * length, 3 * length};

  for (int bufferSize : bufferSizes) {
    // Test writing the entire output using a mixture of write sizes and
    // methods;
    ByteString.Output output = ByteString.newOutput(bufferSize);
    int position = 0;
    while (position < bytes.length) {
      if (rng.nextBoolean()) {
        int count = 1 + rng.nextInt(bytes.length - position);
        output.write(bytes, position, count);
        position += count;
      } else {
        output.write(bytes[position]);
        position++;
      }
      assertEquals("size() returns the right value", position, output.size());
      assertTrue("newOutput() substring must have correct bytes",
          isArrayRange(output.toByteString().toByteArray(),
              bytes, 0, position));
    }
    ByteString byteString = output.toByteString();
    assertTrue("String built from newOutput() must contain the expected bytes",
        isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length));
  }
}
项目:bazel    文件:ByteStringTest.java   
public void testNewOutput_Mutating() throws IOException {
  Output os = ByteString.newOutput(5);
  os.write(new byte[] {1, 2, 3, 4, 5});
  EvilOutputStream eos = new EvilOutputStream();
  os.writeTo(eos);
  byte[] capturedArray = eos.capturedArray;
  ByteString byteString = os.toByteString();
  byte[] oldValue = byteString.toByteArray();
  Arrays.fill(capturedArray, (byte) 0);
  byte[] newValue = byteString.toByteArray();
  assertTrue("Output must not provide access to the underlying byte array",
      Arrays.equals(oldValue, newValue));
}
项目:incubator-tez    文件:DagTypeConverters.java   
public static ByteString convertCredentialsToProto(Credentials credentials) {
  if (credentials == null) {
    return null;
  }
  Output output = ByteString.newOutput();
  DataOutputStream dos = new DataOutputStream(output);
  try {
    credentials.writeTokenStorageToStream(dos);
    return output.toByteString();
  } catch (IOException e) {
    throw new TezUncheckedException("Failed to serialize Credentials", e);
  }
}
项目:cloudata    文件:DynamodbDataStore.java   
public AttributeValue buildHashKey(T item) {
  boolean empty = true;

  Message.Builder key = newBuilder();
  for (AttributeMapping hashKeyField : hashKeyFields) {
    if (item.hasField(hashKeyField.field)) {
      Object value = item.getField(hashKeyField.field);
      key.setField(hashKeyField.field, value);
      empty = false;
    }
  }

  if (empty) {
    return null;
  }

  try {
    Output output = ByteString.newOutput();

    output.write(Ints.toByteArray(typeMetadata.getId()));
    key.buildPartial().writeTo(output);

    return toAttributeValue(output.toByteString());
  } catch (IOException e) {
    throw new IllegalArgumentException("Error building hash key", e);
  }
}
项目:cloudata    文件:DynamodbDataStore.java   
public RangeSpecifier buildRangeKey(T item) {
  try {
    boolean empty = true;
    boolean complete = true;

    Output output = ByteString.newOutput();

    Message.Builder key = newBuilder();
    for (AttributeMapping rangeKeyField : rangeKeyFields) {
      if (item.hasField(rangeKeyField.field)) {
        Object value = item.getField(rangeKeyField.field);
        key.setField(rangeKeyField.field, value);
        empty = false;
      } else {
        complete = false;
        break;
      }
    }

    if (empty) {
      return null;
    } else {
      key.buildPartial().writeTo(output);
      RangeSpecifier specifier = new RangeSpecifier();
      if (complete) {
        specifier.exact = toAttributeValue(output.toByteString());
      } else {
        specifier.prefix = toAttributeValue(output.toByteString());
      }
      return specifier;
    }
  } catch (IOException e) {
    throw new IllegalArgumentException("Error building range key", e);
  }
}
项目:tez    文件:DagTypeConverters.java   
public static ByteString convertCredentialsToProto(Credentials credentials) {
  if (credentials == null) {
    return null;
  }
  Output output = ByteString.newOutput();
  DataOutputStream dos = new DataOutputStream(output);
  try {
    credentials.writeTokenStorageToStream(dos);
    return output.toByteString();
  } catch (IOException e) {
    throw new TezUncheckedException("Failed to serialize Credentials", e);
  }
}