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

The following examples show how to use com.google.protobuf.ByteString#size() . 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 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 2
Source File: Slopbucket.java    From jelectrum with MIT License 6 votes vote down vote up
public synchronized void addListEntry(String trough_name, ByteString key, ByteString value)
{
  long trough_pos = getTroughPtr(trough_name);
  RecordEntry prev_entry = getKeyValueTable(trough_pos, key);
  long prev_location = 0;
  if (prev_entry != null)
  {
    prev_location = prev_entry.getDataLoc();
  }

  byte[] new_data_buff = new byte[8 + value.size()];

  ByteBuffer bb = ByteBuffer.wrap(new_data_buff);
  bb.putLong(prev_location);
  value.copyTo(new_data_buff, 8);
  ByteString new_data = ByteString.copyFrom(new_data_buff);

  RecordEntry re = new RecordEntry(key, new_data);
  re.storeItem(0L);


  putKeyValueTable(trough_pos, re);
  
}
 
Example 3
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 4
Source File: Retro.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Get the current tenant ID from the baggage, if one is being propagated
 * 
 * @return the tenant ID being propagated in the baggage, or 0 if no tenant
 *         is being propagated
 */
public static int getTenant() {
    if (BaggageContents.contains(RETRO_BAGGAGE_NAMESPACE, TENANT_ID_BAGGAGE_FIELD)) {
        Set<ByteString> tenantIds = BaggageContents.get(RETRO_BAGGAGE_NAMESPACE, TENANT_ID_BAGGAGE_FIELD);
        if (tenantIds.size() == 1) {
            ByteString tenantId = tenantIds.iterator().next();
            if (tenantId.size() == 4) {
                return tenantId.asReadOnlyByteBuffer().getInt();
            }
            log.warn("Expected 4-byte tenantID, actually got {} bytes: {}", tenantId.size(), tenantId);
        }
        if (tenantIds.size() > 1) {
            log.warn("Execution has {} tenant IDs", tenantIds.size());
        }

        // Remove erroneous tenant ID value
        BaggageContents.remove(RETRO_BAGGAGE_NAMESPACE, TENANT_ID_BAGGAGE_FIELD);
    }
    return 0;
}
 
Example 5
Source File: AdxBidRequest.java    From XRTB with Apache License 2.0 6 votes vote down vote up
/**
 * Convert IP address to dotted decimal (ipv4) and coloned decimal (ipv6)
 * 
 * @param ip
 *            ByteString. The bytes to decode
 * @return String. The ip address form of the byte stream.
 */
protected static String convertIp(ByteString ip) {
	ByteBuffer b = ip.asReadOnlyByteBuffer();
	StringBuilder sb = new StringBuilder();
	if (ip.size() == 3) {
		for (int i = 0; i < ip.size(); i++) {
			sb.append(Integer.toUnsignedString(0xff & b.get(i)));
			sb.append(".");
		}
		sb.append("0");
	} else {
		for (int i = 0; i < ip.size(); i++) {
			sb.append(Integer.toUnsignedString(0xff & b.get(i)));
			sb.append(":");
		}
		sb.append("0");
	}
	return sb.toString();
}
 
Example 6
Source File: Util.java    From jetcd with Apache License 2.0 6 votes vote down vote up
public static ByteString prefixNamespaceToRangeEnd(ByteString end, ByteSequence namespace) {
    if (namespace.isEmpty()) {
        return end;
    }

    if (end.size() == 1 && end.toByteArray()[0] == 0) {
        // range end is '\0', calculate the prefixed range end by (key + 1)
        byte[] prefixedEndArray = namespace.getByteString().toByteArray();
        boolean ok = false;
        for (int i = (prefixedEndArray.length - 1); i >= 0; i--) {
            prefixedEndArray[i] = (byte) (prefixedEndArray[i] + 1);
            if (prefixedEndArray[i] != 0) {
                ok = true;
                break;
            }
        }
        if (!ok) {
            // 0xff..ff => 0x00
            prefixedEndArray = new byte[] { 0 };
        }
        return ByteString.copyFrom(prefixedEndArray);
    } else {
        return namespace.getByteString().concat(end);
    }
}
 
