Java Code Examples for com.google.protobuf.ByteString.copyFrom()

The following are Jave code examples for showing how to use copyFrom() of the com.google.protobuf.ByteString class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: curiostack   File: ParseSupport.java   Source Code and License Vote up 6 votes
/** Parsers a bytes value out of the input. */
static ByteString parseBytes(JsonParser parser) throws IOException {
  JsonToken json = parser.currentToken();
  byte[] result = null;
  try {
    // Use Guava to decode base64, which can handle more variants than Jackson.
    result =
        BaseEncoding.base64()
            .decode(
                CharBuffer.wrap(
                    parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength()));
  } catch (IllegalArgumentException e) {
    // Fall through
  }
  if (result == null) {
    throw new InvalidProtocolBufferException("Not a bytes value: " + json);
  }
  return ByteString.copyFrom(result);
}
 
Example 2
Project: hadoop   File: PBHelper.java   Source Code and License Vote up 6 votes
public static SnapshotDiffReportEntryProto convert(DiffReportEntry entry) {
  if (entry == null) {
    return null;
  }
  ByteString sourcePath = ByteString
      .copyFrom(entry.getSourcePath() == null ? DFSUtil.EMPTY_BYTES : entry
          .getSourcePath());
  String modification = entry.getType().getLabel();
  SnapshotDiffReportEntryProto.Builder builder = SnapshotDiffReportEntryProto
      .newBuilder().setFullpath(sourcePath)
      .setModificationLabel(modification);
  if (entry.getType() == DiffType.RENAME) {
    ByteString targetPath = ByteString
        .copyFrom(entry.getTargetPath() == null ? DFSUtil.EMPTY_BYTES : entry
            .getTargetPath());
    builder.setTargetPath(targetPath);
  }
  return builder.build();
}
 
Example 3
Project: hashsdn-controller   File: SnapshotTrackerTest.java   Source Code and License Vote up 6 votes
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    data = new HashMap<>();
    data.put("key1", "value1");
    data.put("key2", "value2");
    data.put("key3", "value3");

    byteString = ByteString.copyFrom(SerializationUtils.serialize((Serializable) data));
    chunk1 = getNextChunk(byteString, 0, 10);
    chunk2 = getNextChunk(byteString, 10, 10);
    chunk3 = getNextChunk(byteString, 20, byteString.size());

    fbos = spy(new FileBackedOutputStream(100000000, "target"));
    FileBackedOutputStreamFactory mockFactory = mock(FileBackedOutputStreamFactory.class);
    doReturn(fbos).when(mockFactory).newInstance();
    doReturn(mockFactory).when(mockContext).getFileBackedOutputStreamFactory();
}
 
Example 4
Project: memory   File: Utf8Test.java   Source Code and License Vote up 6 votes
public void testThreeBytes() {
  // Travis' OOM killer doesn't like this test
  if (System.getenv("TRAVIS") == null) {
    int count = 0;
    int valid = 0;
    for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
      for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
        for (int k = Byte.MIN_VALUE; k <= Byte.MAX_VALUE; k++) {
          byte[] bytes = new byte[]{(byte) i, (byte) j, (byte) k};
          ByteString bs = ByteString.copyFrom(bytes);
          if (!bs.isValidUtf8()) {
            assertInvalid(bytes);
          } else {
            valid++;
          }
          count++;
          if ((count % 1000000L) == 0) {
            println("Processed " + (count / 1000000L) + " million characters");
          }
        }
      }
    }
    assertEquals(IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT, valid);
  }
}
 
Example 5
Project: sstore-soft   File: HStoreCoordinator.java   Source Code and License Vote up 5 votes
/**
 * Forward a StoredProcedureInvocation request to a remote site for execution
 * @param serializedRequest
 * @param callback
 * @param partition
 */
