|
20 | 20 | import static java.util.Optional.of; |
21 | 21 | import static java.util.concurrent.TimeUnit.SECONDS; |
22 | 22 |
|
23 | | -import java.io.ByteArrayOutputStream; |
24 | | -import java.io.EOFException; |
25 | 23 | import java.io.IOException; |
26 | | -import java.io.InputStream; |
27 | 24 | import java.net.InetSocketAddress; |
28 | 25 | import java.net.Socket; |
29 | 26 | import java.util.Optional; |
30 | 27 |
|
31 | 28 | import lombok.NonNull; |
32 | 29 | import lombok.SneakyThrows; |
33 | | -import lombok.val; |
34 | 30 |
|
35 | 31 | /** |
36 | 32 | * |
@@ -61,70 +57,24 @@ public static boolean isPortAvailable (int port) { |
61 | 57 | } |
62 | 58 | } |
63 | 59 |
|
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 | | - |
80 | 60 | @SneakyThrows |
81 | 61 | 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()); |
103 | 63 | } |
104 | 64 |
|
105 | 65 | @SneakyThrows |
106 | 66 | 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); |
113 | 68 | } |
114 | 69 |
|
115 | 70 | @SneakyThrows |
116 | 71 | 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()); |
123 | 73 | } |
124 | 74 |
|
125 | 75 | @SneakyThrows |
126 | 76 | public static Bytes readBytes (@NonNull Socket socket, int fixedLength) { |
127 | | - return readBytes(socket.getInputStream(), fixedLength); |
| 77 | + return BytesUtils.readBytes(socket.getInputStream(), fixedLength); |
128 | 78 | } |
129 | 79 |
|
130 | 80 | private SocketUtils () { |
|
0 commit comments