Skip to content

Commit a87936a

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Add (Read/Write)BytesUtils
1 parent 114f9b1 commit a87936a

File tree

12 files changed

+812
-146
lines changed

12 files changed

+812
-146
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Add more tests.
1313
- Add `JavaDoc`.
1414

15+
## [1.14.0](https://github.com/appulse-projects/utils-java/releases/tag/1.14.0) - 2019-04-11
16+
17+
### Added
18+
19+
- `WriteBytesUtils` and `ReadBytesUtils` with write/read helpers.
20+
21+
### Removed
22+
23+
- All `write`/`read` functions from `BytesUtils`.
24+
1525
## [1.13.1](https://github.com/appulse-projects/utils-java/releases/tag/1.13.1) - 2019-04-08
1626

1727
### Added

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ static Bytes resizableArray (int initialSize) {
121121
}
122122

123123
/**
124-
* Tells if the buffer is resizable or not.
124+
* Tells if the buffer is auto resizable or not.
125125
*
126126
* @return {@code true} if the buffer could be extandable
127127
* {@code false} otherwise
128128
*/
129-
boolean isResizable ();
129+
boolean isAutoResizable ();
130130

131131
/**
132132
* Sets the specified byte array at the current {@code writerIndex}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private BytesByteBuf (@NonNull ByteBuf buffer) {
7777
}
7878

7979
@Override
80-
public boolean isResizable () {
80+
public boolean isAutoResizable () {
8181
return false;
8282
}
8383

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static BytesByteBuffer copy (@NonNull ByteBuffer buffer) {
4747
}
4848

4949
@Override
50-
public boolean isResizable () {
50+
public boolean isAutoResizable () {
5151
return false;
5252
}
5353

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BytesExtendableArray extends BytesFixedArray {
3939
}
4040

4141
@Override
42-
public boolean isResizable () {
42+
public boolean isAutoResizable () {
4343
return true;
4444
}
4545

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static BytesFixedArray copy (@NonNull byte[] bytes) {
5353
}
5454

5555
@Override
56-
public boolean isResizable () {
56+
public boolean isAutoResizable () {
5757
return false;
5858
}
5959

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

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

1717
package io.appulse.utils;
1818

19-
import static java.lang.Math.min;
20-
21-
import java.io.ByteArrayOutputStream;
22-
import java.io.EOFException;
23-
import java.io.InputStream;
2419
import java.math.BigInteger;
2520
import java.nio.ByteBuffer;
26-
import java.nio.channels.ReadableByteChannel;
2721
import java.util.stream.Stream;
2822

2923
import io.appulse.utils.exception.CantReadFromArrayException;
3024
import io.appulse.utils.exception.CantWriteToArrayException;
3125

3226
import lombok.NonNull;
33-
import lombok.SneakyThrows;
3427
import lombok.val;
3528

3629
/**
@@ -1019,134 +1012,11 @@ public static byte[] align (@NonNull byte[] bytes, int length) {
10191012
destPos = length - bytes.length;
10201013
}
10211014

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));
10781016
return result;
10791017
}
10801018

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-
11501019
private BytesUtils () {
1020+
throw new UnsupportedOperationException();
11511021
}
11521022
}

0 commit comments

Comments
 (0)