net.consensys.cava.bytes.Bytes.toInt()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(141)

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

Bytes.toInt介绍

[英]The value corresponding to interpreting these bytes as an integer.
[中]对应于将这些字节解释为整数的值。

代码示例

代码示例来源:origin: net.consensys.cava/cava-bytes

@Override
public int toInt() {
 return delegate.toInt();
}

代码示例来源:origin: net.consensys.cava/cava-ssz

private <T> List<T> readList(int elementSize, Supplier<T> elementSupplier) {
  ensureBytes(4, () -> "SSZ encoded data is not a list");
  int originalIndex = this.index;
  List<T> bytesList;
  try {
   int listSize = consumeBytes(4).toInt();
   if ((listSize % elementSize) != 0) {
    throw new InvalidSSZTypeException("SSZ encoded list length does not align with lengths of its elements");
   }
   int nElements = listSize / elementSize;
   bytesList = new ArrayList<>(nElements);
   for (int i = 0; i < nElements; ++i) {
    bytesList.add(elementSupplier.get());
   }
  } catch (Exception e) {
   this.index = originalIndex;
   throw e;
  }
  return bytesList;
 }
}

代码示例来源:origin: net.consensys.cava/cava-ssz

@Override
public int readInt(int bitLength) {
 checkArgument(bitLength % 8 == 0, "bitLength must be a multiple of 8");
 int byteLength = bitLength / 8;
 ensureBytes(byteLength, () -> "SSZ encoded data has insufficient length to read a " + bitLength + "-bit integer");
 Bytes bytes = content.slice(index, byteLength);
 int zeroBytes = bytes.numberOfLeadingZeroBytes();
 if ((byteLength - zeroBytes) > 4) {
  throw new InvalidSSZTypeException("decoded integer is too large for an int");
 }
 index += byteLength;
 return bytes.slice(zeroBytes).toInt();
}

代码示例来源:origin: net.consensys.cava/cava-rlp

/**
 * Read an integer value from the RLP source.
 *
 * @param lenient If {@code false}, an exception will be thrown if the integer is not minimally encoded.
 * @return An integer.
 * @throws InvalidRLPEncodingException If there is an error decoding the RLP source, or the integer is not minimally
 *         encoded and `lenient` is {@code false}.
 * @throws InvalidRLPTypeException If the next RLP value cannot be represented as an integer.
 * @throws EndOfRLPException If there are no more RLP values to read.
 */
default int readInt(boolean lenient) {
 Bytes bytes = readValue();
 if (!lenient && bytes.hasLeadingZeroByte()) {
  throw new InvalidRLPEncodingException("Integer value was not minimally encoded");
 }
 try {
  return bytes.toInt();
 } catch (IllegalArgumentException e) {
  throw new InvalidRLPTypeException("Value is too large to be represented as an int");
 }
}

代码示例来源:origin: net.consensys.cava/cava-rlp

private int getLength(int lengthOfLength, boolean lenient, String type) {
  Bytes lengthBytes = content.slice(index + 1, lengthOfLength);
  if (!lenient) {
   if (lengthBytes.hasLeadingZeroByte()) {
    throw new InvalidRLPEncodingException("RLP " + type + " length contains leading zero bytes");
   }
  } else {
   lengthBytes = lengthBytes.trimLeadingZeros();
  }
  if (lengthBytes.size() == 0) {
   throw new InvalidRLPEncodingException("RLP " + type + " length is zero");
  }
  // Check if the length is greater than a 4 byte integer
  if (lengthBytes.size() > 4) {
   throw new InvalidRLPEncodingException("RLP " + type + " length is oversized");
  }
  int length = lengthBytes.toInt();
  if (length < 0) {
   // Java ints are two's compliment, so this was oversized
   throw new InvalidRLPEncodingException("RLP " + type + " length is oversized");
  }
  assert length > 0;
  if (!lenient && length <= 55) {
   throw new InvalidRLPEncodingException("RLP " + type + " length of " + length + " was not minimally encoded");
  }
  return length;
 }
}

相关文章

微信公众号

最新文章

更多