Skip to content

Commit 791a796

Browse files
authored
Merge pull request #1 from appulse-projects/develop
Add StreamReader
2 parents 0d40d4b + 06735b8 commit 791a796

5 files changed

Lines changed: 194 additions & 8 deletions

File tree

CHANGELOG.md

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

1212
- Add more tests.
1313

14-
## [1.0.3](https://github.com/appulse-projects/utils-java/releases/tag/1.0.3) - 2018-01-28
14+
## [1.1.0](https://github.com/appulse-projects/utils-java/releases/tag/1.1.0) - 2018-01-30
15+
16+
### Added
17+
18+
- `StreamReader` for effective reading `InputStream`.
19+
20+
## [1.0.4](https://github.com/appulse-projects/utils-java/releases/tag/1.0.4) - 2018-01-28
1521

1622
### Changed
1723

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ For building routine automation, I am using [maven](https://maven.apache.org).
2929
To build the project, do the following:
3030

3131
```bash
32-
$> mvn clean package
32+
$> mvn clean compile
3333
...
3434
[INFO] ------------------------------------------------------------------------
3535
[INFO] BUILD SUCCESS
3636
[INFO] ------------------------------------------------------------------------
37-
[INFO] Total time: 15.272 s
38-
[INFO] Finished at: 2018-01-27T18:04:44+03:00
39-
[INFO] Final Memory: 47M/483M
37+
[INFO] Total time: 13.033 s
38+
[INFO] Finished at: 2018-01-30T00:29:40+03:00
39+
[INFO] Final Memory: 43M/467M
4040
[INFO] ------------------------------------------------------------------------
4141
```
4242

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

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.4</version>
27+
<version>1.1.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.0.4</tag>
65+
<tag>1.1.0</tag>
6666
</scm>
6767

6868
<distributionManagement>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 java.io.ByteArrayOutputStream;
20+
import java.io.EOFException;
21+
import java.io.InputStream;
22+
import java.net.Socket;
23+
24+
import lombok.NonNull;
25+
import lombok.SneakyThrows;
26+
import lombok.val;
27+
28+
/**
29+
*
30+
* @author Artem Labazin
31+
* @since 1.1.0
32+
*/
33+
public final class StreamReader {
34+
35+
@SneakyThrows
36+
public static byte[] read (@NonNull InputStream stream) {
37+
val outputStream = new ByteArrayOutputStream(32);
38+
val buffer = new byte[32];
39+
40+
while (true) {
41+
val length = stream.read(buffer);
42+
if (length == -1) {
43+
break;
44+
}
45+
outputStream.write(buffer, 0, length);
46+
}
47+
48+
return outputStream.toByteArray();
49+
}
50+
51+
@SneakyThrows
52+
public static byte[] read (@NonNull Socket socket) {
53+
return read(socket.getInputStream());
54+
}
55+
56+
@SneakyThrows
57+
public static byte[] read (@NonNull InputStream stream, int length) {
58+
if (length < 0) {
59+
throw new IndexOutOfBoundsException();
60+
}
61+
62+
val result = new byte[length];
63+
int readed = 0;
64+
65+
while (readed < length) {
66+
val count = stream.read(result, readed, length - readed);
67+
if (count < -1) {
68+
throw new EOFException();
69+
}
70+
readed += count;
71+
}
72+
73+
return result;
74+
}
75+
76+
@SneakyThrows
77+
public static byte[] read (@NonNull Socket socket, int length) {
78+
return read(socket.getInputStream(), length);
79+
}
80+
81+
public static Bytes readBytes (@NonNull InputStream stream) {
82+
val result = read(stream);
83+
return Bytes.wrap(result);
84+
}
85+
86+
@SneakyThrows
87+
public static Bytes readBytes (@NonNull Socket socket) {
88+
return readBytes(socket.getInputStream());
89+
}
90+
91+
public static Bytes readBytes (@NonNull InputStream stream, int length) {
92+
val result = read(stream, length);
93+
return Bytes.wrap(result);
94+
}
95+
96+
@SneakyThrows
97+
public static Bytes readBytes (@NonNull Socket socket, int fixedLength) {
98+
return readBytes(socket.getInputStream(), fixedLength);
99+
}
100+
101+
private StreamReader () {
102+
}
103+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.util.Arrays;
24+
25+
import org.junit.Test;
26+
27+
import lombok.AccessLevel;
28+
import lombok.experimental.FieldDefaults;
29+
30+
/**
31+
*
32+
* @author Artem Labazin
33+
* @since 1.1.0
34+
*/
35+
public class StreamReaderTest {
36+
37+
@Test
38+
public void read () throws Exception {
39+
byte[] expected = "Hello world".getBytes();
40+
41+
assertThat(StreamReader.read(new CustomInputStream(expected)))
42+
.isEqualTo(expected);
43+
}
44+
45+
@Test
46+
public void readWithLength () throws Exception {
47+
byte[] bytes = "Hello world".getBytes();
48+
byte[] expected = Arrays.copyOfRange(bytes, 0, 2);
49+
50+
assertThat(StreamReader.read(new CustomInputStream(bytes), 2))
51+
.isEqualTo(expected);
52+
}
53+
54+
@FieldDefaults(level = AccessLevel.PRIVATE)
55+
private static class CustomInputStream extends InputStream {
56+
57+
final byte[] bytes;
58+
59+
int index;
60+
61+
CustomInputStream (byte[] bytes) {
62+
this.bytes = new byte[bytes.length + 1];
63+
int middle = bytes.length / 2;
64+
System.arraycopy(bytes, 0, this.bytes, 0, middle);
65+
this.bytes[middle] = -1;
66+
System.arraycopy(bytes, middle, this.bytes, middle + 1, bytes.length - middle);
67+
}
68+
69+
@Override
70+
public int read () throws IOException {
71+
if (index >= bytes.length) {
72+
return -1;
73+
}
74+
return bytes[index++];
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)