Skip to content

Commit 7177551

Browse files
authored
Merge pull request #4 from appulse-projects/develop
Add unsigned methods
2 parents d238388 + 6a26263 commit 7177551

7 files changed

Lines changed: 218 additions & 17 deletions

File tree

CHANGELOG.md

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

15+
## [1.3.1](https://github.com/appulse-projects/utils-java/releases/tag/1.3.1) - 2018-02-09
16+
17+
Minor release with unsigned numbers support.
18+
19+
### Added
20+
21+
- Byte arrays conversion to unsigned byte/short/int.
22+
- Unsigned tests.
23+
24+
### Changed
25+
26+
- Exceptions in tests have full stack traces.
27+
1528
## [1.3.0](https://github.com/appulse-projects/utils-java/releases/tag/1.3.0) - 2018-02-01
1629

1730
Add resource utils class and made small refactorign.

pom.xml

Lines changed: 3 additions & 2 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.3.0</version>
27+
<version>1.3.1</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.3.0</tag>
65+
<tag>1.3.1</tag>
6666
</scm>
6767

6868
<distributionManagement>
@@ -176,6 +176,7 @@ limitations under the License.
176176
<artifactId>maven-surefire-plugin</artifactId>
177177
<version>2.20.1</version>
178178
<configuration>
179+
<trimStackTrace>false</trimStackTrace>
179180
<includes>
180181
<include>**/*Test.java</include>
181182
</includes>

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import lombok.RequiredArgsConstructor;
2525
import lombok.experimental.FieldDefaults;
26+
import lombok.val;
2627

2728
/**
2829
*
@@ -35,6 +36,30 @@ final class BytesDelegateGets {
3536

3637
Bytes bytes;
3738

39+
/**
40+
* Returns unsigned byte as short integer (2 bytes).
41+
*
42+
* @return unsigned byte
43+
*
44+
* @since 1.3.1
45+
*/
46+
public short getUnsignedByte () {
47+
return BytesUtils.asUnsignedByte(bytes.getByte());
48+
}
49+
50+
/**
51+
* Returns unsigned byte as short integer (2 bytes) from specific position.
52+
*
53+
* @param index index in buffer, where extract unsigned byte.
54+
*
55+
* @return unsigned byte
56+
*
57+
* @since 1.3.1
58+
*/
59+
public short getUnsignedByte (int index) {
60+
return BytesUtils.asUnsignedByte(bytes.getByte(index));
61+
}
62+
3863
public char getChar () {
3964
return bytes.getBuffer().getChar();
4065
}
@@ -55,14 +80,59 @@ public short getShort (int index) {
5580
return bytes.getBuffer().getShort(index);
5681
}
5782

83+
/**
84+
* Returns unsigned short (2 bytes) as integer (4 bytes) from specific position.
85+
*
86+
* @param index index in buffer, where extract unsigned short.
87+
*
88+
* @return unsigned short
89+
*
90+
* @since 1.3.1
91+
*/
92+
public int getUnsignedShort (int index) {
93+
return getShort(index) & 0xFFFF;
94+
}
95+
5896
public int getInt () {
5997
return bytes.getBuffer().getInt();
6098
}
6199

100+
/**
101+
* Returns unsigned integer (4 bytes) as long (8 bytes).
102+
*
103+
* @return unsigned integer
104+
*
105+
* @since 1.3.1
106+
*/
107+
public long getUnsignedInt () {
108+
val intBytes = bytes.getBytes(Integer.BYTES);
109+
return BytesUtils.asUnsignedInteger(intBytes);
110+
}
111+
62112
public int getInt (int index) {
63113
return bytes.getBuffer().getInt(index);
64114
}
65115

