Skip to content

Commit d238388

Browse files
authored
Merge pull request #3 from appulse-projects/develop
Add resource utils class
2 parents f8e6a83 + d5c9dce commit d238388

11 files changed

Lines changed: 291 additions & 141 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.0](https://github.com/appulse-projects/utils-java/releases/tag/1.3.0) - 2018-02-01
16+
17+
Add resource utils class and made small refactorign.
18+
19+
### Added
20+
21+
- `ResourceUtils` which contains resource related functions.
22+
23+
### Changed
24+
25+
- Renamed `BytesUtil` to `BytesUtils`.
26+
- Moved methods for reading `InputStream` to `BytesUtils`.
27+
1528
## [1.2.1](https://github.com/appulse-projects/utils-java/releases/tag/1.2.1) - 2018-02-01
1629

1730
Add socket utils class, with help functions related to sockets work.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ $> mvn clean compile
3434
[INFO] ------------------------------------------------------------------------
3535
[INFO] BUILD SUCCESS
3636
[INFO] ------------------------------------------------------------------------
37-
[INFO] Total time: 13.033 s
38-
[INFO] Finished at: 2018-01-30T00:29:40+03:00
39-
[INFO] Final Memory: 43M/467M
37+
[INFO] Total time: 12.712 s
38+
[INFO] Finished at: 2018-02-01T15:23:30+03:00
39+
[INFO] Final Memory: 44M/585M
4040
[INFO] ------------------------------------------------------------------------
4141
```
4242

@@ -54,7 +54,7 @@ $> mvn clean test
5454
[INFO]
5555
[INFO] Results:
5656
[INFO]
57-
[INFO] Tests run: 19, Failures: 0, Errors: 0, Skipped: 0
57+
[INFO] Tests run: 22, Failures: 0, Errors: 0, Skipped: 0
5858
[INFO]
5959
...
6060
```

pom.xml

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

6868
<distributionManagement>
@@ -259,7 +259,7 @@ limitations under the License.
259259
<dependency>
260260
<groupId>com.puppycrawl.tools</groupId>
261261
<artifactId>checkstyle</artifactId>
262-
<version>8.7</version>
262+
<version>8.8</version>
263263
</dependency>
264264
</dependencies>
265265
<executions>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package io.appulse.utils;
1818

19-
import static io.appulse.utils.BytesUtil.align;
20-
import static io.appulse.utils.BytesUtil.asBytes;
19+
import static io.appulse.utils.BytesUtils.align;
20+
import static io.appulse.utils.BytesUtils.asBytes;
2121
import static java.nio.charset.StandardCharsets.UTF_8;
2222
import static lombok.AccessLevel.PRIVATE;
2323

src/main/java/io/appulse/utils/BytesUtil.java renamed to src/main/java/io/appulse/utils/BytesUtils.java

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818

1919
import static java.lang.Math.min;
2020

21+
import java.io.ByteArrayOutputStream;
22+
import java.io.EOFException;
23+
import java.io.InputStream;
2124
import java.nio.ByteBuffer;
2225
import java.util.stream.Stream;
2326

2427
import lombok.NonNull;
28+
import lombok.SneakyThrows;
2529
import lombok.val;
2630

2731
/**
2832
*
2933
* @author Artem Labazin
30-
* @since 1.0.0
34+
* @since 1.3.0
3135
*/
32-
public final class BytesUtil {
36+
public final class BytesUtils {
3337

3438
public static byte[] asBytes (char value) {
3539
return new byte[] {
@@ -148,6 +152,90 @@ public static byte[] align (@NonNull byte[] bytes, int length) {
148152
return result;
149153
}
150154

151-
private BytesUtil () {
155+
/**
156+
* Reads all bytes from stream till the end of the stream.
157+
*
158+
* @param stream steam
159+
*
160+
* @return readed bytes array
161+
*
162+
* @since 1.3.0
163+
*/
164+
@SneakyThrows
165+
public static byte[] read (@NonNull InputStream stream) {
166+
val outputStream = new ByteArrayOutputStream(32);
167+
val buffer = new byte[32];
168+
169+
while (true) {
170+
val length = stream.read(buffer);
171+
if (length == -1) {
172+
break;
173+
}
174+
outputStream.write(buffer, 0, length);
175+
}
176+
177+
return outputStream.toByteArray();
178+
}
179+
180+
/**
181+
* Reads fixed length bytes from the stream.
182+
*
183+
* @param stream steam
184+
* @param length how many bytes to read from stream
185+
*
186+
* @return readed bytes array
187+
*
188+
* @since 1.3.0
189+
*/
190+
@SneakyThrows
191+
public static byte[] read (@NonNull InputStream stream, int length) {
192+
if (length < 0) {
193+
throw new IndexOutOfBoundsException();
194+
}
195+
196+
val result = new byte[length];
197+
int readed = 0;
198+
199+
while (readed < length) {
200+
val count = stream.read(result, readed, length - readed);
201+
if (count < -1) {
202+
throw new EOFException();
203+
}
204+
readed += count;
205+
}
206+
207+
return result;
208+
}
209+
210+
/**
211+
* Reads all bytes from stream till the end of the stream.
212+
*
213+
* @param stream steam
214+
*
215+
* @return readed bytes array wrapped in {@link Bytes} instance
216+
*
217+
* @since 1.3.0
218+
*/
219+
public static Bytes readBytes (@NonNull InputStream stream) {
220+
val result = read(stream);
221+
return Bytes.wrap(result);
222+
}
223+
224+
/**
225+
* Reads fixed length bytes from the stream.
226+
*
227+
* @param stream steam
228+
* @param length how many bytes to read from stream
229+
*
230+
* @return readed bytes array wrapped in {@link Bytes} instance
231+
*
232+
* @since 1.3.0
233+
*/
234+
public static Bytes readBytes (@NonNull InputStream stream, int length) {
235+
val result = read(stream, length);
236+
return Bytes.wrap(result);
237+
}
238+
239+
private BytesUtils () {
152240
}
153241
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appulse.utils;
18+
19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
import static java.util.Optional.empty;
21+
import static java.util.Optional.of;
22+
23+
import java.io.InputStream;
24+
import java.nio.charset.Charset;
25+
import java.util.Optional;
26+
27+
import lombok.NonNull;
28+
import lombok.val;
29+
30+
/**
31+
*
32+
* @author Artem Labazin
33+
* @since 1.3.0
34+
*/
35+
public final class ResourceUtils {
36+
37+
public static Optional<String> getResource (@NonNull String name) {
38+
return getResource(name, UTF_8);
39+
}
40+
41+
public static Optional<String> getResource (@NonNull String name, @NonNull Charset charset) {
42+
val classLoader = Thread.currentThread().getContextClassLoader();
43+
InputStream inputStream = classLoader.getResourceAsStream(name);
44+
if (inputStream == null) {
45+
inputStream = ResourceUtils.class.getResourceAsStream(name);
46+
if (inputStream == null) {
47+
return empty();
48+
}
49+
}
50+
51+
val bytes = BytesUtils.read(inputStream);
52+
val string = new String(bytes, charset);
53+
return of(string);
54+
}
55+
56+
private ResourceUtils () {
57+
}
58+
}

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

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@
2020
import static java.util.Optional.of;
2121
import static java.util.concurrent.TimeUnit.SECONDS;
2222

23-
import java.io.ByteArrayOutputStream;
24-
import java.io.EOFException;
2523
import java.io.IOException;
26-
import java.io.InputStream;
2724
import java.net.InetSocketAddress;
2825
import java.net.Socket;
2926
import java.util.Optional;
3027

3128
import lombok.NonNull;
3229
import lombok.SneakyThrows;
33-
import lombok.val;
3430

3531
/**
3632
*
@@ -61,70 +57,24 @@ public static boolean isPortAvailable (int port) {
6157
}
6258
}
6359

64-
@SneakyThrows
65-
public static byte[] read (@NonNull InputStream stream) {
66-
val outputStream = new ByteArrayOutputStream(32);
67-
val buffer = new byte[32];
68-
69-
while (true) {
70-
val length = stream.read(buffer);
71-
if (length == -1) {
72-
break;
73-
}
74-
outputStream.write(buffer, 0, length);
75-
}
76-
77-
return outputStream.toByteArray();
78-
}
79-
8060
@SneakyThrows
8161
public static byte[] read (@NonNull Socket socket) {
82-
return read(socket.getInputStream());
83-
}
84-
85-
@SneakyThrows
86-
public static byte[] read (@NonNull InputStream stream, int length) {
87-
if (length < 0) {
88-
throw new IndexOutOfBoundsException();
89-
}
90-
91-
val result = new byte[length];
92-
int readed = 0;
93-
94-
while (readed < length) {
95-
val count = stream.read(result, readed, length - readed);
96-
if (count < -1) {
97-
throw new EOFException();
98-
}
99-
readed += count;
100-
}
101-
102-
return result;
62+
return BytesUtils.read(socket.getInputStream());
10363
}
10464

10565
@SneakyThrows
10666
public static byte[] read (@NonNull Socket socket, int length) {
107-
return read(socket.getInputStream(), length);
108-
}
109-
110-
public static Bytes readBytes (@NonNull InputStream stream) {
111-
val result = read(stream);
112-
return Bytes.wrap(result);
67+
return BytesUtils.read(socket.getInputStream(), length);
11368
}
11469

11570
@SneakyThrows
11671
public static Bytes readBytes (@NonNull Socket socket) {
117-
return readBytes(socket.getInputStream());
118-
}
119-
120-
public static Bytes readBytes (@NonNull InputStream stream, int length) {
121-
val result = read(stream, length);
122-
return Bytes.wrap(result);
72+
return BytesUtils.readBytes(socket.getInputStream());
12373
}
12474

12575
@SneakyThrows
12676
public static Bytes readBytes (@NonNull Socket socket, int fixedLength) {
127-
return readBytes(socket.getInputStream(), fixedLength);
77+
return BytesUtils.readBytes(socket.getInputStream(), fixedLength);
12878
}
12979

13080
private SocketUtils () {

0 commit comments

Comments
 (0)