Skip to content

Commit 3fc877b

Browse files
committed
refine contentEquals(InputStream, InputStream)
1 parent af88ba9 commit 3fc877b

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -714,17 +714,40 @@ public static boolean contentEquals(final InputStream input1, final InputStream
714714
if (input1 == null ^ input2 == null) {
715715
return false;
716716
}
717-
final BufferedInputStream bufferedInput1 = buffer(input1);
718-
final BufferedInputStream bufferedInput2 = buffer(input2);
719-
int ch = bufferedInput1.read();
720-
while (EOF != ch) {
721-
final int ch2 = bufferedInput2.read();
722-
if (ch != ch2) {
723-
return false;
717+
718+
byte[] byteArray1 = new byte[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
719+
byte[] byteArray2 = new byte[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
720+
int nowPos1;
721+
int nowPos2;
722+
int nowRead1;
723+
int nowRead2;
724+
while (true) {
725+
nowPos1 = 0;
726+
nowPos2 = 0;
727+
for (int nowCheck = 0; nowCheck < CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE; nowCheck++) {
728+
if (nowPos1 == nowCheck) {
729+
do {
730+
nowRead1 = input1.read(byteArray1, nowPos1, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
731+
} while (nowRead1 == 0);
732+
if (nowRead1 == -1) {
733+
return nowPos2 == nowCheck && input2.read() == -1;
734+
}
735+
nowPos1 += nowRead1;
736+
}
737+
if (nowPos2 == nowCheck) {
738+
do {
739+
nowRead2 = input2.read(byteArray2, nowPos2, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
740+
} while (nowRead2 == 0);
741+
if (nowRead2 == -1) {
742+
return nowPos1 == nowCheck && input1.read() == -1;
743+
}
744+
nowPos2 += nowRead2;
745+
}
746+
if (byteArray1[nowCheck] != byteArray2[nowCheck]) {
747+
return false;
748+
}
724749
}
725-
ch = bufferedInput1.read();
726750
}
727-
return bufferedInput2.read() == EOF;
728751
}
729752

730753
private static final int CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE = 8192;

0 commit comments

Comments
 (0)