116+
/**
117+
* Returns unsigned integer (4 bytes) as long (8 bytes) from specific position.
118+
*
119+
* @param index index in buffer, where extract unsigned intger.
120+
*
121+
* @return unsigned integer
122+
*
123+
* @since 1.3.1
124+
*/
125+
public long getUnsignedInt (int index) {
126+
val oldPosition = bytes.position();
127+
try {
128+
bytes.position(index);
129+
val intBytes = bytes.getBytes(Integer.BYTES);
130+
return BytesUtils.asUnsignedInteger(intBytes);
131+
} finally {
132+
bytes.position(oldPosition);
133+
}
134+
}
135+
66136
public long getLong () {
67137
return bytes.getBuffer().getLong();
68138
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
* @author Artem Labazin
3232
* @since 1.0.0
3333
*/
34-
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
3534
@RequiredArgsConstructor
3635
@FieldDefaults(level = PRIVATE, makeFinal = true)
36+
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
3737
final class BytesDelegatePuts {
3838

3939
Bytes bytes;

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,50 @@ public static byte[] asBytes (double value) {
8383
return asBytes(Double.doubleToRawLongBits(value));
8484
}
8585

86+
/**
87+
* Converts byte to unsigned byte.
88+
*
89+
* @param value byte value
90+
*
91+
* @return unsigned byte value as short integer
92+
*
93+
* @since 1.3.1
94+
*/
95+
public static short asUnsignedByte (byte value) {
96+
return asUnsignedByte(new byte[] { value });
97+
}
98+
99+
/**
100+
* Transforms byte array to unsigned byte value as short integer.
101+
*
102+
* @param bytes byte array
103+
*
104+
* @return unsigned byte
105+
*
106+
* @since 1.3.1
107+
*/
108+
public static short asUnsignedByte (@NonNull byte[] bytes) {
109+
return asShort(bytes);
110+
}
111+
86112
public static short asShort (@NonNull byte[] bytes) {
87113
val aligned = align(bytes, Short.BYTES);
88114
return (short) ((aligned[0] << 8) | (aligned[1] & 0xff));
89115
}
90116

117+
/**
118+
* Transforms byte array to unsigned short integer value as integer.
119+
*
120+
* @param bytes byte array
121+
*
122+
* @return unsigned short integer
123+
*
124+
* @since 1.3.1
125+
*/
126+
public static int asUnsignedShort (@NonNull byte[] bytes) {
127+
return asShort(bytes) & 0xFFFF;
128+
}
129+
91130
public static char asChar (@NonNull byte[] bytes) {
92131
val aligned = align(bytes, Short.BYTES);
93132
return (char) ((aligned[0] << 8) | (aligned[1] & 0xff));
@@ -101,6 +140,19 @@ public static int asInteger (@NonNull byte[] bytes) {
101140
| (aligned[3] & 0xff);
102141
}
103142

143+
/**
144+
* Transforms byte array to unsigned integer value as long integer.
145+
*
146+
* @param bytes byte array
147+
*
148+
* @return unsigned integer
149+
*
150+
* @since 1.3.1
151+
*/
152+
public static long asUnsignedInteger (@NonNull byte[] bytes) {
153+
return asLong(bytes);
154+
}
155+
104156
public static long asLong (@NonNull byte[] bytes) {
105157
val aligned = align(bytes, Long.BYTES);
106158
return ((long) aligned[0] << 56)

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,54 @@ public void array () {
9797

9898
@Test
9999
public void limit () {
100-
Bytes bytes = Bytes.allocate(2);
101-
assertThat(bytes.limit()).isEqualTo(0);
100+
Bytes bytes = Bytes.allocate(2);
101+
assertThat(bytes.limit()).isEqualTo(0);
102102

103-
bytes.put4B(4);
104-
assertThat(bytes.limit()).isEqualTo(4);
103+
bytes.put4B(4);
104+
assertThat(bytes.limit()).isEqualTo(4);
105105

106-
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
107-
assertThat(wrapped.limit()).isEqualTo(1);
106+
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
107+
assertThat(wrapped.limit()).isEqualTo(1);
108108
}
109109

110110
@Test
111111
public void remaining () {
112-
Bytes bytes = Bytes.allocate(2);
113-
assertThat(bytes.remaining()).isEqualTo(0);
112+
Bytes bytes = Bytes.allocate(2);
113+
assertThat(bytes.remaining()).isEqualTo(0);
114114

115-
bytes.put4B(4);
116-
assertThat(bytes.remaining()).isEqualTo(0);
115+
bytes.put4B(4);
116+
assertThat(bytes.remaining()).isEqualTo(0);
117117

118-
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
119-
assertThat(wrapped.remaining()).isEqualTo(1);
118+
Bytes wrapped = Bytes.wrap(new byte[] { 1 });
119+
assertThat(wrapped.remaining()).isEqualTo(1);
120120

121-
wrapped.flip();
122-
assertThat(wrapped.remaining()).isEqualTo(0);
121+
wrapped.flip();
122+
assertThat(wrapped.remaining()).isEqualTo(0);
123+
}
124+
125+
@Test
126+
public void unsignedTest () {
127+
Bytes bytes = Bytes.allocate()
128+
.put1B(254)
129+
.put2B(62994)
130+
.put4B(4_100_000_000L);
131+
132+
assertThat(bytes.getByte(0))
133+
.isNotEqualTo(254);
134+
135+
assertThat(bytes.getUnsignedByte(0))
136+
.isEqualTo((short) 254);
137+
138+
assertThat(bytes.getShort(1))
139+
.isNotEqualTo(62994);
140+
141+
assertThat(bytes.getUnsignedShort(1))
142+
.isEqualTo(62994);
143+
144+
assertThat(bytes.getInt(3))
145+
.isNotEqualTo(4_100_000_000L);
146+
147+
assertThat(bytes.getUnsignedInt(3))
148+
.isEqualTo(4_100_000_000L);
123149
}
124150
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public void align () {
6969
.isEqualTo(new byte[] { 3, 4 });
7070
}
7171

72+
@Test
73+
public void asUnsignedByte () {
74+
val bytes = ByteBuffer.allocate(Byte.BYTES)
75+
.put((byte) 254)
76+
.array();
77+
78+
assertThat(bytes[0])
79+
.isNotEqualTo(254);
80+
81+
assertThat(BytesUtils.asUnsignedByte(bytes))
82+
.isNotEqualTo(254);
83+
}
84+
7285
@Test
7386
public void asShort () {
7487
val bytes1 = ByteBuffer.allocate(Short.BYTES)
@@ -93,6 +106,19 @@ public void asShort () {
93106
.isEqualTo((short) 42);
94107
}
95108

109+
@Test
110+
public void asUnsignedShort () {
111+
val bytes = ByteBuffer.allocate(Short.BYTES)
112+
.putShort((short) 62994)
113+
.array();
114+
115+
assertThat(BytesUtils.asShort(bytes))
116+
.isNotEqualTo(62994);
117+
118+
assertThat(BytesUtils.asUnsignedShort(bytes))
119+
.isEqualTo(62994);
120+
}
121+
96122
@Test
97123
public void asChar () {
98124
val bytes = ByteBuffer.allocate(Character.BYTES)
@@ -127,6 +153,19 @@ public void asInteger () {
127153
.isEqualTo(42);
128154
}
129155

156+
@Test
157+
public void asUnsignedInteger () {
158+
val bytes = ByteBuffer.allocate(Integer.BYTES)
159+
.putInt((int) 4_100_000_000L)
160+
.array();
161+
162+
assertThat(BytesUtils.asInteger(bytes))
163+
.isNotEqualTo(4_100_000_000L);
164+
165+
assertThat(BytesUtils.asUnsignedInteger(bytes))
166+
.isEqualTo(4_100_000_000L);
167+
}
168+
130169
@Test
131170
public void asLong () {
132171
val bytes1 = ByteBuffer.allocate(Long.BYTES)

0 commit comments

Comments
 (0)