本文整理了Java中net.consensys.cava.bytes.Bytes.slice()
方法的一些代码示例,展示了Bytes.slice()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytes.slice()
方法的具体详情如下:
包路径:net.consensys.cava.bytes.Bytes
类名称:Bytes
方法名:slice
[英]Create a new value representing (a view of) a slice of the bytes of this value.
Please note that the resulting slice is only a view and as such maintains a link to the underlying full value. So holding a reference to the returned slice may hold more memory than the slide represents. Use #copy on the returned slice if that is not what you want.
[中]创建一个新值,表示该值的字节片(视图)。
请注意,生成的切片只是一个视图,因此维护到底层完整值的链接。因此,保留对返回片的引用可能比幻灯片所代表的内存更多。如果返回的切片不是您想要的,请使用#copy。
代码示例来源:origin: net.consensys.cava/cava-ssz
private Bytes consumeBytes(int size) {
Bytes bytes = content.slice(index, size);
index += size;
return bytes;
}
代码示例来源:origin: net.consensys.cava/cava-bytes
@Override
public Bytes slice(int index) {
return delegate.slice(index);
}
代码示例来源:origin: net.consensys.cava/cava-bytes
@Override
public Bytes slice(int index, int length) {
return delegate.slice(index, length);
}
代码示例来源:origin: net.consensys.cava/cava-bytes
/**
* Return a slice over the common prefix between this set of bytes and another.
*
* @param other The bytes to compare to.
* @return A slice covering the common prefix.
*/
default Bytes commonPrefix(Bytes other) {
return slice(0, commonPrefixLength(other));
}
代码示例来源:origin: net.consensys.cava/cava-bytes
/**
* Create a new value representing (a view of) a slice of the bytes of this value.
*
* <p>
* Please note that the resulting slice is only a view and as such maintains a link to the underlying full value. So
* holding a reference to the returned slice may hold more memory than the slide represents. Use {@link #copy} on the
* returned slice if that is not what you want.
*
* @param i The start index for the slice.
* @return A new value providing a view over the bytes from index {@code i} (included) to the end.
* @throws IndexOutOfBoundsException if {@code i < 0}.
*/
default Bytes slice(int i) {
int size = size();
if (i >= size) {
return EMPTY;
}
return slice(i, size - i);
}
代码示例来源: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-ssz
@Override
public long readLong(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 = content.numberOfLeadingZeroBytes();
if ((byteLength - zeroBytes) > 8) {
throw new InvalidSSZTypeException("decoded integer is too large for an int");
}
index += byteLength;
return bytes.slice(zeroBytes).toLong();
}
代码示例来源:origin: net.consensys.cava/cava-bytes
/**
* Wrap a slice/sub-part of the provided value as a {@link Bytes32}.
*
* <p>
* Note that value is not copied, only wrapped, and thus any future update to {@code value} within the wrapped parts
* will be reflected in the returned value.
*
* @param value The bytes to wrap.
* @param offset The index (inclusive) in {@code value} of the first byte exposed by the returned value. In other
* words, you will have {@code wrap(value, i).get(0) == value.get(i)}.
* @return A {@link Bytes32} that exposes the bytes of {@code value} from {@code offset} (inclusive) to
* {@code offset + 32} (exclusive).
* @throws IndexOutOfBoundsException if {@code offset < 0 || (value.size() > 0 && offset >=
* value.size())}.
* @throws IllegalArgumentException if {@code length < 0 || offset + 32 > value.size()}.
*/
static Bytes32 wrap(Bytes value, int offset) {
checkNotNull(value);
if (value instanceof Bytes32) {
return (Bytes32) value;
}
Bytes slice = value.slice(offset, Bytes32.SIZE);
if (slice instanceof Bytes32) {
return (Bytes32) slice;
}
return DelegatingBytes32.delegateTo(slice);
}
代码示例来源:origin: net.consensys.cava/cava-bytes
/**
* Return a slice of representing the same value but without any leading zero bytes.
*
* @return {@code value} if its left-most byte is non zero, or a slice that exclude any leading zero bytes.
*/
default Bytes trimLeadingZeros() {
int size = size();
for (int i = 0; i < size; i++) {
if (get(i) != 0) {
return slice(i);
}
}
return Bytes.EMPTY;
}
代码示例来源:origin: net.consensys.cava/cava-bytes
return values[j].slice(i, length);
Bytes firstValue = this.values[j].slice(i);
int firstOffset = j;
System.arraycopy(this.values, firstOffset + 1, combined, 1, combined.length - 2);
combined[combined.length - 1] = this.values[j].slice(0, remaining);
} else if (combined.length > 1) {
System.arraycopy(this.values, firstOffset + 1, combined, 1, combined.length - 1);
代码示例来源:origin: net.consensys.cava/cava-rlpx
static Bytes decryptMessage(Bytes msgBytes, SecretKey ourKey) {
Bytes commonMac = msgBytes.slice(0, 2);
int size = (commonMac.get(1) & 0xFF) + ((commonMac.get(0) & 0xFF) << 8);
PublicKey ephemeralPublicKey = PublicKey.fromBytes(msgBytes.slice(3, 64));
Bytes iv = msgBytes.slice(67, 16);
Bytes encrypted = msgBytes.slice(83, size - 81);
EthereumIESEncryptionEngine decryptor = forDecryption(ourKey, ephemeralPublicKey, iv, commonMac);
byte[] result;
try {
result = decryptor.processBlock(encrypted.toArrayUnsafe(), 0, encrypted.size());
} catch (InvalidCipherTextException e) {
throw new InvalidMACException(e);
}
return Bytes.wrap(result);
}
代码示例来源:origin: net.consensys.cava/cava-rlpx
public synchronized void stream(Bytes newBytes, Consumer<RLPxMessage> messageConsumer) {
buffer = Bytes.concatenate(buffer, newBytes);
RLPxMessage message = null;
do {
message = readFrame(buffer);
if (message != null) {
buffer = buffer.slice(message.bytesLength());
messageConsumer.accept(message);
}
} while (buffer.size() != 0 && message != null);
}
代码示例来源:origin: net.consensys.cava/cava-rlp
/**
* Write a big integer to the output.
*
* @param value The integer to write.
*/
default void writeBigInteger(BigInteger value) {
if (value.signum() == 0) {
writeInt(0);
return;
}
byte[] byteArray = value.toByteArray();
if (byteArray[0] == 0) {
writeValue(Bytes.wrap(byteArray).slice(1));
} else {
writeByteArray(byteArray);
}
}
代码示例来源:origin: net.consensys.cava/cava-devp2p
static PacketHeader decode(Bytes data) {
Bytes hash = data.slice(0, 32);
Bytes encodedSignature = data.slice(32, 65);
Signature signature;
try {
signature = Signature.fromBytes(encodedSignature);
} catch (IllegalArgumentException e) {
throw new PeerDiscoveryPacketDecodingException(
"Could not retrieve the public key from the signature and signed data");
}
Bytes signedPayload = data.slice(97, data.size() - 97);
byte typeByte = data.get(97);
Bytes payloadBytes = data.slice(98);
PublicKey publicKey = PublicKey.recoverFromSignature(signedPayload, signature);
if (publicKey == null) {
throw new PeerDiscoveryPacketDecodingException(
"Could not retrieve the public key from the signature and signed data");
}
Bytes computedHash = Hash.sha3_256(Bytes.wrap(Bytes.wrap(signature.bytes(), Bytes.of(typeByte)), payloadBytes));
if (!computedHash.equals(hash)) {
throw new PeerDiscoveryPacketDecodingException("Hash does not match content");
}
return new PacketHeader(hash, publicKey, signature, typeByte);
}
代码示例来源:origin: net.consensys.cava/cava-rlp
Bytes bytes = content.slice(index, length);
index += length;
return bytes;
Bytes bytes = content.slice(index, length);
index += length;
return bytes;
代码示例来源:origin: net.consensys.cava/cava-rlp
return content.slice(index++, 1);
"Insufficient bytes in RLP encoding: expected " + length + " but have only " + remaining);
Bytes bytes = content.slice(index + 1, length);
if (!lenient && length == 1 && (bytes.get(0) & 0xFF) <= 0x7f) {
throw new InvalidRLPEncodingException("Value should have been encoded as a single byte " + bytes.toHexString());
Bytes bytes = content.slice(index, length);
index += length;
return bytes;
代码示例来源:origin: net.consensys.cava/cava-rlpx
decryptionCipher.init(false, new ParametersWithIV(aesKey, IV));
Bytes macBytes = messageFrame.slice(16, 16);
Bytes headerBytes = messageFrame.slice(0, 16);
Bytes frameData = messageFrame.slice(32, frameSize);
Bytes frameMac = messageFrame.slice(32 + frameSize + pad, 16);
Bytes frameMacSeed = updateIngress(messageFrame.slice(32, frameSize + pad));
macEncryptionEngine.processBlock(frameMacSeed.toArrayUnsafe(), 0, newFrameMac.toArrayUnsafe(), 0);
Bytes expectedFrameMac = updateIngress(newFrameMac.xor(frameMacSeed.slice(0, 16))).slice(0, 16);
if (!expectedFrameMac.equals(frameMac)) {
throw new InvalidMACException(
.processBytes(frameData.toArrayUnsafe(), 0, frameData.size(), decryptedFrameData.toArrayUnsafe(), 0);
int messageType = RLP.decodeInt(decryptedFrameData.slice(0, 1));
Bytes messageData = decryptedFrameData.slice(1);
if (applySnappyCompression) {
try {
代码示例来源:origin: net.consensys.cava/cava-rlpx
private Bytes calculateMac(Bytes input, boolean ingress) {
Bytes mac = Bytes.wrap(new byte[16]);
macEncryptionEngine.processBlock(
snapshot(ingress ? ingressMac : egressMac).slice(0, 16).toArrayUnsafe(),
0,
mac.toArrayUnsafe(),
0);
mac = mac.xor(input);
if (ingress) {
mac = updateIngress(mac).slice(0, 16);
} else {
mac = updateEgress(mac).slice(0, 16);
}
return mac.slice(0, 16);
}
代码示例来源:origin: net.consensys.cava/cava-devp2p
void receivePacket(Bytes data) {
PacketHeader header = PacketHeader.decode(data);
Bytes payloadBytes = data.slice(98);
switch (header.packetType()) {
case 0x01:
handlePing(new Packet<>(PingPayload.decode(payloadBytes), header));
break;
case 0x02:
handlePong(new Packet<>(PongPayload.decode(payloadBytes), header));
break;
case 0x03:
handleFindNeighbors(new Packet<>(FindNeighborsPayload.decode(payloadBytes), header));
break;
case 0x04:
handleNeighbors(new Packet<>(NeighborsPayload.decode(payloadBytes), header));
break;
default:
throw new PeerDiscoveryPacketDecodingException(String.format("Invalid packet type %02X", header.packetType()));
}
}
}
代码示例来源: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;
}
}
内容来源于网络,如有侵权,请联系作者删除!