Skip to content

Commit 9e37539

Browse files
committed
Speed-up unsigned functions
1 parent f45ecbd commit 9e37539

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

CHANGELOG.md

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

15+
## [1.5.2](https://github.com/appulse-projects/utils-java/releases/tag/1.5.2) - 2018-04-22
16+
17+
Speed-up unsigned functions execution.
18+
19+
### Changed
20+
21+
- byte/short/int unsigned functions became more fast.
22+
- Catched exception in `ExecutorServiceWithClientTraceTest` rethrows again.
23+
1524
## [1.5.1](https://github.com/appulse-projects/utils-java/releases/tag/1.5.1) - 2018-04-07
1625

1726
Add fancy logging for `ExecutorServiceWithClientTraceTest`.

pom.xml

Lines changed: 1 addition & 1 deletion
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.5.1</version>
27+
<version>1.5.2</version>
2828
<packaging>jar</packaging>
2929

3030
<properties>

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static byte[] asBytes (double value) {
9393
* @since 1.3.1
9494
*/
9595
public static short asUnsignedByte (byte value) {
96-
return asUnsignedByte(new byte[] { value });
96+
return (short) (value & 0xff);
9797
}
9898

9999
/**
@@ -111,15 +111,28 @@ public static short asUnsignedByte (@NonNull byte[] bytes) {
111111

112112
public static short asShort (@NonNull byte[] bytes) {
113113
val aligned = align(bytes, Short.BYTES);
114-
return (short) ((aligned[0] << 8) | (aligned[1] & 0xff));
114+
return (short) ((aligned[0] << 8) | (aligned[1] & 0xff));
115+
}
116+
117+
/**
118+
* Transforms byte array to unsigned short integer value as integer.
119+
*
120+
* @param value signed short value
121+
*
122+
* @return unsigned short as integer
123+
*
124+
* @since 1.5.2
125+
*/
126+
public static int asUnsignedShort (short value) {
127+
return value & 0xFFFF;
115128
}
116129

117130
/**
118131
* Transforms byte array to unsigned short integer value as integer.
119132
*
120133
* @param bytes byte array
121134
*
122-
* @return unsigned short integer
135+
* @return unsigned short as integer
123136
*
124137
* @since 1.3.1
125138
*/
@@ -129,23 +142,36 @@ public static int asUnsignedShort (@NonNull byte[] bytes) {
129142

130143
public static char asChar (@NonNull byte[] bytes) {
131144
val aligned = align(bytes, Short.BYTES);
132-
return (char) ((aligned[0] << 8) | (aligned[1] & 0xff));
145+
return (char) ((aligned[0] << 8) | (aligned[1] & 0xff));
133146
}
134147

135148
public static int asInteger (@NonNull byte[] bytes) {
136149
val aligned = align(bytes, Integer.BYTES);
137150
return (aligned[0] << 24)
138151
| ((aligned[1] & 0xff) << 16)
139-
| ((aligned[2] & 0xff) << 8)
152+
| ((aligned[2] & 0xff) << 8)
140153
| (aligned[3] & 0xff);
141154
}
142155

156+
/**
157+
* Transforms byte array to unsigned integer value as long integer.
158+
*
159+
* @param value signed integer value
160+
*
161+
* @return unsigned integer as long
162+
*
163+
* @since 1.5.2
164+
*/
165+
public static long asUnsignedInteger (int value) {
166+
return value & 0x00000000FFFFFFFFL;
167+
}
168+
143169
/**
144170
* Transforms byte array to unsigned integer value as long integer.
145171
*
146172
* @param bytes byte array
147173
*
148-
* @return unsigned integer
174+
* @return unsigned integer as long
149175
*
150176
* @since 1.3.1
151177
*/
@@ -161,7 +187,7 @@ public static long asLong (@NonNull byte[] bytes) {
161187
| (((long) aligned[3] & 0xff) << 32)
162188
| (((long) aligned[4] & 0xff) << 24)
163189
| (((long) aligned[5] & 0xff) << 16)
164-
| (((long) aligned[6] & 0xff) << 8)
190+
| (((long) aligned[6] & 0xff) << 8)
165191
| ((long) aligned[7] & 0xff);
166192
}
167193

src/main/java/io/appulse/utils/threads/executor/ExecutorServiceWithClientTrace.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void execute (@NonNull Runnable command) {
4848
} catch (Exception ex) {
4949
log.error("Exception during task execution submitted from thread '{}'",
5050
threadName, merge(clientStack, ex));
51+
throw ex;
5152
}
5253
});
5354
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ public void asUnsignedShort () {
117117

118118
assertThat(BytesUtils.asUnsignedShort(bytes))
119119
.isEqualTo(62994);
120+
121+
assertThat(BytesUtils.asUnsignedShort((short) -1))
122+
.isEqualTo(65535);
120123
}
121124

122125
@Test
@@ -164,6 +167,9 @@ public void asUnsignedInteger () {
164167

165168
assertThat(BytesUtils.asUnsignedInteger(bytes))
166169
.isEqualTo(4_100_000_000L);
170+
171+
assertThat(BytesUtils.asUnsignedInteger(-1))
172+
.isEqualTo(4_294_967_295L);
167173
}
168174

169175
@Test

0 commit comments

Comments
 (0)