Skip to content

Commit 114f9b1

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Small API updates
1 parent 772c1e4 commit 114f9b1

File tree

7 files changed

+142
-15
lines changed

7 files changed

+142
-15
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Add more tests.
1313
- Add `JavaDoc`.
1414

15+
## [1.13.1](https://github.com/appulse-projects/utils-java/releases/tag/1.13.1) - 2019-04-08
16+
17+
### Added
18+
19+
- `Bytes`.`capacity(int)` method for resizing buffers.
20+
21+
### Changed
22+
23+
- Added static methods for `BytesByteBuf` wrapper;
24+
- Concrete Java version (1.8) now specified in `pom.xml`.
25+
1526
## [1.13.0](https://github.com/appulse-projects/utils-java/releases/tag/1.13.0) - 2019-04-07
1627

1728
### Changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ limitations under the License.
2424

2525
<groupId>io.appulse</groupId>
2626
<artifactId>utils-java</artifactId>
27-
<version>1.13.0</version>
27+
<version>1.13.1</version>
2828
<packaging>jar</packaging>
2929

3030
<properties>
3131
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3232
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
3333

34-
<java.version>${java.vm.specification.version}</java.version>
34+
<java.version>1.8</java.version>
3535

3636
<maven.compiler.source>${java.version}</maven.compiler.source>
3737
<maven.compiler.target>${java.version}</maven.compiler.target>
@@ -66,7 +66,7 @@ limitations under the License.
6666
<url>https://github.com/appulse-projects/utils-java</url>
6767
<connection>scm:git:https://github.com/appulse-projects/utils-java.git</connection>
6868
<developerConnection>scm:git:https://github.com/appulse-projects/utils-java.git</developerConnection>
69-
<tag>1.13.0</tag>
69+
<tag>1.13.1</tag>
7070
</scm>
7171

