Java Code Examples for com.google.protobuf.ByteString#concat()

The following examples show how to use com.google.protobuf.ByteString#concat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestServiceImpl.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a payload of desired type and size. Reads compressableBuffer or
 * uncompressableBuffer as a circular buffer.
 */
private ByteString generatePayload(ByteString dataBuffer, int offset, int size) {
  ByteString payload = ByteString.EMPTY;
  // This offset would never pass the array boundary.
  int begin = offset;
  int end = 0;
  int bytesLeft = size;
  while (bytesLeft > 0) {
    end = Math.min(begin + bytesLeft, dataBuffer.size());
    // ByteString.substring returns the substring from begin, inclusive, to end, exclusive.
    payload = payload.concat(dataBuffer.substring(begin, end));
    bytesLeft -= (end - begin);
    begin = end % dataBuffer.size();
  }
  return payload;
}
 
Example 2
Source File: TXUtil.java    From jelectrum with MIT License 6 votes vote down vote up
public static ByteString parseWitness(TransactionInput in)
{
  if (in.hasWitness())
  if (in.getWitness().getPushCount() == 2)
  if (in.getWitness().getPush(1).length == 33)
  {
    try (TimeRecordAuto tra = new TimeRecordAuto("txutil_parse_input_witness"))
    {
      ByteString h = ByteString.copyFrom( in.getWitness().getPush(1));
      h = Util.SHA256BIN(h);
      h = Util.RIPEMD160(h);
      ByteString prefix = HexUtil.hexStringToBytes("0014");

      ByteString out_script = prefix.concat(h);

      return Util.reverse(Util.SHA256BIN(out_script));
    }
 
  }


  return null;
}
 
Example 3
Source File: TestServiceImpl.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a payload of desired type and size. Reads compressableBuffer or
 * uncompressableBuffer as a circular buffer.
 */
private static ByteString generatePayload(ByteString dataBuffer, int offset, int size) {
    ByteString payload = ByteString.EMPTY;
    // This offset would never pass the array boundary.
    int begin = offset;
    int end = 0;
    int bytesLeft = size;
    while (bytesLeft > 0) {
        end = Math.min(begin + bytesLeft, dataBuffer.size());
        // ByteString.substring returns the substring from begin, inclusive, to end, exclusive.
        payload = payload.concat(dataBuffer.substring(begin, end));
        bytesLeft -= end - begin;
        begin = end % dataBuffer.size();
    }
    return payload;
}
 
Example 4
Source File: TransactionContext.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public ByteString signByteStrings(ByteString... bs) throws CryptoException, InvalidArgumentException {
    if (bs == null) {
        return null;
    }
    if (bs.length == 0) {
        return null;
    }
    if (bs.length == 1 && bs[0] == null) {
        return null;
    }

    ByteString f = bs[0];
    for (int i = 1; i < bs.length; ++i) {
        f = f.concat(bs[i]);

    }
    return ByteString.copyFrom(sign(f.toByteArray()));
}
 
Example 5
Source File: TestOptimisticByteOutput.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Test
public void testRopeByteString() throws Exception {
  ByteString literal = ByteString.copyFrom(smallData);

  ByteString data = literal;
  for (int i = 0; i < 3; i++) {
    data = data.concat(literal);
  }

  final byte[] expected;
  try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
    for (int i = 0; i < 4; i++) {
      baos.write(smallData);
    }
    expected = baos.toByteArray();
  }

  OptimisticByteOutput byteOutput = new OptimisticByteOutput(smallData.length * 4);
  UnsafeByteOperations.unsafeWriteTo(data, byteOutput);

  assertArrayEquals(expected, byteOutput.toByteArray());
}
 
Example 6
Source File: TestServiceImpl.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a payload of desired type and size. Reads compressableBuffer or
 * uncompressableBuffer as a circular buffer.
 */
private ByteString generatePayload(ByteString dataBuffer, int offset, int size) {
  ByteString payload = ByteString.EMPTY;
  // This offset would never pass the array boundary.
  int begin = offset;
  int end = 0;
  int bytesLeft = size;
  while (bytesLeft > 0) {
    end = Math.min(begin + bytesLeft, dataBuffer.size());
    // ByteString.substring returns the substring from begin, inclusive, to end, exclusive.
    payload = payload.concat(dataBuffer.substring(begin, end));
    bytesLeft -= (end - begin);
    begin = end % dataBuffer.size();
  }
  return payload;
}
 
Example 7
Source File: RangeSplitterTest.java    From tikv-client-lib-java with Apache License 2.0 5 votes vote down vote up
private static ByteString shiftByStatus(ByteString v, Status s) {
  switch (s) {
    case EQUAL:
      return v;
    case LESS:
      return v.substring(0, v.size() - 1);
    case GREATER:
      return v.concat(ByteString.copyFrom(new byte[] {1, 0}));
    default:
  	throw new IllegalArgumentException("Only EQUAL,LESS,GREATER allowed");
  }
}
 
