Skip to content

Commit f0e5cac

Browse files
committed
Support creation of immutable Bytes from mutable one
1 parent cd9e314 commit f0e5cac

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* add cache for calculating the hashCode
1313
* add HMAC byte transformer #11
1414
* add unsigned sort transformer #17
15+
* add `.immutable()` converter in MutableBytes #18
1516

1617
## v0.6.0
1718

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,12 @@ b.wipe() //fills with zeros
549549
b.secureWipe() //fills with random data
550550
```
551551

552+
Create a immutable version again with:
553+
554+
```java
555+
Bytes b2 = b.immutable();
556+
```
557+
552558
*Note:* a copy will inherit mutability/read-only properties:
553559

554560
```java

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ public MutableBytes secureWipe(SecureRandom random) {
126126
return this;
127127
}
128128

129+
/**
130+
* Convert this instance to an immutable version with the same reference of the internal array and byte-order.
131+
* If the mutable instance is kept, it can be used to alter the internal array of the just created instance, so be
132+
* aware.
133+
*
134+
* @return immutable version of this instance
135+
*/
136+
public Bytes immutable() {
137+
return Bytes.wrap(internalArray(), byteOrder());
138+
}
139+
129140
@Override
130141
public int hashCode() {
131142
return Util.hashCode(internalArray(), byteOrder());

src/test/java/at/favre/lib/bytes/MutableBytesABytesTest.java renamed to src/test/java/at/favre/lib/bytes/MutableBytesTest.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@
2828

2929
import static org.junit.Assert.*;
3030

31-
public class MutableBytesABytesTest extends ABytesTest {
31+
public class MutableBytesTest extends ABytesTest {
3232
@Test
33-
public void overwriteWithEmptyArray() throws Exception {
33+
public void overwriteWithEmptyArray() {
3434
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
3535
assertSame(b, b.overwrite(new byte[example_bytes_seven.length]));
3636
assertArrayEquals(new byte[example_bytes_seven.length], b.array());
3737
}
3838

3939
@Test
40-
public void overwriteOtherArray() throws Exception {
40+
public void overwriteOtherArray() {
4141
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
4242
assertSame(b, b.overwrite(Arrays.copyOf(example2_bytes_seven, example2_bytes_seven.length)));
4343
assertArrayEquals(example2_bytes_seven, b.array());
4444
}
4545

4646
@Test
47-
public void overwritePartialArray() throws Exception {
47+
public void overwritePartialArray() {
4848
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
4949
assertSame(b, b.overwrite(new byte[]{(byte) 0xAA}, 0));
5050
assertArrayEquals(Bytes.from((byte) 0xAA).append(Bytes.wrap(example_bytes_seven).copy(1, example_bytes_seven.length - 1)).array(), b.array());
5151
}
5252

5353
@Test
54-
public void overwritePartialArray2() throws Exception {
54+
public void overwritePartialArray2() {
5555
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
5656
assertSame(b, b.overwrite(new byte[]{(byte) 0xAA}, 1));
5757
assertArrayEquals(
@@ -62,14 +62,44 @@ public void overwritePartialArray2() throws Exception {
6262
}
6363

6464
@Test
65-
public void fill() throws Exception {
65+
public void fill() {
6666
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
6767
assertSame(b, b.fill((byte) 0));
6868
assertArrayEquals(new byte[example_bytes_seven.length], b.array());
6969
}
7070

7171
@Test
72-
public void setByteAtTest() throws Exception {
72+
public void testConvertImmutable() {
73+
Bytes b = Bytes.from(example_bytes_seven);
74+
MutableBytes m = b.copy().mutable();
75+
assertNotEquals(b, m);
76+
assertTrue(b.equalsContent(m));
77+
assertEquals(b.byteOrder(), m.byteOrder());
78+
79+
Bytes m2b = m.immutable();
80+
assertNotEquals(m2b, m);
81+
assertEquals(m2b, b);
82+
assertNotSame(m2b, b);
83+
assertTrue(m2b.equalsContent(m));
84+
assertEquals(m2b.byteOrder(), m.byteOrder());
85+
86+
assertEquals(m.length(), m2b.length());
87+
assertEquals(m.length(), b.length());
88+
89+
assertNotEquals(example_bytes_seven[0], 0);
90+
assertEquals(example_bytes_seven[0], b.byteAt(0));
91+
assertEquals(example_bytes_seven[0], m.byteAt(0));
92+
assertEquals(example_bytes_seven[0], m2b.byteAt(0));
93+
94+
m.fill((byte) 0);
95+
96+
assertEquals(example_bytes_seven[0], b.byteAt(0));
97+
assertEquals(0, m.byteAt(0));
98+
assertEquals(0, m2b.byteAt(0));
99+
}
100+
101+
@Test
102+
public void setByteAtTest() {
73103
MutableBytes b = fromAndTest(example_bytes_sixteen).mutable();
74104

75105
for (int i = 0; i < b.length(); i++) {
@@ -82,14 +112,14 @@ public void setByteAtTest() throws Exception {
82112
}
83113

84114
@Test
85-
public void wipe() throws Exception {
115+
public void wipe() {
86116
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
87117
assertSame(b, b.wipe());
88118
assertArrayEquals(new byte[example_bytes_seven.length], b.array());
89119
}
90120

91121
@Test
92-
public void secureWipe() throws Exception {
122+
public void secureWipe() {
93123
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
94124
int hashcode = b.hashCode();
95125
assertSame(b, b.secureWipe());
@@ -99,7 +129,7 @@ public void secureWipe() throws Exception {
99129
}
100130

101131
@Test
102-
public void secureWipeWithSecureRandom() throws Exception {
132+
public void secureWipeWithSecureRandom() {
103133
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
104134
int hashcode = b.hashCode();
105135
assertSame(b, b.secureWipe(new SecureRandom()));
@@ -109,18 +139,18 @@ public void secureWipeWithSecureRandom() throws Exception {
109139
}
110140

111141
@Test(expected = NullPointerException.class)
112-
public void secureWipeShouldThrowException() throws Exception {
142+
public void secureWipeShouldThrowException() {
113143
Bytes.wrap(new byte[0]).mutable().secureWipe(null);
114144
}
115145

116146
@Test
117-
public void testIfGetSameInstance() throws Exception {
147+
public void testIfGetSameInstance() {
118148
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
119149
assertSame(b, b.mutable());
120150
}
121151

122152
@Test
123-
public void testTransformerShouldBeMutable() throws Exception {
153+
public void testTransformerShouldBeMutable() {
124154
MutableBytes b = fromAndTest(example_bytes_seven).mutable();
125155
assertTrue(b.isMutable());
126156
assertTrue(b.copy().isMutable());
@@ -137,4 +167,4 @@ public void testTransformerShouldBeMutable() throws Exception {
137167
assertTrue(b.append(3).isMutable());
138168
assertTrue(b.hashSha256().isMutable());
139169
}
140-
}
170+
}

0 commit comments

Comments
 (0)