7272
<distributionManagement>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,13 @@ static Bytes resizableArray (int initialSize) {
16411641
*/
16421642
int capacity ();
16431643

1644+
/**
1645+
* Extends or reduces the buffer's capacity.
1646+
*
1647+
* @param bytes the new buffer's capacity
1648+
*/
1649+
void capacity (int bytes);
1650+
16441651
/**
16451652
* Returns the {@code writerIndex} of this buffer.
16461653
*

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

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,66 @@
1616

1717
package io.appulse.utils;
1818

19-
import static lombok.AccessLevel.PACKAGE;
2019
import static lombok.AccessLevel.PRIVATE;
2120

21+
import java.nio.ByteBuffer;
2222
import java.nio.charset.Charset;
23+
import java.util.Arrays;
2324

2425
import io.netty.buffer.ByteBuf;
26+
import io.netty.buffer.Unpooled;
2527
import lombok.NonNull;
26-
import lombok.RequiredArgsConstructor;
2728
import lombok.experimental.FieldDefaults;
2829
import lombok.val;
2930

3031
@SuppressWarnings("PMD.LinguisticNaming")
31-
@RequiredArgsConstructor(access = PACKAGE)
32-
@FieldDefaults(level = PRIVATE, makeFinal = true)
33-
class BytesByteBuf extends BytesAbstract {
32+
@FieldDefaults(level = PRIVATE)
33+
public final class BytesByteBuf extends BytesAbstract {
3434

35-
static BytesByteBuf copy (@NonNull ByteBuf buffer) {
35+
public static BytesByteBuf allocate (int size) {
36+
val buffer = Unpooled.buffer(size);
37+
buffer.clear();
38+
return new BytesByteBuf(buffer);
39+
}
40+
41+
public static BytesByteBuf wrap (@NonNull byte[] bytes) {
42+
val wrapped = Unpooled.wrappedBuffer(bytes);
43+
wrapped.clear();
44+
return new BytesByteBuf(wrapped);
45+
}
46+
47+
public static BytesByteBuf wrap (@NonNull ByteBuffer buffer) {
48+
val wrapped = Unpooled.wrappedBuffer(buffer);
49+
return new BytesByteBuf(wrapped);
50+
}
51+
52+
public static BytesByteBuf wrap (@NonNull ByteBuf buffer) {
53+
val wrapped = Unpooled.wrappedBuffer(buffer);
54+
return new BytesByteBuf(wrapped);
55+
}
56+
57+
public static BytesByteBuf copy (@NonNull byte[] bytes) {
58+
val copy = Unpooled.copiedBuffer(bytes);
59+
return new BytesByteBuf(copy);
60+
}
61+
62+
public static BytesByteBuf copy (@NonNull ByteBuffer buffer) {
63+
val copy = Unpooled.copiedBuffer(buffer);
64+
return new BytesByteBuf(copy);
65+
}
66+
67+
public static BytesByteBuf copy (@NonNull ByteBuf buffer) {
3668
val copy = buffer.copy();
3769
return new BytesByteBuf(copy);
3870
}
3971

40-
@NonNull
4172
ByteBuf buffer;
4273

74+
private BytesByteBuf (@NonNull ByteBuf buffer) {
75+
super();
76+
this.buffer = buffer;
77+
}
78+
4379
@Override
4480
public boolean isResizable () {
4581
return false;
@@ -199,6 +235,21 @@ public int capacity () {
199235
return buffer.capacity();
200236
}
201237

238+
@Override
239+
public void capacity (int bytes) {
240+
if (capacity() == bytes) {
241+
return;
242+
}
243+
244+
val oldReaderIndex = buffer.readerIndex();
245+
val oldWriterIndex = buffer.writerIndex();
246+
val newByteArray = Arrays.copyOf(buffer.array(), bytes);
247+
248+
buffer = Unpooled.wrappedBuffer(newByteArray);
249+
buffer.readerIndex(Math.min(oldReaderIndex, bytes - 1));
250+
buffer.writerIndex(Math.min(oldWriterIndex, bytes - 1));
251+
}
252+
202253
@Override
203254
public int writerIndex () {
204255
return buffer.writerIndex();

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,36 @@
1616

1717
package io.appulse.utils;
1818

19-
import static lombok.AccessLevel.PACKAGE;
2019
import static lombok.AccessLevel.PRIVATE;
2120

2221
import java.nio.ByteBuffer;
2322
import java.nio.charset.Charset;
23+
import java.util.Arrays;
2424

2525
import lombok.NonNull;
26-
import lombok.RequiredArgsConstructor;
2726
import lombok.experimental.FieldDefaults;
2827
import lombok.val;
2928

3029
@FieldDefaults(level = PRIVATE)
3130
@SuppressWarnings("PMD.LinguisticNaming")
32-
@RequiredArgsConstructor(access = PACKAGE)
3331
class BytesByteBuffer extends BytesAbstract {
3432

3533
static BytesByteBuffer copy (@NonNull ByteBuffer buffer) {
3634
val copy = buffer;
3735
return new BytesByteBuffer(copy);
3836
}
3937

40-
@NonNull
41-
final ByteBuffer buffer;
38+
ByteBuffer buffer;
4239

4340
int writerIndex;
4441

4542
int readerIndex;
4643

44+
BytesByteBuffer (@NonNull ByteBuffer buffer) {
45+
super();
46+
this.buffer = buffer;
47+
}
48+
4749
@Override
4850
public boolean isResizable () {
4951
return false;
@@ -244,6 +246,22 @@ public int capacity () {
244246
return buffer.capacity();
245247
}
246248

249+
@Override
250+
public void capacity (int bytes) {
251+
if (capacity() == bytes) {
252+
return;
253+
}
254+
255+
val oldPosition = buffer.position();
256+
val newByteArray = Arrays.copyOf(buffer.array(), bytes);
257+
258+
buffer = ByteBuffer.wrap(newByteArray);
259+
260+
buffer.position(Math.min(oldPosition, bytes - 1));
261+
writerIndex = Math.min(writerIndex, bytes - 1);
262+
readerIndex = Math.min(readerIndex, bytes - 1);
263+
}
264+
247265
@Override
248266
public int writerIndex () {
249267
return writerIndex;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,16 @@ public int capacity () {
243243
return buffer.length;
244244
}
245245

246+
@Override
247+
public void capacity (int bytes) {
248+
if (capacity() == bytes) {
249+
return;
250+
}
251+
buffer = Arrays.copyOf(buffer, bytes);
252+
writerIndex = Math.min(writerIndex, bytes - 1);
253+
readerIndex = Math.min(readerIndex, bytes - 1);
254+
}
255+
246256
@Override
247257
public int writerIndex () {
248258
return writerIndex;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import static org.assertj.core.api.Assertions.assertThat;
2121
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2222

23+
import java.nio.ByteBuffer;
24+
2325
import lombok.val;
2426
import org.junit.jupiter.api.Test;
2527

@@ -180,4 +182,32 @@ void unsignedTest () {
180182
assertThat(bytes.getUnsignedInt(3))
181183
.isEqualTo(4_100_000_000L);
182184
}
185+
186+
@Test
187+
void capacity () {
188+
assertCapacity(new BytesFixedArray(2));
189+
assertCapacity(new BytesByteBuffer(ByteBuffer.allocate(2)));
190+
assertCapacity(BytesByteBuf.allocate(2));
191+
}
192+
193+
private void assertCapacity (Bytes buffer) {
194+
buffer.capacity(2);
195+
buffer.write2B(7);
196+
assertThat(buffer.capacity())
197+
.isEqualTo(2);
198+
assertThat(buffer.array())
199+
.isEqualTo(new byte[] { 0, 7 });
200+
201+
buffer.capacity(4);
202+
assertThat(buffer.capacity())
203+
.isEqualTo(4);
204+
assertThat(buffer.array())
205+
.isEqualTo(new byte[] { 0, 7, 0, 0 });
206+
207+
buffer.capacity(1);
208+
assertThat(buffer.capacity())
209+
.isEqualTo(1);
210+
assertThat(buffer.array())
211+
.isEqualTo(new byte[] { 0 });
212+
}
183213
}

0 commit comments

Comments
 (0)