Skip to content

Commit 0d40d4b

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Interface refactoring
1 parent e0ed5f4 commit 0d40d4b

4 files changed

Lines changed: 49 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313

1414
## [1.0.3](https://github.com/appulse-projects/utils-java/releases/tag/1.0.3) - 2018-01-28
1515

16+
### Changed
17+
18+
- Refactored `Bytes` interface.
19+
20+
### Removed
21+
22+
- Netty's dependency.
23+
24+
## [1.0.3](https://github.com/appulse-projects/utils-java/releases/tag/1.0.3) - 2018-01-28
25+
1626
### Added
1727

1828
- New `Bytes` methods.

pom.xml

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

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

3030
<properties>
@@ -62,7 +62,7 @@ limitations under the License.
6262
<url>https://github.com/appulse-projects/utils-java</url>
6363
<connection>scm:git:https://github.com/appulse-projects/utils-java.git</connection>
6464
<developerConnection>scm:git:https://github.com/appulse-projects/utils-java.git</developerConnection>
65-
<tag>1.0.3</tag>
65+
<tag>1.0.4</tag>
6666
</scm>
6767

6868
<distributionManagement>
@@ -102,13 +102,6 @@ limitations under the License.
102102
<scope>provided</scope>
103103
</dependency>
104104

105-
<dependency>
106-
<groupId>io.netty</groupId>
107-
<artifactId>netty-buffer</artifactId>
108-
<version>4.1.20.Final</version>
109-
<scope>provided</scope>
110-
</dependency>
111-
112105
<dependency>
113106
<groupId>com.google.code.findbugs</groupId>
114107
<artifactId>annotations</artifactId>

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import java.util.Arrays;
2424
import java.util.stream.IntStream;
2525

26-
import io.netty.buffer.ByteBuf;
2726
import lombok.Getter;
27+
import lombok.NonNull;
2828
import lombok.experimental.Delegate;
2929
import lombok.experimental.FieldDefaults;
3030
import lombok.experimental.NonFinal;
@@ -38,26 +38,15 @@
3838
@FieldDefaults(level = PRIVATE, makeFinal = true)
3939
public final class Bytes {
4040

41-
public static Bytes wrap (byte[] bytes) {
41+
public static Bytes wrap (@NonNull byte[] bytes) {
4242
val result = allocate(bytes.length);
43-
return result.put(bytes);
44-
}
45-
46-
public static Bytes wrap (ByteBuffer buffer) {
47-
val clone = ByteBuffer.wrap(buffer.array());
48-
return new Bytes(clone);
43+
result.put(bytes);
44+
result.flip();
45+
return result;
4946
}
5047

51-
public static Bytes wrap (ByteBuf buf) {
52-
ByteBuffer buffer;
53-
if (buf.isDirect()) {
54-
buffer = buf.nioBuffer();
55-
} else {
56-
val bytes = new byte[buf.readableBytes()];
57-
buf.getBytes(buf.readerIndex(), bytes);
58-
buffer = ByteBuffer.wrap(bytes);
59-
}
60-
return new Bytes(buffer);
48+
public static Bytes wrap (@NonNull ByteBuffer buffer) {
49+
return wrap(buffer.array());
6150
}
6251

6352
public static Bytes allocate () {
@@ -79,15 +68,20 @@ public static Bytes allocate (int capacity) {
7968
@Getter(value = PACKAGE)
8069
ByteBuffer buffer;
8170

71+
@NonFinal
72+
int limit;
73+
8274
private Bytes (ByteBuffer buffer) {
8375
puts = new BytesDelegatePuts(this);
8476
gets = new BytesDelegateGets(this);
8577
this.buffer = buffer;
78+
limit = buffer.position();
8679
}
8780

8881
public Bytes put (byte value) {
8982
checkCapacity(1);
9083
buffer.put(value);
84+
limit++;
9185
return this;
9286
}
9387

@@ -96,13 +90,14 @@ public Bytes put (int index, byte value) {
9690
return this;
9791
}
9892

99-
public Bytes put (byte[] bytes) {
93+
public Bytes put (@NonNull byte[] bytes) {
10094
checkCapacity(bytes.length);
10195
buffer.put(bytes);
96+
limit += bytes.length;
10297
return this;
10398
}
10499

105-
public Bytes put (int index, byte[] bytes) {
100+
public Bytes put (int index, @NonNull byte[] bytes) {
106101
checkCapacity(index, bytes.length);
107102
IntStream.range(index, index + bytes.length).forEach(it -> {
108103
buffer.put(it, bytes[it - index]);
@@ -133,32 +128,39 @@ public byte[] getBytes (int offset, int length) {
133128
}
134129

135130
public byte[] array () {
136-
return Arrays.copyOfRange(buffer.array(), 0, buffer.position());
131+
return Arrays.copyOfRange(buffer.array(), 0, limit);
137132
}
138133

139134
public int limit () {
140-
return buffer.limit();
135+
return limit;
141136
}
142137

143138
public int remaining () {
144-
return buffer.remaining();
139+
return limit - buffer.position();
145140
}
146141

147142
public int position () {
148143
return buffer.position();
149144
}
150145

151146
public Bytes position (int position) {
147+
if (position > limit || position < 0) {
148+
throw new IllegalArgumentException();
149+
}
152150
buffer.position(position);
153151
return this;
154152
}
155153

156-
public void clear () {
154+
public Bytes clear () {
157155
buffer.clear();
156+
limit = 0;
157+
return this;
158158
}
159159

160-
public void flip () {
160+
public Bytes flip () {
161+
limit = buffer.position();
161162
buffer.flip();
163+
return this;
162164
}
163165

164166
private void checkCapacity (int size) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public void wrap () {
3535
Bytes bytes = Bytes.wrap(expected);
3636

3737
assertThat(bytes.position())
38+
.isEqualTo(0);
39+
40+
assertThat(bytes.limit())
41+
.isEqualTo(expected.length);
42+
43+
assertThat(bytes.remaining())
3844
.isEqualTo(expected.length);
3945

4046
assertThat(bytes.array())
@@ -92,7 +98,7 @@ public void array () {
9298
@Test
9399
public void limit () {
94100
Bytes bytes = Bytes.allocate(2);
95-
assertThat(bytes.limit()).isEqualTo(2);
101+
assertThat(bytes.limit()).isEqualTo(0);
96102

97103
bytes.put4B(4);
98104
assertThat(bytes.limit()).isEqualTo(4);
@@ -104,15 +110,15 @@ public void limit () {
104110
@Test
105111
public void remaining () {
106112
Bytes bytes = Bytes.allocate(2);
107-
assertThat(bytes.remaining()).isEqualTo(2);
113+
assertThat(bytes.remaining()).isEqualTo(0);
108114

109115
bytes.put4B(4);
110116
assertThat(bytes.remaining()).isEqualTo(0);
111117

112118
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
113-
assertThat(wrapped.remaining()).isEqualTo(0);
119+
assertThat(wrapped.remaining()).isEqualTo(1);
114120

115121
wrapped.flip();
116-
assertThat(wrapped.remaining()).isEqualTo(1);
122+
assertThat(wrapped.remaining()).isEqualTo(0);
117123
}
118124
}

0 commit comments

Comments
 (0)