public void transactionRedirect(byte[] serializedRequest, RpcCallback<TransactionRedirectResponse> callback, int partition) {
    int dest_site_id = catalogContext.getSiteIdForPartitionId(partition);
    if (debug.val)
        LOG.debug(String.format("Redirecting transaction request to partition #%d on %s",
                  partition, HStoreThreadManager.formatSiteName(dest_site_id)));
    
    ByteString bs = ByteString.copyFrom(serializedRequest);
    TransactionRedirectRequest mr = TransactionRedirectRequest.newBuilder()
                                    .setSenderSite(this.local_site_id)
                                    .setWork(bs)
                                    .build();
    this.channels[dest_site_id].transactionRedirect(new ProtoRpcController(), mr, callback);
}
 
Example 6
Project: sstore-soft   File: TransactionRedirectResponseCallback.java   Source Code and License Vote up 5 votes
@Override
    public void run(ClientResponseImpl parameter) {
        if (debug.val)
            LOG.debug(String.format("Got ClientResponse callback for txn #%d! Sending back to %s",
                      parameter.getTransactionId(), HStoreThreadManager.formatSiteName(this.destSiteId)));
        FastSerializer fs = new FastSerializer();
        try {
            parameter.writeExternal(fs);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        ByteString bs = ByteString.copyFrom(fs.getBuffer());
        TransactionRedirectResponse response = TransactionRedirectResponse.newBuilder()
                                                              .setSenderSite(this.sourceSiteId)
                                                              .setOutput(bs)
                                                              .build();
        this.orig_callback.run(response);
        if (debug.val)
            LOG.debug(String.format("Sent back ClientResponse for txn #%d to %s [bytes=%d]",
                      parameter.getTransactionId(), HStoreThreadManager.formatSiteName(this.destSiteId),
                      bs.size()));
        
        // IMPORTANT: Since we're the only one that knows that we're finished (and actually even
        // cares), we need to be polite and clean-up after ourselves...
//        try {
//            this.finish();
//            hstore_site.getObjectPools().CALLBACKS_TXN_REDIRECT_RESPONSE.returnObject(this);
//        } catch (Exception ex) {
//            throw new RuntimeException("Funky failure", ex);
//        }
    }
 
Example 7
Project: sstore-soft   File: TestSerializableException.java   Source Code and License Vote up 5 votes
public void testSerializeToBuffer() throws Exception {
        SerializableException error = null;
        try {
            throw new NullPointerException();
        } catch (Exception ex) {
            error = new SerializableException(ex);
        }
        assertNotNull(error);
        
        int size = error.getSerializedSize();
        ByteBuffer b = ByteBuffer.allocate(size);
        error.serializeToBuffer(b);
        b.rewind();
        int expected = b.getInt();
        assertTrue(expected > 0);
        assertTrue(expected <= size);
        b.rewind();

        // Make sure we can still do this with a BufferPool
        DBBPool buffer_pool = new DBBPool(false, false);
        BBContainer bc = buffer_pool.acquire(size);
        error.serializeToBuffer(bc.b);
        bc.b.rewind();
        assertEquals(expected, bc.b.getInt());
        bc.b.rewind();
        
        ByteString bs = ByteString.copyFrom(b);
        ByteBuffer bs_b = bs.asReadOnlyByteBuffer();
//        System.err.println("NEW: " + StringUtil.md5sum(b));
        
        assertEquals(expected, bs_b.getInt());
    }
 
Example 8
Project: hadoop-oss   File: NuCypherExtPBHelperClient.java   Source Code and License Vote up 5 votes
public static ByteString getByteString(byte[] bytes) {
  // return singleton to reduce object allocation
  return (bytes.length == 0) ? ByteString.EMPTY : ByteString.copyFrom(bytes);
}
 
Example 9
Project: hashsdn-controller   File: AbstractRaftActorBehaviorTest.java   Source Code and License Vote up 5 votes
protected ByteString toByteString(final Map<String, String> state) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        oos.writeObject(state);
        return ByteString.copyFrom(bos.toByteArray());
    } catch (IOException e) {
        throw new AssertionError("IOException occurred converting Map to Bytestring", e);
    }
}
 
