Avoid overreading of InputStream in copyRange
Closes gh-32695
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -177,18 +177,13 @@ public abstract class StreamUtils {
|
|||||||
long bytesToCopy = end - start + 1;
|
long bytesToCopy = end - start + 1;
|
||||||
byte[] buffer = new byte[(int) Math.min(StreamUtils.BUFFER_SIZE, bytesToCopy)];
|
byte[] buffer = new byte[(int) Math.min(StreamUtils.BUFFER_SIZE, bytesToCopy)];
|
||||||
while (bytesToCopy > 0) {
|
while (bytesToCopy > 0) {
|
||||||
int bytesRead = in.read(buffer);
|
int bytesRead = (bytesToCopy < buffer.length ? in.read(buffer, 0, (int) bytesToCopy) :
|
||||||
|
in.read(buffer));
|
||||||
if (bytesRead == -1) {
|
if (bytesRead == -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (bytesRead <= bytesToCopy) {
|
out.write(buffer, 0, bytesRead);
|
||||||
out.write(buffer, 0, bytesRead);
|
bytesToCopy -= bytesRead;
|
||||||
bytesToCopy -= bytesRead;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
out.write(buffer, 0, (int) bytesToCopy);
|
|
||||||
bytesToCopy = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (end - start + 1 - bytesToCopy);
|
return (end - start + 1 - bytesToCopy);
|
||||||
}
|
}
|
||||||
@@ -202,7 +197,9 @@ public abstract class StreamUtils {
|
|||||||
* @since 4.3
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
public static int drain(@Nullable InputStream in) throws IOException {
|
public static int drain(@Nullable InputStream in) throws IOException {
|
||||||
Assert.notNull(in, "No InputStream specified");
|
if (in == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return (int) in.transferTo(OutputStream.nullOutputStream());
|
return (int) in.transferTo(OutputStream.nullOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.never;
|
|||||||
* Tests for {@link StreamUtils}.
|
* Tests for {@link StreamUtils}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Juergen Hoeller
|
||||||
*/
|
*/
|
||||||
class StreamUtilsTests {
|
class StreamUtilsTests {
|
||||||
|
|
||||||
@@ -45,6 +46,7 @@ class StreamUtilsTests {
|
|||||||
|
|
||||||
private String string = "";
|
private String string = "";
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
new Random().nextBytes(bytes);
|
new Random().nextBytes(bytes);
|
||||||
@@ -53,6 +55,7 @@ class StreamUtilsTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void copyToByteArray() throws Exception {
|
void copyToByteArray() throws Exception {
|
||||||
InputStream inputStream = new ByteArrayInputStream(bytes);
|
InputStream inputStream = new ByteArrayInputStream(bytes);
|
||||||
@@ -91,11 +94,30 @@ class StreamUtilsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void copyRange() throws Exception {
|
void copyRangeWithinBuffer() throws Exception {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
StreamUtils.copyRange(new ByteArrayInputStream(bytes), out, 0, 100);
|
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||||
byte[] range = Arrays.copyOfRange(bytes, 0, 101);
|
StreamUtils.copyRange(in, out, 0, 100);
|
||||||
assertThat(out.toByteArray()).isEqualTo(range);
|
assertThat(in.available()).isEqualTo(bytes.length - 101);
|
||||||
|
assertThat(out.toByteArray()).isEqualTo(Arrays.copyOfRange(bytes, 0, 101));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void copyRangeBeyondBuffer() throws Exception {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||||
|
StreamUtils.copyRange(in, out, 0, 8200);
|
||||||
|
assertThat(in.available()).isEqualTo(1);
|
||||||
|
assertThat(out.toByteArray()).isEqualTo(Arrays.copyOfRange(bytes, 0, 8201));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void copyRangeBeyondAvailable() throws Exception {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||||
|
StreamUtils.copyRange(in, out, 0, 8300);
|
||||||
|
assertThat(in.available()).isEqualTo(0);
|
||||||
|
assertThat(out.toByteArray()).isEqualTo(Arrays.copyOfRange(bytes, 0, 8202));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -127,4 +149,5 @@ class StreamUtilsTests {
|
|||||||
ordered.verify(source).write(bytes, 1, 2);
|
ordered.verify(source).write(bytes, 1, 2);
|
||||||
ordered.verify(source, never()).close();
|
ordered.verify(source, never()).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user