|
16 | 16 | */
|
17 | 17 | package org.apache.commons.io;
|
18 | 18 |
|
| 19 | +import static org.apache.commons.io.IOUtils.DEFAULT_BUFFER_SIZE; |
| 20 | +import static org.apache.commons.io.IOUtils.EOF; |
| 21 | + |
19 | 22 | import java.io.BufferedInputStream;
|
20 | 23 | import java.io.BufferedOutputStream;
|
21 | 24 | import java.io.BufferedReader;
|
@@ -790,43 +793,60 @@ public static boolean contentEquals(final InputStream input1, final InputStream
|
790 | 793 | }
|
791 | 794 |
|
792 | 795 | /**
|
793 |
| - * Compares the contents of two Readers to determine if they are equal or |
794 |
| - * not. |
| 796 | + * Compares the contents of two Readers to determine if they are equal or not. |
795 | 797 | * <p>
|
796 |
| - * This method buffers the input internally using |
797 |
| - * {@code BufferedReader} if they are not already buffered. |
| 798 | + * This method buffers the input internally using {@code BufferedReader} if they are not already buffered. |
798 | 799 | * </p>
|
799 | 800 | *
|
800 |
| - * @param reader1 the first reader |
801 |
| - * @param reader2 the second reader |
802 |
| - * @return true if the content of the readers are equal or they both don't |
803 |
| - * exist, false otherwise |
| 801 | + * @param input1 the first reader |
| 802 | + * @param input2 the second reader |
| 803 | + * @return true if the content of the readers are equal or they both don't exist, false otherwise |
804 | 804 | * @throws NullPointerException if either input is null
|
805 |
| - * @throws IOException if an I/O error occurs |
| 805 | + * @throws IOException if an I/O error occurs |
806 | 806 | * @since 1.1
|
807 | 807 | */
|
808 | 808 | @SuppressWarnings("resource")
|
809 |
| - public static boolean contentEquals(final Reader reader1, final Reader reader2) |
810 |
| - throws IOException { |
811 |
| - if (reader1 == reader2) { |
| 809 | + public static boolean contentEquals(final Reader input1, final Reader input2) throws IOException { |
| 810 | + if (input1 == input2) { |
812 | 811 | return true;
|
813 | 812 | }
|
814 |
| - if (reader1 == null ^ reader2 == null) { |
| 813 | + if (input1 == null || input2 == null) { |
815 | 814 | return false;
|
816 | 815 | }
|
817 |
| - final BufferedReader bufferedInput1 = toBufferedReader(reader1); |
818 |
| - final BufferedReader bufferedInput2 = toBufferedReader(reader2); |
819 | 816 |
|
820 |
| - int ch = bufferedInput1.read(); |
821 |
| - while (EOF != ch) { |
822 |
| - final int ch2 = bufferedInput2.read(); |
823 |
| - if (ch != ch2) { |
824 |
| - return false; |
| 817 | + final char[] array1 = new char[DEFAULT_BUFFER_SIZE]; |
| 818 | + final char[] array2 = new char[DEFAULT_BUFFER_SIZE]; |
| 819 | + int pos1; |
| 820 | + int pos2; |
| 821 | + int count1; |
| 822 | + int count2; |
| 823 | + while (true) { |
| 824 | + pos1 = 0; |
| 825 | + pos2 = 0; |
| 826 | + for (int index = 0; index < DEFAULT_BUFFER_SIZE; index++) { |
| 827 | + if (pos1 == index) { |
| 828 | + do { |
| 829 | + count1 = input1.read(array1, pos1, DEFAULT_BUFFER_SIZE - pos1); |
| 830 | + } while (count1 == 0); |
| 831 | + if (count1 == EOF) { |
| 832 | + return pos2 == index && input2.read() == EOF; |
| 833 | + } |
| 834 | + pos1 += count1; |
| 835 | + } |
| 836 | + if (pos2 == index) { |
| 837 | + do { |
| 838 | + count2 = input2.read(array2, pos2, DEFAULT_BUFFER_SIZE - pos2); |
| 839 | + } while (count2 == 0); |
| 840 | + if (count2 == EOF) { |
| 841 | + return pos1 == index && input1.read() == EOF; |
| 842 | + } |
| 843 | + pos2 += count2; |
| 844 | + } |
| 845 | + if (array1[index] != array2[index]) { |
| 846 | + return false; |
| 847 | + } |
825 | 848 | }
|
826 |
| - ch = bufferedInput1.read(); |
827 | 849 | }
|
828 |
| - |
829 |
| - return bufferedInput2.read() == EOF; |
830 | 850 | }
|
831 | 851 |
|
832 | 852 | /**
|
|
0 commit comments