Example 8
Source File: ByteStringQueueInputStreamTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Test
public void readSpanningChunksWithEmptyChunk() throws IOException {
  ByteString hello = copyFromUtf8("Hello, ");
  ByteString world = copyFromUtf8("World");
  InputStream in = newInput(of(hello, EMPTY, world));
  ByteString helloWorld = hello.concat(world);
  byte[] data = new byte[helloWorld.size()];

  assertThat(in.read(data)).isEqualTo(helloWorld.size());
  assertThat(ByteString.copyFrom(data)).isEqualTo(helloWorld);
  assertThat(in.read()).isEqualTo(-1);
}
 
Example 9
Source File: ByteStringQueueInputStreamTest.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Test
public void readSpanningChunks() throws IOException {
  ByteString hello = copyFromUtf8("Hello, ");
  ByteString world = copyFromUtf8("World");
  InputStream in = newInput(of(hello, world));
  ByteString helloWorld = hello.concat(world);
  byte[] data = new byte[helloWorld.size()];

  assertThat(in.read(data)).isEqualTo(helloWorld.size());
  assertThat(ByteString.copyFrom(data)).isEqualTo(helloWorld);
  assertThat(in.read()).isEqualTo(-1);
}
 
Example 10
Source File: FuseCAS.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
public synchronized void write(ByteString value, long offset) {
  int size = value.size();
  int contentSize = content.size();
  int index = (int) offset;
  if (index == contentSize) {
    if (size > 0) {
      if (contentSize == 0) {
        content = value;
      } else {
        concat(value);
      }
    }
  } else {
    /* eliminating EMPTY adds here - saw crashes to that effect */
    ByteString newContent = null;
    if (index > 0) { // pre
      if (index < contentSize) {
        newContent = content.substring(0, index);
      } else {
        newContent = content;

        if (index != contentSize) { // pad
          ByteString pad = ByteString.copyFrom(ByteBuffer.allocate(index - contentSize));
          newContent = newContent.concat(pad);
        }
      }
    }

    newContent = newContent == null ? value : newContent.concat(value);

    if (index + size < contentSize) { // post
      newContent = newContent.concat(content.substring(index + size));
    }

    content = newContent;
  }
}
 
Example 11
Source File: Duck32.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
private static void validateChecksum(String label, ByteString data, ByteString checksum)
  throws ValidationException
{
  ByteString expected = applyChecksum(label, data);
  ByteString have = data.concat(checksum);

  if (!expected.equals(have)) throw new ValidationException("Checksum mismatch");

}
 
Example 12
Source File: Duck32.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
private static ByteString applyChecksum(String label, ByteString data)
{
  MessageDigest md = DigestUtil.getMD();

  md.update(label.getBytes());
  md.update(data.toByteArray());
  byte[] hash = md.digest();

  ByteString checksum = ByteString.copyFrom(hash, 0, HASH_BYTES);

  return data.concat(checksum);
}
 
Example 13
Source File: RangeCacheTest.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompaction() throws Exception {
    KvClient kvc = client.getKvClient();

    Thread noise = new Thread() {
        @Override
        public void run() {
            ByteString n = bs("/tmp/noise-");
            ByteString k = n.concat(bs("0"));
            for (int i = 0; i < 1000; i++) {
                kvc.delete(k).sync();
                k = n.concat(bs("" + i));
                PutResponse pr = kvc.put(k, n).sync();
                if (i == 500) {
                    kvc.compact(pr.getHeader().getRevision(), false);
                }
            }
        }
    };
    
    try (RangeCache rc = new RangeCache(client, bs("tmp/"), false)) {

        rc.start().get(1L, TimeUnit.SECONDS);

                noise.start();
                noise.join();
    }


}
 
Example 14
Source File: KeyUtils.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
public static ByteString plusOne(ByteString key) {
    int max = key.size() - 1;
    if (max < 0) {
        return singleByte(1);
    }
    int lastPlusOne = key.byteAt(max) + 1;
    ByteString excludeLast = key.substring(0, max);
    return lastPlusOne == 0 ? plusOne(excludeLast)
            : excludeLast.concat(singleByte(lastPlusOne));
}
 
