Skip to content

Commit d8e3074

Browse files
authored
Merge pull request #2 from appulse-projects/develop
Add socket utils class
2 parents 791a796 + 83a25e6 commit d8e3074

5 files changed

Lines changed: 78 additions & 9 deletions

File tree

CHANGELOG.md

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

1212
- Add more tests.
13+
- Add `JavaDoc`.
14+
15+
## [1.2.0](https://github.com/appulse-projects/utils-java/releases/tag/1.2.0) - 2018-02-01
16+
17+
Add socket utils class, with help functions related to sockets work.
18+
19+
### Added
20+
21+
- Method `array(int)` in `Bytes` for getting `byte[]` with offset.
22+
- Added port related functions to `StreamReader`.
23+
24+
### Changed
25+
26+
- Renamed `StreamReader` to `SocketUtils`.
1327

1428
## [1.1.0](https://github.com/appulse-projects/utils-java/releases/tag/1.1.0) - 2018-01-30
1529

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.1.0</version>
27+
<version>1.2.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.1.0</tag>
65+
<tag>1.2.0</tag>
6666
</scm>
6767

6868
<distributionManagement>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ public byte[] array () {
131131
return Arrays.copyOfRange(buffer.array(), 0, limit);
132132
}
133133

134+
/**
135+
* Returns byte array representation of class, starting from specified position.
136+
*
137+
* @param offset start position
138+
*
139+
* @return byte array representation
140+
*
141+
* @throws IndexOutOfBoundsException if offset is lower zero or larger the limit
142+
*
143+
* @since 1.2.0
144+
*/
145+
public byte[] array (int offset) {
146+
if (offset < 0 || offset > limit) {
147+
throw new IndexOutOfBoundsException();
148+
}
149+
return Arrays.copyOfRange(buffer.array(), offset, limit);
150+
}
151+
134152
public int limit () {
135153
return limit;
136154
}

src/main/java/io/appulse/utils/StreamReader.java renamed to src/main/java/io/appulse/utils/SocketUtils.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@
1616

1717
package io.appulse.utils;
1818

19+
import static java.util.Optional.empty;
20+
import static java.util.Optional.of;
21+
import static java.util.concurrent.TimeUnit.SECONDS;
22+
1923
import java.io.ByteArrayOutputStream;
2024
import java.io.EOFException;
25+
import java.io.IOException;
2126
import java.io.InputStream;
27+
import java.net.InetSocketAddress;
2228
import java.net.Socket;
29+
import java.util.Optional;
2330

2431
import lombok.NonNull;
2532
import lombok.SneakyThrows;
@@ -28,9 +35,31 @@
2835
/**
2936
*
3037
* @author Artem Labazin
31-
* @since 1.1.0
38+
* @since 1.2.0
3239
*/
33-
public final class StreamReader {
40+
public final class SocketUtils {
41+
42+
public static Optional<Integer> findFreePort () {
43+
return findFreePort(1024, 65535);
44+
}
45+
46+
public static Optional<Integer> findFreePort (int from, int to) {
47+
for (int port = from; port <= to; port++) {
48+
if (isPortAvailable(port)) {
49+
return of(port);
50+
}
51+
}
52+
return empty();
53+
}
54+
55+
public static boolean isPortAvailable (int port) {
56+
try (Socket socket = new Socket()) {
57+
socket.connect(new InetSocketAddress("localhost", port), (int) SECONDS.toMillis(1));
58+
return false;
59+
} catch (IOException ex) {
60+
return true;
61+
}
62+
}
3463

3564
@SneakyThrows
3665
public static byte[] read (@NonNull InputStream stream) {
@@ -98,6 +127,6 @@ public static Bytes readBytes (@NonNull Socket socket, int fixedLength) {
98127
return readBytes(socket.getInputStream(), fixedLength);
99128
}
100129

101-
private StreamReader () {
130+
private SocketUtils () {
102131
}
103132
}

src/test/java/io/appulse/utils/StreamReaderTest.java renamed to src/test/java/io/appulse/utils/SocketUtilsTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.util.Arrays;
24+
import java.util.Optional;
2425

2526
import org.junit.Test;
2627

@@ -30,15 +31,22 @@
3031
/**
3132
*
3233
* @author Artem Labazin
33-
* @since 1.1.0
34+
* @since 1.2.0
3435
*/
35-
public class StreamReaderTest {
36+
public class SocketUtilsTest {
37+
38+
@Test
39+
public void isPortAvailable () {
40+
Optional<Integer> port = SocketUtils.findFreePort();
41+
assertThat(port).isPresent();
42+
assertThat(SocketUtils.isPortAvailable(port.get())).isTrue();
43+
}
3644

3745
@Test
3846
public void read () throws Exception {
3947
byte[] expected = "Hello world".getBytes();
4048

41-
assertThat(StreamReader.read(new CustomInputStream(expected)))
49+
assertThat(SocketUtils.read(new CustomInputStream(expected)))
4250
.isEqualTo(expected);
4351
}
4452

@@ -47,7 +55,7 @@ public void readWithLength () throws Exception {
4755
byte[] bytes = "Hello world".getBytes();
4856
byte[] expected = Arrays.copyOfRange(bytes, 0, 2);
4957

50-
assertThat(StreamReader.read(new CustomInputStream(bytes), 2))
58+
assertThat(SocketUtils.read(new CustomInputStream(bytes), 2))
5159
.isEqualTo(expected);
5260
}
5361

0 commit comments

Comments
 (0)