Example 7
Source File: ShardWorkerContext.java    From bazel-buildfarm with Apache License 2.0 6 votes vote down vote up
private void updateActionResultStdOutputs(ActionResult.Builder resultBuilder)
    throws InterruptedException {
  ByteString stdoutRaw = resultBuilder.getStdoutRaw();
  if (stdoutRaw.size() > 0) {
    // reset to allow policy to determine inlining
    resultBuilder.setStdoutRaw(ByteString.EMPTY);
    Digest stdoutDigest = getDigestUtil().compute(stdoutRaw);
    insertBlob(stdoutDigest, stdoutRaw);
    resultBuilder.setStdoutDigest(stdoutDigest);
  }

  ByteString stderrRaw = resultBuilder.getStderrRaw();
  if (stderrRaw.size() > 0) {
    // reset to allow policy to determine inlining
    resultBuilder.setStderrRaw(ByteString.EMPTY);
    Digest stderrDigest = getDigestUtil().compute(stderrRaw);
    insertBlob(stderrDigest, stderrRaw);
    resultBuilder.setStderrDigest(stderrDigest);
  }
}
 
Example 8
Source File: GrpcCAS.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Override
public Blob get(Digest digest) {
  try (InputStream in = newStreamInput(getBlobName(digest), /* offset=*/ 0)) {
    ByteString content = ByteString.readFrom(in);
    if (content.size() != digest.getSizeBytes()) {
      throw new IOException(
          String.format(
              "size/data mismatch: was %d, expected %d", content.size(), digest.getSizeBytes()));
    }
    return new Blob(content, digest);
  } catch (IOException ex) {
    expire(digest);
    return null;
  }
}
 
Example 9
Source File: TestOptimisticByteOutputUnsafeByteOperations.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsafeByteOperations() throws IOException {
  // Arrange
  final ByteString bytes = ByteString.copyFrom(data);
  final OptimisticByteOutput byteOutput = new OptimisticByteOutput(bytes.size());

  // Act
  UnsafeByteOperations.unsafeWriteTo(bytes, byteOutput);

  // Assert
  assertNotSame(data, byteOutput.toByteArray());
  assertArrayEquals(data, byteOutput.toByteArray());
}
 
Example 10
Source File: FileBlock.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public static FileBlock newInstance(final String type, final ByteString blob, final ByteString indexdata) {
	if (blob != null && blob.size() > MAX_BODY_SIZE / 2) {
		System.err.println("Warning: Fileblock has body size too large and may be considered corrupt");
		if (blob.size() > MAX_BODY_SIZE - 1024 * 1024) {
			throw new Error("This file has too many entities in a block. Parsers will reject it.");
		}
	}
	if (indexdata != null && indexdata.size() > MAX_HEADER_SIZE / 2) {
		System.err.println("Warning: Fileblock has indexdata too large and may be considered corrupt");
		if (indexdata.size() > MAX_HEADER_SIZE - 512) {
			throw new Error("This file header is too large. Parsers will reject it.");
		}
	}
	return new FileBlock(type, blob, indexdata);
}
 
Example 11
Source File: KeyUtils.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
public static String toHexString(ByteString bs) {
    int len = bs.size();
    if (len == 0) {
        return "";
    }
    StringBuilder sb = new StringBuilder(len << 1);
    for (int i = 0; i < len; i++) {
        int b = bs.byteAt(i);
        sb.append(HEX_CHARS[(b >> 4) & 0xf]).append(HEX_CHARS[b & 0xf]);
    }
    return sb.toString();
}
 
Example 12
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 13
Source File: StubInstance.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Digest> putAllBlobs(Iterable<ByteString> blobs, RequestMetadata requestMetadata) {
  long totalSize = 0;
  ImmutableList.Builder<Request> requests = ImmutableList.builder();
  for (ByteString blob : blobs) {
    checkState(totalSize + blob.size() <= maxBatchUpdateBlobsSize);
    requests.add(Request.newBuilder().setDigest(digestUtil.compute(blob)).setData(blob).build());
    totalSize += blob.size();
  }
  BatchUpdateBlobsRequest batchRequest =
      BatchUpdateBlobsRequest.newBuilder()
          .setInstanceName(getName())
          .addAllRequests(requests.build())
          .build();
  BatchUpdateBlobsResponse batchResponse =
      deadlined(casBlockingStub)
          .withInterceptors(attachMetadataInterceptor(requestMetadata))
          .batchUpdateBlobs(batchRequest);
  PutAllBlobsException exception = null;
  for (BatchUpdateBlobsResponse.Response response : batchResponse.getResponsesList()) {
    com.google.rpc.Status status = response.getStatus();
    if (Code.forNumber(status.getCode()) != Code.OK) {
      if (exception == null) {
        exception = new PutAllBlobsException();
      }
      exception.addFailedResponse(response);
    }
  }
  if (exception != null) {
    throw exception;
  }
  return Iterables.transform(
      batchResponse.getResponsesList(), (response) -> response.getDigest());
}
 
