Skip to content

Commit 2a23768

Browse files
committed
fixes to MutableBytes.set for empty structures
1 parent fd779c0 commit 2a23768

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

bytes/src/main/java/org/apache/tuweni/bytes/v2/MutableBytes.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ public static MutableBytes of(int... bytes) {
294294
*/
295295
public void set(int index, Bytes bytes) {
296296
checkNotNull(bytes);
297+
if (bytes.isEmpty()) {
298+
return;
299+
}
297300
checkElementIndex(index, length);
298301
checkArgument(
299302
index + bytes.size() <= length,
@@ -316,6 +319,9 @@ public void set(int index, Bytes bytes) {
316319
*/
317320
public void set(int index, byte[] bytes) {
318321
checkNotNull(bytes);
322+
if (bytes.length == 0) {
323+
return;
324+
}
319325
checkElementIndex(index, length);
320326
checkArgument(
321327
index + bytes.length <= length,
@@ -378,6 +384,24 @@ public void setLong(int index, long value) {
378384
set(index, (byte) (value & 0xFF));
379385
}
380386

387+
/**
388+
* Set a byte in this value.
389+
*
390+
* @param index The index of the byte to set.
391+
* @param b The value to set that byte to.
392+
* @throws IndexOutOfBoundsException if {@code i < 0} or {i >= size()}.
393+
*/
394+
public void set(int index, byte b) {
395+
checkElementIndex(index, length);
396+
if (index > (length - 1)) {
397+
throw new IndexOutOfBoundsException(
398+
format(
399+
"Value of size %s has not enough bytes to write a 4 bytes int from index %s",
400+
length, index));
401+
}
402+
bytesArray[offset + index] = b;
403+
}
404+
381405
/**
382406
* Increments the value of the bytes by 1, treating the value as big endian.
383407
*
@@ -724,17 +748,6 @@ public int size() {
724748
return length;
725749
}
726750

727-
/**
728-
* Set a byte in this value.
729-
*
730-
* @param i The index of the byte to set.
731-
* @param b The value to set that byte to.
732-
* @throws IndexOutOfBoundsException if {@code i < 0} or {i >= size()}.
733-
*/
734-
public void set(int i, byte b) {
735-
bytesArray[offset + i] = b;
736-
}
737-
738751
@Override
739752
public byte get(int i) {
740753
return bytesArray[offset + i];

devp2p/src/test/kotlin/org/apache/tuweni/devp2p/v5/internal/DefaultAuthenticationProviderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DefaultAuthenticationProviderTest {
5151
// val ephemeralKeyPair = SECP256K1.KeyPair.random()
5252
// val ephemeralKey = ephemeralKeyPair.secretKey()
5353
//
54-
// val signValue = Bytes.concatenate(Bytes.wrap("discovery-id-nonce".toByteArray()), nonce)
54+
// val signValue = Bytes.wrap(Bytes.wrap("discovery-id-nonce".toByteArray()), nonce)
5555
// val hashedSignValue = Hash.sha2_256(signValue)
5656
// val signature = SECP256K1.sign(hashedSignValue, keyPair)
5757
//

0 commit comments

Comments
 (0)