|
16 | 16 |
|
17 | 17 | package io.appulse.utils; |
18 | 18 |
|
19 | | -import static java.lang.Math.min; |
20 | | - |
21 | | -import java.io.ByteArrayOutputStream; |
22 | | -import java.io.EOFException; |
23 | | -import java.io.InputStream; |
24 | 19 | import java.math.BigInteger; |
25 | 20 | import java.nio.ByteBuffer; |
26 | | -import java.nio.channels.ReadableByteChannel; |
27 | 21 | import java.util.stream.Stream; |
28 | 22 |
|
29 | 23 | import io.appulse.utils.exception.CantReadFromArrayException; |
30 | 24 | import io.appulse.utils.exception.CantWriteToArrayException; |
31 | 25 |
|
32 | 26 | import lombok.NonNull; |
33 | | -import lombok.SneakyThrows; |
34 | 27 | import lombok.val; |
35 | 28 |
|
36 | 29 | /** |
@@ -1019,134 +1012,11 @@ public static byte[] align (@NonNull byte[] bytes, int length) { |
1019 | 1012 | destPos = length - bytes.length; |
1020 | 1013 | } |
1021 | 1014 |
|
1022 | | - System.arraycopy(bytes, srcPos, result, destPos, min(bytes.length, length)); |
1023 | | - return result; |
1024 | | - } |
1025 | | - |
1026 | | - /** |
1027 | | - * Reads all bytes from stream till the end of the stream. |
1028 | | - * |
1029 | | - * @param stream steam |
1030 | | - * |
1031 | | - * @return readed bytes array |
1032 | | - * |
1033 | | - * @since 1.3.0 |
1034 | | - */ |
1035 | | - @SneakyThrows |
1036 | | - public static byte[] read (@NonNull InputStream stream) { |
1037 | | - val outputStream = new ByteArrayOutputStream(32); |
1038 | | - val buffer = new byte[32]; |
1039 | | - |
1040 | | - while (true) { |
1041 | | - val length = stream.read(buffer); |
1042 | | - if (length == -1) { |
1043 | | - break; |
1044 | | - } |
1045 | | - outputStream.write(buffer, 0, length); |
1046 | | - } |
1047 | | - |
1048 | | - return outputStream.toByteArray(); |
1049 | | - } |
1050 | | - |
1051 | | - /** |
1052 | | - * Reads fixed length bytes from the stream. |
1053 | | - * |
1054 | | - * @param stream steam |
1055 | | - * @param length how many bytes to read from stream |
1056 | | - * |
1057 | | - * @return readed bytes array |
1058 | | - * |
1059 | | - * @since 1.3.0 |
1060 | | - */ |
1061 | | - @SneakyThrows |
1062 | | - public static byte[] read (@NonNull InputStream stream, int length) { |
1063 | | - if (length < 0) { |
1064 | | - throw new IndexOutOfBoundsException(); |
1065 | | - } |
1066 | | - |
1067 | | - val result = new byte[length]; |
1068 | | - int readed = 0; |
1069 | | - |
1070 | | - while (readed < length) { |
1071 | | - val count = stream.read(result, readed, length - readed); |
1072 | | - if (count < -1) { |
1073 | | - throw new EOFException(); |
1074 | | - } |
1075 | | - readed += count; |
1076 | | - } |
1077 | | - |
| 1015 | + System.arraycopy(bytes, srcPos, result, destPos, Math.min(bytes.length, length)); |
1078 | 1016 | return result; |
1079 | 1017 | } |
1080 | 1018 |
|
1081 | | - /** |
1082 | | - * Reads all bytes from the channel till the buffer is full. |
1083 | | - * |
1084 | | - * @param channel the bytes source |
1085 | | - * |
1086 | | - * @param buffer the destination, whre readed bytes store |
1087 | | - * |
1088 | | - * @since 1.13.0 |
1089 | | - */ |
1090 | | - @SneakyThrows |
1091 | | - public static void read (@NonNull ReadableByteChannel channel, @NonNull ByteBuffer buffer) { |
1092 | | - int totalReaded = 0; |
1093 | | - do { |
1094 | | - val readed = channel.read(buffer); |
1095 | | - if (readed > 0) { |
1096 | | - totalReaded += readed; |
1097 | | - } else if (readed == -1) { |
1098 | | - return; |
1099 | | - } |
1100 | | - } while (totalReaded < buffer.capacity()); |
1101 | | - } |
1102 | | - |
1103 | | - /** |
1104 | | - * Reads the bytes from the channel till the buffer's limit. |
1105 | | - * |
1106 | | - * @param channel the bytes source |
1107 | | - * |
1108 | | - * @param buffer the destination, whre readed bytes store |
1109 | | - * |
1110 | | - * @param limit the limit amount of bytes for read |
1111 | | - * |
1112 | | - * @since 1.13.0 |
1113 | | - */ |
1114 | | - @SneakyThrows |
1115 | | - public static void read (@NonNull ReadableByteChannel channel, @NonNull ByteBuffer buffer, int limit) { |
1116 | | - val subBuffer = ByteBuffer.allocate(limit); |
1117 | | - read(channel, subBuffer); |
1118 | | - buffer.put(subBuffer); |
1119 | | - } |
1120 | | - |
1121 | | - /** |
1122 | | - * Reads all bytes from stream till the end of the stream. |
1123 | | - * |
1124 | | - * @param stream steam |
1125 | | - * |
1126 | | - * @return readed bytes array wrapped in {@link Bytes} instance |
1127 | | - * |
1128 | | - * @since 1.3.0 |
1129 | | - */ |
1130 | | - public static Bytes readBytes (@NonNull InputStream stream) { |
1131 | | - val result = read(stream); |
1132 | | - return Bytes.wrap(result); |
1133 | | - } |
1134 | | - |
1135 | | - /** |
1136 | | - * Reads fixed length bytes from the stream. |
1137 | | - * |
1138 | | - * @param stream steam |
1139 | | - * @param length how many bytes to read from stream |
1140 | | - * |
1141 | | - * @return readed bytes array wrapped in {@link Bytes} instance |
1142 | | - * |
1143 | | - * @since 1.3.0 |
1144 | | - */ |
1145 | | - public static Bytes readBytes (@NonNull InputStream stream, int length) { |
1146 | | - val result = read(stream, length); |
1147 | | - return Bytes.wrap(result); |
1148 | | - } |
1149 | | - |
1150 | 1019 | private BytesUtils () { |
| 1020 | + throw new UnsupportedOperationException(); |
1151 | 1021 | } |
1152 | 1022 | } |
0 commit comments