Skip to content

Commit e100681

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Speed up BytesUtil
1 parent 59cc101 commit e100681

4 files changed

Lines changed: 177 additions & 8 deletions

File tree

CHANGELOG.md

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

1212
- Add more tests.
1313

14+
## [1.0.2](https://github.com/appulse-projects/utils-java/releases/tag/1.0.2) - 2018-01-28
15+
16+
### Changed
17+
18+
- Speed up `BytesUtil` methods.
19+
1420
## [1.0.1](https://github.com/appulse-projects/utils-java/releases/tag/1.0.1) - 2018-01-27
1521

1622
### Added

pom.xml

Lines changed: 2 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.0.1</version>
27+
<version>1.0.2</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.1</tag>
65+
<tag>1.0.2</tag>
6666
</scm>
6767

6868
<distributionManagement>

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,44 @@ public static byte[] asBytes (double value) {
7979
return asBytes(Double.doubleToRawLongBits(value));
8080
}
8181

82-
public static int asInteger (@NonNull ByteBuffer buffer, int length) {
83-
val bytes = new byte[length];
84-
buffer.get(bytes, 0, length);
85-
return asInteger(bytes);
82+
public static short asShort (@NonNull byte[] bytes) {
83+
val aligned = align(bytes, Short.BYTES);
84+
return (short) ((aligned[0] << 8) | (aligned[1] & 0xff));
85+
}
86+
87+
public static char asChar (@NonNull byte[] bytes) {
88+
val aligned = align(bytes, Short.BYTES);
89+
return (char) ((aligned[0] << 8) | (aligned[1] & 0xff));
8690
}
8791

8892
public static int asInteger (@NonNull byte[] bytes) {
8993
val aligned = align(bytes, Integer.BYTES);
90-
return ByteBuffer.wrap(aligned).getInt();
94+
return (aligned[0] << 24)
95+
| ((aligned[1] & 0xff) << 16)
96+
| ((aligned[2] & 0xff) << 8)
97+
| (aligned[3] & 0xff);
98+
}
99+
100+
public static long asLong (@NonNull byte[] bytes) {
101+
val aligned = align(bytes, Long.BYTES);
102+
return ((long) aligned[0] << 56)
103+
| (((long) aligned[1] & 0xff) << 48)
104+
| (((long) aligned[2] & 0xff) << 40)
105+
| (((long) aligned[3] & 0xff) << 32)
106+
| (((long) aligned[4] & 0xff) << 24)
107+
| (((long) aligned[5] & 0xff) << 16)
108+
| (((long) aligned[6] & 0xff) << 8)
109+
| ((long) aligned[7] & 0xff);
110+
}
111+
112+
public static float asFloat (@NonNull byte[] bytes) {
113+
val aligned = align(bytes, Float.BYTES);
114+
return Float.intBitsToFloat(asInteger(aligned));
115+
}
116+
117+
public static double asDouble (@NonNull byte[] bytes) {
118+
val aligned = align(bytes, Double.BYTES);
119+
return Double.longBitsToDouble(asLong(aligned));
91120
}
92121

93122
public static byte[] concatenate (@NonNull byte[]... arrays) {
@@ -101,7 +130,7 @@ public static byte[] concatenate (@NonNull byte[]... arrays) {
101130
}
102131

103132
public static byte[] align (@NonNull byte[] bytes, int length) {
104-
if (length <= 0) {
133+
if (length <= 0 || bytes.length == length) {
105134
return bytes;
106135
}
107136

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21+
import java.nio.ByteBuffer;
22+
2123
import org.junit.Test;
2224

25+
import lombok.val;
26+
2327
public class BytesUtilTest {
2428

2529
@Test
@@ -60,4 +64,134 @@ public void align () {
6064
assertThat(BytesUtil.align(new byte[] { 1, 2, 3, 4 }, 2))
6165
.isEqualTo(new byte[] { 3, 4 });
6266
}
67+
68+
@Test
69+
public void asShort () {
70+
val bytes1 = ByteBuffer.allocate(Short.BYTES)
71+
.putShort(Short.MAX_VALUE)
72+
.array();
73+
74+
assertThat(BytesUtil.asShort(bytes1))
75+
.isEqualTo(Short.MAX_VALUE);
76+
77+
val bytes2 = ByteBuffer.allocate(Short.BYTES)
78+
.putShort(Short.MIN_VALUE)
79+
.array();
80+
81+
assertThat(BytesUtil.asShort(bytes2))
82+
.isEqualTo(Short.MIN_VALUE);
83+
84+
val bytes3 = ByteBuffer.allocate(Short.BYTES)
85+
.putShort((short) 42)
86+
.array();
87+
88+
assertThat(BytesUtil.asShort(bytes3))
89+
.isEqualTo((short) 42);
90+
}
91+
92+
@Test
93+
public void asChar () {
94+
val bytes = ByteBuffer.allocate(Character.BYTES)
95+
.putChar('z')
96+
.array();
97+
98+
assertThat(BytesUtil.asChar(bytes))
99+
.isEqualTo('z');
100+
}
101+
102+
@Test
103+
public void asInteger () {
104+
val bytes1 = ByteBuffer.allocate(Integer.BYTES)
105+
.putInt(Integer.MAX_VALUE)
106+
.array();
107+
108+
assertThat(BytesUtil.asInteger(bytes1))
109+
.isEqualTo(Integer.MAX_VALUE);
110+
111+
val bytes2 = ByteBuffer.allocate(Integer.BYTES)
112+
.putInt(Integer.MIN_VALUE)
113+
.array();
114+
115+
assertThat(BytesUtil.asInteger(bytes2))
116+
.isEqualTo(Integer.MIN_VALUE);
117+
118+
val bytes3 = ByteBuffer.allocate(Integer.BYTES)
119+
.putInt(42)
120+
.array();
121+
122+
assertThat(BytesUtil.asInteger(bytes3))
123+
.isEqualTo(42);
124+
}
125+
126+
@Test
127+
public void asLong () {
128+
val bytes1 = ByteBuffer.allocate(Long.BYTES)
129+
.putLong(Long.MAX_VALUE)
130+
.array();
131+
132+
assertThat(BytesUtil.asLong(bytes1))
133+
.isEqualTo(Long.MAX_VALUE);
134+
135+
val bytes2 = ByteBuffer.allocate(Long.BYTES)
136+
.putLong(Long.MIN_VALUE)
137+
.array();
138+
139+
assertThat(BytesUtil.asLong(bytes2))
140+
.isEqualTo(Long.MIN_VALUE);
141+
142+
val bytes3 = ByteBuffer.allocate(Long.BYTES)
143+
.putLong(42L)
144+
.array();
145+
146+
assertThat(BytesUtil.asLong(bytes3))
147+
.isEqualTo(42L);
148+
}
149+
150+
@Test
151+
public void asFloat () {
152+
val bytes1 = ByteBuffer.allocate(Float.BYTES)
153+
.putFloat(Float.MAX_VALUE)
154+
.array();
155+
156+
assertThat(BytesUtil.asFloat(bytes1))
157+
.isEqualTo(Float.MAX_VALUE);
158+
159+
val bytes2 = ByteBuffer.allocate(Float.BYTES)
160+
.putFloat(Float.MIN_VALUE)
161+
.array();
162+
163+
assertThat(BytesUtil.asFloat(bytes2))
164+
.isEqualTo(Float.MIN_VALUE);
165+
166+
val bytes3 = ByteBuffer.allocate(Float.BYTES)
167+
.putFloat(.5F)
168+
.array();
169+
170+
assertThat(BytesUtil.asFloat(bytes3))
171+
.isEqualTo(.5F);
172+
}
173+
174+
@Test
175+
public void asDouble () {
176+
val bytes1 = ByteBuffer.allocate(Double.BYTES)
177+
.putDouble(Double.MAX_VALUE)
178+
.array();
179+
180+
assertThat(BytesUtil.asDouble(bytes1))
181+
.isEqualTo(Double.MAX_VALUE);
182+
183+
val bytes2 = ByteBuffer.allocate(Double.BYTES)
184+
.putDouble(Double.MIN_VALUE)
185+
.array();
186+
187+
assertThat(BytesUtil.asDouble(bytes2))
188+
.isEqualTo(Double.MIN_VALUE);
189+
190+
val bytes3 = ByteBuffer.allocate(Double.BYTES)
191+
.putDouble(.5D)
192+
.array();
193+
194+
assertThat(BytesUtil.asDouble(bytes3))
195+
.isEqualTo(.5D);
196+
}
63197
}

0 commit comments

Comments
 (0)