Example 14
Source File: JavaPropsFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Escapes bytes in the format used in protocol buffer text format, which
 * is the same as the format used for C string literals.  All bytes
 * that are not printable 7-bit ASCII characters are escaped, as well as
 * backslash, single-quote, and double-quote characters.  Characters for
 * which no defined short-hand escape sequence is defined will be escaped
 * using 3-digit octal sequences.
 */
static String escapeBytes(final ByteString input) {
  final StringBuilder builder = new StringBuilder(input.size());
  for (int i = 0; i < input.size(); i++) {
    final byte b = input.byteAt(i);
    switch (b) {
      // Java does not recognize \a or \v, apparently.
      case 0x07: builder.append("\\a" ); break;
      case '\b': builder.append("\\b" ); break;
      case '\f': builder.append("\\f" ); break;
      case '\n': builder.append("\\n" ); break;
      case '\r': builder.append("\\r" ); break;
      case '\t': builder.append("\\t" ); break;
      case 0x0b: builder.append("\\v" ); break;
      case '\\': builder.append("\\\\"); break;
      case '\'': builder.append("\\\'"); break;
      case '"' : builder.append("\\\""); break;
      default:
        if (b >= 0x20) {
          builder.append((char) b);
        } else {
          builder.append('\\');
          builder.append((char) ('0' + ((b >>> 6) & 3)));
          builder.append((char) ('0' + ((b >>> 3) & 7)));
          builder.append((char) ('0' + (b & 7)));
        }
        break;
    }
  }
  return builder.toString();
}
 
Example 15
Source File: KeyRangeUtils.java    From client-java with Apache License 2.0 5 votes vote down vote up
static String formatByteString(ByteString key) {
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < key.size(); i++) {
    sb.append(key.byteAt(i) & 0xff);
    if (i < key.size() - 1) {
      sb.append(",");
    }
  }
  return sb.toString();
}
 
Example 16
Source File: KeyUtils.java    From client-java with Apache License 2.0 5 votes vote down vote up
public static boolean hasPrefix(ByteString str, ByteString prefix) {
  for (int i = 0; i < prefix.size(); i++) {
    if (str.byteAt(i) != prefix.byteAt(i)) {
      return false;
    }
  }
  return true;
}
 
Example 17
Source File: RpcClientProcessServiceImpl.java    From redtorch with MIT License 5 votes vote down vote up
public boolean sendCoreRpc(int targetNodeId, ByteString content, String reqId, RpcId rpcId) {
	logger.info("发送RPC记录,目标节点ID:{},请求ID:{},RPC:{}", targetNodeId, reqId, rpcId.getValueDescriptor().getName());
	if (content.size() > 262144) {
		return sendLz4CoreRpc(targetNodeId, content, reqId, rpcId);
	} else {
		return sendRoutineCoreRpc(targetNodeId, content, reqId, rpcId);
	}
}
 