Example 10
Project: creacoinj   File: PaymentSessionTest.java   Source Code and License Vote up 5 votes
@Test
public void testSimplePayment() throws Exception {
    // Create a PaymentRequest and make sure the correct values are parsed by the PaymentSession.
    MockPaymentSession paymentSession = new MockPaymentSession(newSimplePaymentRequest("test"));
    assertEquals(paymentRequestMemo, paymentSession.getMemo());
    assertEquals(coin, paymentSession.getValue());
    assertEquals(simplePaymentUrl, paymentSession.getPaymentUrl());
    assertTrue(new Date(time * 1000L).equals(paymentSession.getDate()));
    assertTrue(paymentSession.getSendRequest().tx.equals(tx));
    assertFalse(paymentSession.isExpired());

    // Send the payment and verify that the correct information is sent.
    // Add a dummy input to tx so it is considered valid.
    tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes()));
    ArrayList<Transaction> txns = new ArrayList<>();
    txns.add(tx);
    Address refundAddr = new Address(PARAMS, serverKey.getPubKeyHash());
    paymentSession.sendPayment(txns, refundAddr, paymentMemo);
    assertEquals(1, paymentSession.getPaymentLog().size());
    assertEquals(simplePaymentUrl, paymentSession.getPaymentLog().get(0).getUrl().toString());
    Protos.Payment payment = paymentSession.getPaymentLog().get(0).getPayment();
    assertEquals(paymentMemo, payment.getMemo());
    assertEquals(merchantData, payment.getMerchantData());
    assertEquals(1, payment.getRefundToCount());
    assertEquals(coin.getValue(), payment.getRefundTo(0).getAmount());
    TransactionOutput refundOutput = new TransactionOutput(PARAMS, null, coin, refundAddr);
    ByteString refundScript = ByteString.copyFrom(refundOutput.getScriptBytes());
    assertTrue(refundScript.equals(payment.getRefundTo(0).getScript()));
}
 
Example 11
Project: memory   File: Utf8Test.java   Source Code and License Vote up 5 votes
@Test
public void testOneByte() {
  int valid = 0;
  for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
    ByteString bs = ByteString.copyFrom(new byte[] {(byte) i });
    if (!bs.isValidUtf8()) { //from -128 to -1
      assertInvalid(bs.toByteArray());
    } else {
      valid++; //from 0 to 127
    }
  }
  assertEquals(IsValidUtf8TestUtil.EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT, valid);
}
 
Example 12
Project: dremio-oss   File: PlanCaptureAttemptObserver.java   Source Code and License Vote up 5 votes
@Override
public void planCompleted(ExecutionPlan plan) {
  if (plan != null) {
    try {
      schema = RootSchemaFinder.getSchema(plan.getRootOperator(), funcRegistry);
    } catch (Exception e) {
      logger.warn("Failed to capture query output schema", e);
    }
  }

  accelerationDetails = ByteString.copyFrom(detailsPopulator.computeAcceleration());
}
 
Example 13
Project: tikv-client-lib-java   File: ComparablesTest.java   Source Code and License Vote up 5 votes
private void testBytes(byte[] lhs, byte[] rhs, Function<Integer, Boolean> tester) {
  ByteString lhsBS = ByteString.copyFrom(lhs);
  ByteString rhsBS = ByteString.copyFrom(rhs);

  Comparable lhsComp = Comparables.wrap(lhsBS);
  Comparable rhsComp = Comparables.wrap(rhsBS);

  assertTrue(tester.apply(lhsComp.compareTo(rhsComp)));

  lhsComp = Comparables.wrap(lhs);
  rhsComp = Comparables.wrap(rhs);

  assertTrue(tester.apply(lhsComp.compareTo(rhsComp)));
}
 
Example 14
Project: s-store   File: TestSerializableException.java   Source Code and License Vote up 5 votes
public void testSerializeToBuffer() throws Exception {
        SerializableException error = null;
        try {
            throw new NullPointerException();
        } catch (Exception ex) {
            error = new SerializableException(ex);
        }
        assertNotNull(error);
        
        int size = error.getSerializedSize();
        ByteBuffer b = ByteBuffer.allocate(size);
        error.serializeToBuffer(b);
        b.rewind();
        int expected = b.getInt();
        assertTrue(expected > 0);
        assertTrue(expected <= size);
        b.rewind();

        // Make sure we can still do this with a BufferPool
        DBBPool buffer_pool = new DBBPool(false, false);
        BBContainer bc = buffer_pool.acquire(size);
        error.serializeToBuffer(bc.b);
        bc.b.rewind();
        assertEquals(expected, bc.b.getInt());
        bc.b.rewind();
        
        ByteString bs = ByteString.copyFrom(b);
        ByteBuffer bs_b = bs.asReadOnlyByteBuffer();
//        System.err.println("NEW: " + StringUtil.md5sum(b));
        
        assertEquals(expected, bs_b.getInt());
    }
 
