Skip to content

Commit c3c1a85

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Add limit and remaining methods
1 parent e100681 commit c3c1a85

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/main/java/io/appulse/utils/Bytes.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ public byte[] array () {
136136
return Arrays.copyOfRange(buffer.array(), 0, buffer.position());
137137
}
138138

139-
public int size () {
140-
return buffer.position();
139+
public int limit () {
140+
return buffer.limit();
141+
}
142+
143+
public int remaining () {
144+
return buffer.remaining();
141145
}
142146

143147
public int position () {

src/test/java/io/appulse/utils/BytesTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,31 @@ public void array () {
8888
assertThat(bytes.array())
8989
.isEqualTo(new byte[] { 1, 2 });
9090
}
91+
92+
@Test
93+
public void limit () {
94+
Bytes bytes = Bytes.allocate(2);
95+
assertThat(bytes.limit()).isEqualTo(2);
96+
97+
bytes.put4B(4);
98+
assertThat(bytes.limit()).isEqualTo(4);
99+
100+
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
101+
assertThat(wrapped.limit()).isEqualTo(1);
102+
}
103+
104+
@Test
105+
public void remaining () {
106+
Bytes bytes = Bytes.allocate(2);
107+
assertThat(bytes.remaining()).isEqualTo(2);
108+
109+
bytes.put4B(4);
110+
assertThat(bytes.remaining()).isEqualTo(0);
111+
112+
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
113+
assertThat(wrapped.remaining()).isEqualTo(0);
114+
115+
wrapped.flip();
116+
assertThat(wrapped.remaining()).isEqualTo(1);
117+
}
91118
}

0 commit comments

Comments
 (0)