Skip to content

Commit 7c36da3

Browse files
JadePaukkunenpatrickfav
authored andcommitted
Overwrite-methods that take Bytes as parameters. Tests for IndexOutOf… (#35)
* Overwrite-methods that take Bytes as parameters. Tests for IndexOutOfBounds-exception when overwritten array exceeds the original size and NullPointerException when the array used to overwrite is null * add requested changes
1 parent 3b55099 commit 7c36da3

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Releases
22

3+
## Unreleased
4+
5+
* adds overwrite method to Bytes
6+
* adds tests to NullPointerException and IndexOutOfBoundsException
7+
38
## v1.1.0
49

510
* add `unsecureRandom()` constructor which creates random for e.g. tests or deterministic randoms

src/main/java/at/favre/lib/bytes/MutableBytes.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,25 @@ public boolean isMutable() {
7171
* @return this instance
7272
* @throws IndexOutOfBoundsException if newArray.length > internal length
7373
*/
74+
7475
public MutableBytes overwrite(byte[] newArray) {
76+
7577
return overwrite(newArray, 0);
7678
}
7779

80+
/**
81+
* Uses given Bytes array to overwrite internal array
82+
*
83+
* @param newBytes used to overwrite internal
84+
* @return this instance
85+
* @throws IndexOutOfBoundsException if newArray.length > internal length
86+
*/
87+
88+
public MutableBytes overwrite(Bytes newBytes) {
89+
90+
return overwrite(newBytes, 0);
91+
}
92+
7893
/**
7994
* Uses given array to overwrite internal array.
8095
*
@@ -83,12 +98,26 @@ public MutableBytes overwrite(byte[] newArray) {
8398
* @return this instance
8499
* @throws IndexOutOfBoundsException if newArray.length + offsetInternalArray > internal length
85100
*/
101+
86102
public MutableBytes overwrite(byte[] newArray, int offsetInternalArray) {
87103
Objects.requireNonNull(newArray, "must provide non-null array as source");
88104
System.arraycopy(newArray, 0, internalArray(), offsetInternalArray, newArray.length);
89105
return this;
90106
}
91107

108+
/**
109+
* Uses given Bytes array to overwrite internal array.
110+
*
111+
* @param newBytes used to overwrite internal
112+
* @param offsetInternalArray index of the internal array to start overwriting
113+
* @return this instance
114+
* @throws IndexOutOfBoundsException if newBytes.length + offsetInternalArray > internal length
115+
*/
116+
117+
public MutableBytes overwrite(Bytes newBytes, int offsetInternalArray) {
118+
return overwrite(Objects.requireNonNull(newBytes, "must provide non-null array as source").array(), offsetInternalArray);
119+
}
120+
92121
/**
93122
* Sets new byte to given index
94123
*

src/test/java/at/favre/lib/bytes/MutableBytesTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,50 @@ public void overwritePartialArray2() {
6363
.append(Bytes.wrap(example_bytes_seven).copy(2, example_bytes_seven.length - 2)).array(), b.array());
6464
}
6565

66+
@Test
67+
public void overwriteBytes() {
68+
MutableBytes a = fromAndTest(example_bytes_seven).mutable();
69+
MutableBytes b = Bytes.from((byte)0).mutable();
70+
MutableBytes c = a.overwrite(b, 0).mutable();
71+
MutableBytes d = Bytes.wrap(a).copy(1, a.array().length-1).mutable();
72+
73+
assertArrayEquals(c.array(), Bytes.from(b).append(d).array());
74+
}
75+
76+
@Test
77+
public void overwriteTooBigArrayShouldThrowException() {
78+
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
79+
try {
80+
b.overwrite(new byte[]{(byte) 0xAA, 0x30}, b.length());
81+
fail();
82+
} catch(IndexOutOfBoundsException ignored) {}
83+
84+
}
85+
86+
@Test
87+
public void overwriteTooBigBytesShouldThrowException() {
88+
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
89+
try {
90+
b.overwrite(Bytes.from((byte) 0xAA, 0x30), b.length());
91+
fail();
92+
} catch(IndexOutOfBoundsException ignored) {}
93+
94+
}
95+
96+
@Test
97+
public void overwriteNullArrayShouldThrowException() {
98+
MutableBytes nonsense = null;
99+
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
100+
101+
try{
102+
b.overwrite(nonsense);
103+
fail();
104+
} catch (NullPointerException ignored){}
105+
106+
107+
}
108+
109+
66110
@Test
67111
public void fill() {
68112
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
@@ -184,4 +228,5 @@ public void testAutoCloseable() {
184228
assertArrayNotEquals(new byte[16], leak.array());
185229

186230
}
231+
187232
}

0 commit comments

Comments
 (0)