Example 15
Source File: KeyRangeUtils.java    From tikv-client-lib-java with Apache License 2.0 4 votes vote down vote up
public static List<Coprocessor.KeyRange> split(Coprocessor.KeyRange range, int splitFactor) {
  if (splitFactor > 32 || splitFactor <= 0 || (splitFactor & (splitFactor - 1)) != 0) {
    throw new TiClientInternalException(
        "splitFactor must be positive integer power of 2 and no greater than 16");
  }

  ByteString startKey = range.getStart();
  ByteString endKey = range.getEnd();
  // we don't cut infinite
  if (startKey.isEmpty() || endKey.isEmpty()) {
    return ImmutableList.of(range);
  }

  ImmutableList.Builder<Coprocessor.KeyRange> resultList = ImmutableList.builder();
  int maxSize = Math.max(startKey.size(), endKey.size());
  int i;

  for (i = 0; i < maxSize; i++) {
    byte sb = i < startKey.size() ? startKey.byteAt(i) : 0;
    byte eb = i < endKey.size() ? endKey.byteAt(i) : 0;
    if (sb != eb) {
      break;
    }
  }

  ByteString sRemaining = i < startKey.size() ? startKey.substring(i) : ByteString.EMPTY;
  ByteString eRemaining = i < endKey.size() ? endKey.substring(i) : ByteString.EMPTY;

  CodecDataInput cdi = new CodecDataInput(sRemaining);
  int uss = cdi.readPartialUnsignedShort();

  cdi = new CodecDataInput(eRemaining);
  int ues = cdi.readPartialUnsignedShort();

  int delta = (ues - uss) / splitFactor;
  if (delta <= 0) {
    return ImmutableList.of(range);
  }

  ByteString prefix = startKey.size() > endKey.size() ?
                      startKey.substring(0, i) : endKey.substring(0, i);
  ByteString newStartKey = startKey;
  ByteString newEndKey;
  for (int j = 0; j < splitFactor; j++) {
    uss += delta;
    if (j == splitFactor - 1) {
      newEndKey = endKey;
    } else {
      CodecDataOutput cdo = new CodecDataOutput();
      cdo.writeShort(uss);
      newEndKey = prefix.concat(cdo.toByteString());
    }
    resultList.add(makeCoprocRange(newStartKey, newEndKey));
    newStartKey = newEndKey;
  }

  return resultList.build();
}
 
Example 16
Source File: KeyUtils.java    From tikv-client-lib-java with Apache License 2.0 4 votes vote down vote up
public static ByteString getNextKeyInByteOrder(ByteString key) {
  return key.concat(ZERO_BYTE);
}
 
Example 17
Source File: GetUTXOUtil.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
private static void descend(
  ByteString prefix, 
  ByteString search, 
  UserServiceBlockingStub stub,
  List<TransactionBridge> bridges,
  Map<ByteString, TrieNode> node_map,
  ByteString expected_hash,
  ByteString utxo_root)
  throws ValidationException
{
  if (prefix.size() > search.size())
  {
    if (!node_map.containsKey(prefix))
    {
      logger.log(Level.FINE,"Doing additional scan into " + HexUtil.getHexString(prefix) + ".");
      for(TrieNode n : getNodesByPrefix(prefix, stub, false, utxo_root))
      {
        node_map.put(n.getPrefix(), n);
      }
    }
  }

  if (!node_map.containsKey(prefix))
  {
    throw new ValidationException("No node at prefix: " + HexUtil.getHexString(prefix) + ".");
  }

  TrieNode node = node_map.get(prefix);
  if (!node.getHash().equals(expected_hash))
  {
    throw new ValidationException("Hash mismatch at prefix: " + HexUtil.getHexString(prefix) + ".");
  }
  if (node.getIsLeaf())
  {
    bridges.add(new TransactionBridge(node));
  }
  
  for(ChildEntry ce : node.getChildrenList())
  {
    ByteString next = prefix.concat(ce.getKey());
    if (next.size() <= search.size())
    {
      if (search.startsWith(next))
      {
        descend(next, search, stub, bridges, node_map, ce.getHash(), utxo_root);
      }
    }
    if (next.size() > search.size())
    {
      if (next.startsWith(search))
      {
        descend(next, search, stub, bridges, node_map, ce.getHash(), utxo_root);
      }
    }
  }

}
 
Example 18
Source File: ContractFunctionParams.java    From hedera-sdk-java with Apache License 2.0 4 votes vote down vote up
static ByteString rightPad32(ByteString input) {
    int rem = 32 - input.size() % 32;
    return rem == 32 ? input : input.concat(padding.substring(0, rem));
}
 
Example 19
Source File: TransactionContext.java    From fabric-sdk-java with Apache License 2.0 3 votes vote down vote up
public TransactionContext(Channel channel, User user, CryptoSuite cryptoPrimitives) {

        this.user = user;
        this.channel = channel;
        //TODO clean up when public classes are interfaces.
        this.verify = !"".equals(channel.getName());  //if name is not blank not system channel and need verify.

        //  this.txID = transactionID;
        this.cryptoPrimitives = cryptoPrimitives;

        // Get the signing identity from the user
        this.signingIdentity = IdentityFactory.getSigningIdentity(cryptoPrimitives, user);

        // Serialize signingIdentity
        this.identity = signingIdentity.createSerializedIdentity();

        ByteString no = getNonce();

        ByteString comp = no.concat(identity.toByteString());

        byte[] txh = cryptoPrimitives.hash(comp.toByteArray());

        //    txID = Hex.encodeHexString(txh);
        txID = new String(Utils.toHexString(txh));
        toString = "TransactionContext{ txID: " + txID + ", mspid: " + user.getMspId() + ", user: " + user.getName() + "}";

    }