Example 15
Project: hadoop-oss   File: KeyRotationBC.java   Source Code and License Vote up 4 votes
public static ByteString getByteString(byte[] bytes) {
	// return singleton to reduce object allocation
	return (bytes.length == 0) ? ByteString.EMPTY : ByteString.copyFrom(bytes);
}
 
Example 16
Project: bazel-buildfarm   File: Chunker.java   Source Code and License Vote up 4 votes
/**
 * Returns the next {@link Chunk} or throws a {@link NoSuchElementException} if no data is left.
 *
 * <p>Always call {@link #hasNext()} before calling this method.
 *
 * <p>Zero byte inputs are treated special. Instead of throwing a {@link NoSuchElementException}
 * on the first call to {@link #next()}, a {@link Chunk} with an empty {@link ByteString} is
 * returned.
 */
public Chunk next() throws IOException {
  if (!hasNext()) {
    throw new NoSuchElementException();
  }

  maybeInitialize();

  if (digest.getSizeBytes() == 0) {
    data = null;
    return EMPTY_CHUNK;
  }

  // The cast to int is safe, because the return value is capped at chunkSize.
  int bytesToRead = (int) Math.min(bytesLeft(), chunkSize);
  if (bytesToRead == 0) {
    chunkCache = null;
    data = null;
    throw new NoSuchElementException();
  }

  if (chunkCache == null) {
    // Lazily allocate it in order to save memory on small data.
    // 1) bytesToRead < chunkSize: There will only ever be one next() call.
    // 2) bytesToRead == chunkSize: chunkCache will be set to its biggest possible value.
    // 3) bytestoRead > chunkSize: Not possible, due to Math.min above.
    chunkCache = new byte[bytesToRead];
  }

  long offsetBefore = offset;
  try {
    ByteStreams.readFully(data, chunkCache, 0, bytesToRead);
  } catch (EOFException e) {
    throw new IllegalStateException("Reached EOF, but expected "
        + bytesToRead + " bytes.", e);
  }
  offset += bytesToRead;

  ByteString blob = ByteString.copyFrom(chunkCache, 0, bytesToRead);

  if (bytesLeft() == 0) {
    data.close();
    data = null;
    chunkCache = null;
  }

  return new Chunk(digest, blob, offsetBefore);
}
 
Example 17
Project: seldon-core   File: JsonFormat.java   Source Code and License Vote up 4 votes
private ByteString parseBytes(JsonElement json) throws InvalidProtocolBufferException {
  return ByteString.copyFrom(BaseEncoding.base64().decode(json.getAsString()));
}
 
Example 18
Project: ditb   File: ByteStringer.java   Source Code and License Vote up 4 votes
/**
 * Wraps a byte array in a {@link ByteString} without copying it.
 */
public static ByteString wrap(final byte[] array) {
  return USE_ZEROCOPYBYTESTRING? HBaseZeroCopyByteString.wrap(array): ByteString.copyFrom(array);
}
 
Example 19
Project: creacoinj   File: WalletPasswordController.java   Source Code and License Vote up 4 votes
public static void setTargetTime(Duration targetTime) {
    ByteString bytes = ByteString.copyFrom(Longs.toByteArray(targetTime.toMillis()));
    Main.bitcoin.wallet().setTag(TAG, bytes);
}
 
Example 20
Project: generator-jhipster-grpc   File: _ProtobufMappers.java   Source Code and License Vote up 4 votes
public static ByteString byteBufferToByteString(ByteBuffer buffer) {
    return buffer == null ? null : ByteString.copyFrom(buffer);
}