Example 18
Source File: NettyHttpServer.java    From krpc with Apache License 2.0 4 votes vote down vote up
void writeStream0(ChannelHandlerContext ctx, ChannelPromise promise, DefaultWebRes data, ByteString downloadStream) throws IOException {

        int size = downloadStream.size();
        ByteBuf bb = ctx.alloc().buffer(size);  //  ByteBuf type: io.netty.buffer.PooledUnsafeDirectByteBuf
        bb.writeBytes(downloadStream.asReadOnlyByteBuffer());

        DefaultFullHttpResponse res = null;

        int len = bb.readableBytes();
        if (data.isHeadMethod()) {
            res = new DefaultFullHttpResponse(data.getVersion(), HttpResponseStatus.valueOf(data.getHttpCode()));
            ReferenceCountUtil.release(bb);
        } else {
            res = new DefaultFullHttpResponse(data.getVersion(), HttpResponseStatus.valueOf(data.getHttpCode()), bb);
        }
        res.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, len);

        if (data.getHeaders() != null) {
            for (String key : data.getHeaders().names()) {
                res.headers().set(key, data.getHeaders().get(key));
            }
        }

        if (data.getCookies() != null) {
            for (Cookie c : data.getCookies()) {
                String s = ServerCookieEncoder.STRICT.encode(c);
                res.headers().add(HttpHeaderNames.SET_COOKIE, s);
            }
        }

        String filename = data.getStringResult("filename");

        res.headers().set(HttpHeaderNames.SERVER, WebConstants.Server);
        res.headers().set(HttpHeaderNames.DATE, WebUtils.formatDate(new GregorianCalendar().getTime()));
        res.headers().set(HttpHeaderNames.CONTENT_TYPE, WebUtils.getContentType(filename));
        res.headers().set(HttpHeaderNames.LAST_MODIFIED, WebUtils.formatDate(new Date()));
        res.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache");
        setAttachment(data, res, filename);

        if (data.isKeepAlive()) {
            res.headers().set(HttpHeaderNames.CONNECTION, "keep-alive");
        }

        ChannelFuture future = ctx.writeAndFlush(res, promise);
        if (!data.isKeepAlive()) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
 
Example 19
Source File: AccountPermissionUpdateOperator.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean checkPermission(Permission permission) throws ContractValidateException {
    if (permission.getKeysCount() > dbManager.getDynamicPropertiesStore().getTotalSignNum()) {
        throw new ContractValidateException("number of keys in permission should not be greater "
                + "than " + dbManager.getDynamicPropertiesStore().getTotalSignNum());
    }
    if (permission.getKeysCount() == 0) {
        throw new ContractValidateException("key's count should be greater than 0");
    }
    if (permission.getType() == PermissionType.Witness && permission.getKeysCount() != 1) {
        throw new ContractValidateException("Witness permission's key count should be 1");
    }
    if (permission.getThreshold() <= 0) {
        throw new ContractValidateException("permission's threshold should be greater than 0");
    }
    String name = permission.getPermissionName();
    if (!StringUtils.isEmpty(name) && name.length() > 32) {
        throw new ContractValidateException("permission's name is too long");
    }
    //check owner name ?
    if (permission.getParentId() != 0) {
        throw new ContractValidateException("permission's parent should be owner");
    }

    long weightSum = 0;
    List<ByteString> addressList = permission.getKeysList()
            .stream()
            .map(x -> x.getAddress())
            .distinct()
            .collect(toList());
    if (addressList.size() != permission.getKeysList().size()) {
        throw new ContractValidateException(
                "address should be distinct in permission " + permission.getType());
    }
    for (Key key : permission.getKeysList()) {
        if (!Wallet.addressValid(key.getAddress().toByteArray())) {
            throw new ContractValidateException("key is not a validate address");
        }
        if (key.getWeight() <= 0) {
            throw new ContractValidateException("key's weight should be greater than 0");
        }
        try {
            weightSum = Math.addExact(weightSum, key.getWeight());
        } catch (ArithmeticException e) {
            throw new ContractValidateException(e.getMessage());
        }
    }
    if (weightSum < permission.getThreshold()) {
        throw new ContractValidateException(
                "sum of all key's weight should not be less than threshold in permission " + permission
                        .getType());
    }

    ByteString operations = permission.getOperations();
    if (permission.getType() != PermissionType.Active) {
        if (!operations.isEmpty()) {
            throw new ContractValidateException(
                    permission.getType() + " permission needn't operations");
        }
        return true;
    }
    //check operations
    if (operations.isEmpty() || operations.size() != 32) {
        throw new ContractValidateException("operations size must 32");
    }

    byte[] types1 = dbManager.getDynamicPropertiesStore().getAvailableContractType();
    for (int i = 0; i < 256; i++) {
        boolean b = (operations.byteAt(i / 8) & (1 << (i % 8))) != 0;
        boolean t = ((types1[(i / 8)] & 0xff) & (1 << (i % 8))) != 0;
        if (b && !t) {
            throw new ContractValidateException(i + " isn't a validate ContractType");
        }
    }
    return true;
}
 
Example 20
Source File: XProtocolRow.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public boolean getNull(int columnIndex) {
    ByteString byteString = this.rowMessage.getField(columnIndex);
    this.wasNull = byteString.size() == 0;
    return this.wasNull;
}