Skip to content

Commit 143960a

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Add tests
1 parent a87936a commit 143960a

File tree

5 files changed

+828
-17
lines changed

5 files changed

+828
-17
lines changed

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.13.1</version>
27+
<version>1.14.0</version>
2828
<packaging>jar</packaging>
2929

3030
<properties>
@@ -66,7 +66,7 @@ limitations under the License.
6666
<url>https://github.com/appulse-projects/utils-java</url>
6767
<connection>scm:git:https://github.com/appulse-projects/utils-java.git</connection>
6868
<developerConnection>scm:git:https://github.com/appulse-projects/utils-java.git</developerConnection>
69-
<tag>1.13.1</tag>
69+
<tag>1.14.0</tag>
7070
</scm>
7171

7272
<distributionManagement>

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,28 @@ public static int read (@NonNull InputStream inputStream, @NonNull ByteBuffer bu
200200
@SneakyThrows
201201
public static int read (@NonNull InputStream inputStream, @NonNull byte[] bytes, int offset, int length) {
202202
if (offset < 0 || offset >= bytes.length) {
203-
val msg = String.format(ENGLISH, "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length", offset);
203+
val msg = String.format(ENGLISH,
204+
"Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length",
205+
offset
206+
);
204207
throw new IndexOutOfBoundsException(msg);
205208
}
206209
if (length < 0) {
207210
val msg = String.format(ENGLISH, "Invalid length %d. The length must be greater or equal 0", length);
208211
throw new IndexOutOfBoundsException(msg);
209212
}
210213

214+
int position = offset;
211215
int remaining = Math.min(bytes.length - offset, length);
212216
while (remaining > 0) {
213-
val readed = inputStream.read(bytes, offset + length - remaining, remaining);
217+
val readed = inputStream.read(bytes, position, remaining);
214218
if (readed < 0) {
215219
break;
216220
}
221+
position += readed;
217222
remaining -= readed;
218223
}
219-
return offset + length - remaining;
224+
return position - offset;
220225
}
221226

222227
/**
@@ -263,7 +268,10 @@ public static int read (ReadableByteChannel channel, byte[] buffer, int length)
263268
@SneakyThrows
264269
public static int read (@NonNull ReadableByteChannel channel, @NonNull byte[] bytes, int offset, int length) {
265270
if (offset < 0 || offset >= bytes.length) {
266-
val msg = String.format(ENGLISH, "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length", offset);
271+
val msg = String.format(ENGLISH,
272+
"Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length",
273+
offset
274+
);
267275
throw new IndexOutOfBoundsException(msg);
268276
}
269277
if (length < 0) {
@@ -277,7 +285,7 @@ public static int read (@NonNull ReadableByteChannel channel, @NonNull byte[] by
277285
byteBuffer.limit(limit);
278286

279287
int totalReaded = 0;
280-
while (true) {
288+
while (byteBuffer.hasRemaining()) {
281289
val readed = channel.read(byteBuffer);
282290
if (readed < 0) {
283291
break;

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public static int write (OutputStream outputStream, byte[] buffer, int length) {
5050
@SneakyThrows
5151
public static int write (@NonNull OutputStream outputStream, @NonNull byte[] bytes, int offset, int length) {
5252
if (offset < 0 || offset >= bytes.length) {
53-
val msg = String.format(ENGLISH, "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length", offset);
53+
val msg = String.format(ENGLISH,
54+
"Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length (%d)",
55+
offset, bytes.length
56+
);
5457
throw new IndexOutOfBoundsException(msg);
5558
}
5659
if (length < 0) {
@@ -81,13 +84,13 @@ public static int write (@NonNull OutputStream outputStream, @NonNull ByteBuffer
8184
}
8285

8386
public static int write (OutputStream outputStream, @NonNull Bytes buffer) {
84-
return write(outputStream, buffer, buffer.writableBytes());
87+
return write(outputStream, buffer, buffer.readableBytes());
8588
}
8689

8790
public static int write (@NonNull OutputStream outputStream, @NonNull Bytes buffer, int length) {
8891
val bytes = buffer.array();
89-
val written = write(outputStream, bytes, buffer.writerIndex(), length);
90-
buffer.writerIndex(buffer.writerIndex() + written);
92+
val written = write(outputStream, bytes, buffer.readerIndex(), length);
93+
buffer.readerIndex(buffer.readerIndex() + written);
9194
return written;
9295
}
9396

@@ -102,7 +105,10 @@ public static int write (WritableByteChannel channel, byte[] buffer, int length)
102105
@SneakyThrows
103106
public static int write (@NonNull WritableByteChannel channel, @NonNull byte[] bytes, int offset, int length) {
104107
if (offset < 0 || offset >= bytes.length) {
105-
val msg = String.format(ENGLISH, "Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length", offset);
108+
val msg = String.format(ENGLISH,
109+
"Invalid offset %d. The offset must be equal or greater than 0 and less than byte array length (%d)",
110+
offset, bytes.length
111+
);
106112
throw new IndexOutOfBoundsException(msg);
107113
}
108114
if (length < 0) {
@@ -121,6 +127,7 @@ public static int write (@NonNull WritableByteChannel channel, @NonNull byte[] b
121127
if (written < 0) {
122128
break;
123129
}
130+
totalWritten += written;
124131
}
125132
return totalWritten;
126133
}
@@ -137,13 +144,13 @@ public static int write (@NonNull WritableByteChannel channel, @NonNull ByteBuff
137144
}
138145

139146
public static int write (WritableByteChannel channel, @NonNull Bytes buffer) {
140-
return write(channel, buffer, buffer.writableBytes());
147+
return write(channel, buffer, buffer.readableBytes());
141148
}
142149

143150
public static int write (@NonNull WritableByteChannel channel, @NonNull Bytes buffer, int length) {
144151
val bytes = buffer.array();
145-
val written = write(channel, bytes, buffer.writerIndex(), length);
146-
buffer.writerIndex(buffer.writerIndex() + written);
152+
val written = write(channel, bytes, buffer.readerIndex(), length);
153+
buffer.readerIndex(buffer.readerIndex() + written);
147154
return written;
148155
}
149156

@@ -168,7 +175,7 @@ public static int write (@NonNull File file, ByteBuffer buffer, int length) {
168175
}
169176

170177
public static int write (File file, @NonNull Bytes buffer) {
171-
return write(file, buffer, buffer.writableBytes());
178+
return write(file, buffer, buffer.readableBytes());
172179
}
173180

174181
public static int write (@NonNull File file, Bytes buffer, int length) {
@@ -202,7 +209,7 @@ public static int write (@NonNull Path path, ByteBuffer buffer, int length) {
202209
}
203210

204211
public static int write (Path path, @NonNull Bytes buffer) {
205-
return write(path, buffer, buffer.writableBytes());
212+
return write(path, buffer, buffer.readableBytes());
206213
}
207214

208215
@SneakyThrows

0 commit comments

Comments
 (0)