Skip to content

Commit 125d490

Browse files
committed
remake contentEqualsIgnoreEOL and add related tests
1 parent 3da8a4f commit 125d490

File tree

2 files changed

+366
-30
lines changed

2 files changed

+366
-30
lines changed

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

Lines changed: 340 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,12 @@ public static boolean contentEquals(final Reader input1, final Reader input2)
813813
}
814814
}
815815

816+
private enum LastState {
817+
r,
818+
normal,
819+
newLine;
820+
}
821+
816822
/**
817823
* Compares the contents of two Readers to determine if they are equal or
818824
* not, ignoring EOL characters.
@@ -836,16 +842,342 @@ public static boolean contentEqualsIgnoreEOL(final Reader input1, final Reader i
836842
if (input1 == null ^ input2 == null) {
837843
return false;
838844
}
839-
final BufferedReader br1 = toBufferedReader(input1);
840-
final BufferedReader br2 = toBufferedReader(input2);
841845

842-
String line1 = br1.readLine();
843-
String line2 = br2.readLine();
844-
while (line1 != null && line1.equals(line2)) {
845-
line1 = br1.readLine();
846-
line2 = br2.readLine();
846+
char[] charArray1 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
847+
char[] charArray2 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
848+
int nowPos1 = 0;
849+
int nowPos2 = 0;
850+
int nowRead1;
851+
int nowRead2;
852+
int nowCheck1 = 0;
853+
int nowCheck2 = 0;
854+
boolean readEnd1 = false;
855+
boolean readEnd2 = false;
856+
LastState lastState1 = LastState.newLine;
857+
LastState lastState2 = LastState.newLine;
858+
while (true) {
859+
if (nowPos1 == nowCheck1) {
860+
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
861+
nowPos1 = nowCheck1 = 0;
862+
}
863+
do {
864+
nowRead1 = input1.read(charArray1, nowPos1, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
865+
} while (nowRead1 == 0);
866+
if (nowRead1 == -1) {
867+
readEnd1 = true;
868+
} else {
869+
nowPos1 += nowRead1;
870+
}
871+
}
872+
if (nowPos2 == nowCheck2) {
873+
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
874+
nowPos2 = nowCheck2 = 0;
875+
}
876+
do {
877+
nowRead2 = input2.read(charArray2, nowPos2, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
878+
} while (nowRead2 == 0);
879+
if (nowRead2 == -1) {
880+
readEnd2 = true;
881+
} else {
882+
nowPos2 += nowRead2;
883+
}
884+
}
885+
if (readEnd1) {
886+
if (readEnd2) {
887+
return true;
888+
} else {
889+
switch (lastState1) {
890+
case r:
891+
case newLine:
892+
switch (lastState2) {
893+
case r:
894+
if (charArray2[nowCheck2] == '\n') {
895+
nowCheck2++;
896+
if (nowPos2 == nowCheck2) {
897+
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
898+
nowPos2 = nowCheck2 = 0;
899+
}
900+
do {
901+
nowRead2 = input2.read(charArray2, nowPos2,
902+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
903+
} while (nowRead2 == 0);
904+
if (nowRead2 == -1) {
905+
readEnd2 = true;
906+
} else {
907+
nowPos2 += nowRead2;
908+
}
909+
}
910+
return readEnd2;
911+
}
912+
return false;
913+
default:
914+
return false;
915+
}
916+
case normal:
917+
switch (lastState2) {
918+
case normal:
919+
switch (charArray2[nowCheck2]) {
920+
case '\r':
921+
nowCheck2++;
922+
if (nowPos2 == nowCheck2) {
923+
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
924+
nowPos2 = nowCheck2 = 0;
925+
}
926+
do {
927+
nowRead2 = input2.read(charArray2, nowPos2,
928+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
929+
} while (nowRead2 == 0);
930+
if (nowRead2 == -1) {
931+
readEnd2 = true;
932+
} else {
933+
nowPos2 += nowRead2;
934+
}
935+
}
936+
if (readEnd2) {
937+
return true;
938+
} else if (charArray2[nowCheck2] == '\n') {
939+
nowCheck2++;
940+
if (nowPos2 == nowCheck2) {
941+
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
942+
nowPos2 = nowCheck2 = 0;
943+
}
944+
do {
945+
nowRead2 = input2.read(charArray2, nowPos2,
946+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
947+
} while (nowRead2 == 0);
948+
if (nowRead2 == -1) {
949+
readEnd2 = true;
950+
} else {
951+
nowPos2 += nowRead2;
952+
}
953+
}
954+
return readEnd2;
955+
}
956+
return false;
957+
case '\n':
958+
nowCheck2++;
959+
if (nowPos2 == nowCheck2) {
960+
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
961+
nowPos2 = nowCheck2 = 0;
962+
}
963+
do {
964+
nowRead2 = input2.read(charArray2, nowPos2,
965+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
966+
} while (nowRead2 == 0);
967+
if (nowRead2 == -1) {
968+
readEnd2 = true;
969+
} else {
970+
nowPos2 += nowRead2;
971+
}
972+
}
973+
return readEnd2;
974+
default:
975+
return false;
976+
}
977+
default:
978+
return false;
979+
}
980+
default:
981+
//shall never enter
982+
}
983+
}
984+
} else if (readEnd2) {
985+
switch (lastState2) {
986+
case r:
987+
case newLine:
988+
switch (lastState1) {
989+
case r:
990+
if (charArray1[nowCheck1] == '\n') {
991+
nowCheck1++;
992+
if (nowPos1 == nowCheck1) {
993+
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
994+
nowPos1 = nowCheck1 = 0;
995+
}
996+
do {
997+
nowRead1 = input1.read(charArray1, nowPos1,
998+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
999+
} while (nowRead1 == 0);
1000+
if (nowRead1 == -1) {
1001+
readEnd1 = true;
1002+
} else {
1003+
nowPos1 += nowRead1;
1004+
}
1005+
}
1006+
return readEnd1;
1007+
}
1008+
return false;
1009+
default:
1010+
return false;
1011+
}
1012+
case normal:
1013+
switch (lastState1) {
1014+
case normal:
1015+
switch (charArray1[nowCheck1]) {
1016+
case '\r':
1017+
nowCheck1++;
1018+
if (nowPos1 == nowCheck1) {
1019+
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
1020+
nowPos1 = nowCheck1 = 0;
1021+
}
1022+
do {
1023+
nowRead1 = input1.read(charArray1, nowPos1,
1024+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
1025+
} while (nowRead1 == 0);
1026+
if (nowRead1 == -1) {
1027+
readEnd1 = true;
1028+
} else {
1029+
nowPos1 += nowRead1;
1030+
}
1031+
}
1032+
if (readEnd1) {
1033+
return true;
1034+
} else if (charArray1[nowCheck1] == '\n') {
1035+
nowCheck1++;
1036+
if (nowPos1 == nowCheck1) {
1037+
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
1038+
nowPos1 = nowCheck1 = 0;
1039+
}
1040+
do {
1041+
nowRead1 = input1.read(charArray1, nowPos1,
1042+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
1043+
} while (nowRead1 == 0);
1044+
if (nowRead1 == -1) {
1045+
readEnd1 = true;
1046+
} else {
1047+
nowPos1 += nowRead1;
1048+
}
1049+
}
1050+
return readEnd1;
1051+
}
1052+
return false;
1053+
case '\n':
1054+
nowCheck1++;
1055+
if (nowPos1 == nowCheck1) {
1056+
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
1057+
nowPos1 = nowCheck1 = 0;
1058+
}
1059+
do {
1060+
nowRead1 = input1.read(charArray1, nowPos1,
1061+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
1062+
} while (nowRead1 == 0);
1063+
if (nowRead1 == -1) {
1064+
readEnd1 = true;
1065+
} else {
1066+
nowPos1 += nowRead1;
1067+
}
1068+
}
1069+
return readEnd1;
1070+
default:
1071+
return false;
1072+
}
1073+
default:
1074+
return false;
1075+
}
1076+
default:
1077+
//shall never enter
1078+
}
1079+
}
1080+
1081+
switch (charArray1[nowCheck1]) {
1082+
case '\r':
1083+
switch (charArray2[nowCheck2]) {
1084+
case '\r':
1085+
lastState1 = lastState2 = LastState.r;
1086+
nowCheck1++;
1087+
nowCheck2++;
1088+
continue;
1089+
case '\n':
1090+
nowCheck1++;
1091+
if (nowPos1 == nowCheck1) {
1092+
if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
1093+
nowPos1 = nowCheck1 = 0;
1094+
}
1095+
do {
1096+
nowRead1 = input1.read(charArray1, nowPos1,
1097+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
1098+
} while (nowRead1 == 0);
1099+
if (nowRead1 == -1) {
1100+
readEnd1 = true;
1101+
} else {
1102+
nowPos1 += nowRead1;
1103+
}
1104+
}
1105+
lastState1 = lastState2 = LastState.newLine;
1106+
nowCheck2++;
1107+
if (readEnd1) {
1108+
continue;
1109+
}
1110+
if (charArray1[nowCheck1] == '\n') {
1111+
nowCheck1++;
1112+
}
1113+
continue;
1114+
default:
1115+
return false;
1116+
}
1117+
case '\n':
1118+
switch (charArray2[nowCheck2]) {
1119+
case '\n':
1120+
lastState1 = lastState2 = LastState.newLine;
1121+
nowCheck1++;
1122+
nowCheck2++;
1123+
continue;
1124+
case '\r':
1125+
nowCheck2++;
1126+
if (nowPos2 == nowCheck2) {
1127+
if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
1128+
nowPos2 = nowCheck2 = 0;
1129+
}
1130+
do {
1131+
nowRead2 = input2.read(charArray2, nowPos2,
1132+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
1133+
} while (nowRead2 == 0);
1134+
if (nowRead2 == -1) {
1135+
readEnd2 = true;
1136+
} else {
1137+
nowPos2 += nowRead2;
1138+
}
1139+
}
1140+
lastState1 = lastState2 = LastState.newLine;
1141+
nowCheck1++;
1142+
if (readEnd2) {
1143+
continue;
1144+
}
1145+
if (charArray2[nowCheck2] == '\n') {
1146+
nowCheck2++;
1147+
}
1148+
continue;
1149+
default:
1150+
if (lastState1 == LastState.r) {
1151+
lastState1 = LastState.newLine;
1152+
nowCheck1++;
1153+
continue;
1154+
} else {
1155+
return false;
1156+
}
1157+
}
1158+
default:
1159+
switch (charArray2[nowCheck2]) {
1160+
case '\n':
1161+
if (lastState2 == LastState.r) {
1162+
lastState2 = LastState.newLine;
1163+
nowCheck2++;
1164+
continue;
1165+
} else {
1166+
return false;
1167+
}
1168+
case '\r':
1169+
return false;
1170+
default:
1171+
if (charArray1[nowCheck1] != charArray2[nowCheck2]) {
1172+
return false;
1173+
}
1174+
lastState1 = lastState2 = LastState.normal;
1175+
nowCheck1++;
1176+
nowCheck2++;
1177+
continue;
1178+
}
1179+
}
8471180
}
848-
return Objects.equals(line1, line2);
8491181
}
8501182

8511183
/**

0 commit comments

Comments
 (0)