garydgregory commented on a change in pull request #118: URL: https://github.com/apache/commons-io/pull/118#discussion_r462220545
########## File path: src/main/java/org/apache/commons/io/IOUtils.java ########## @@ -790,16 +842,342 @@ public static boolean contentEqualsIgnoreEOL(final Reader input1, final Reader i if (input1 == null ^ input2 == null) { return false; } - final BufferedReader br1 = toBufferedReader(input1); - final BufferedReader br2 = toBufferedReader(input2); - String line1 = br1.readLine(); - String line2 = br2.readLine(); - while (line1 != null && line1.equals(line2)) { - line1 = br1.readLine(); - line2 = br2.readLine(); + char[] charArray1 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE]; + char[] charArray2 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE]; + int nowPos1 = 0; + int nowPos2 = 0; + int nowRead1; + int nowRead2; + int nowCheck1 = 0; + int nowCheck2 = 0; + boolean readEnd1 = false; + boolean readEnd2 = false; + LastState lastState1 = LastState.newLine; + LastState lastState2 = LastState.newLine; + while (true) { + if (nowPos1 == nowCheck1) { + if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos1 = nowCheck1 = 0; + } + do { + nowRead1 = input1.read(charArray1, nowPos1, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1); + } while (nowRead1 == 0); + if (nowRead1 == -1) { + readEnd1 = true; + } else { + nowPos1 += nowRead1; + } + } + if (nowPos2 == nowCheck2) { + if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos2 = nowCheck2 = 0; + } + do { + nowRead2 = input2.read(charArray2, nowPos2, CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2); + } while (nowRead2 == 0); + if (nowRead2 == -1) { + readEnd2 = true; + } else { + nowPos2 += nowRead2; + } + } + if (readEnd1) { + if (readEnd2) { + return true; + } else { + switch (lastState1) { + case r: + case newLine: + switch (lastState2) { + case r: + if (charArray2[nowCheck2] == '\n') { + nowCheck2++; + if (nowPos2 == nowCheck2) { + if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos2 = nowCheck2 = 0; + } + do { + nowRead2 = input2.read(charArray2, nowPos2, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2); + } while (nowRead2 == 0); + if (nowRead2 == -1) { + readEnd2 = true; + } else { + nowPos2 += nowRead2; + } + } + return readEnd2; + } + return false; + default: + return false; + } + case normal: + switch (lastState2) { + case normal: + switch (charArray2[nowCheck2]) { + case '\r': + nowCheck2++; + if (nowPos2 == nowCheck2) { + if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos2 = nowCheck2 = 0; + } + do { + nowRead2 = input2.read(charArray2, nowPos2, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2); + } while (nowRead2 == 0); + if (nowRead2 == -1) { + readEnd2 = true; + } else { + nowPos2 += nowRead2; + } + } + if (readEnd2) { + return true; + } else if (charArray2[nowCheck2] == '\n') { + nowCheck2++; + if (nowPos2 == nowCheck2) { + if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos2 = nowCheck2 = 0; + } + do { + nowRead2 = input2.read(charArray2, nowPos2, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2); + } while (nowRead2 == 0); + if (nowRead2 == -1) { + readEnd2 = true; + } else { + nowPos2 += nowRead2; + } + } + return readEnd2; + } + return false; + case '\n': + nowCheck2++; + if (nowPos2 == nowCheck2) { + if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos2 = nowCheck2 = 0; + } + do { + nowRead2 = input2.read(charArray2, nowPos2, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2); + } while (nowRead2 == 0); + if (nowRead2 == -1) { + readEnd2 = true; + } else { + nowPos2 += nowRead2; + } + } + return readEnd2; + default: + return false; + } + default: + return false; + } + default: + //shall never enter + } + } + } else if (readEnd2) { + switch (lastState2) { + case r: + case newLine: + switch (lastState1) { + case r: + if (charArray1[nowCheck1] == '\n') { + nowCheck1++; + if (nowPos1 == nowCheck1) { + if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos1 = nowCheck1 = 0; + } + do { + nowRead1 = input1.read(charArray1, nowPos1, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1); + } while (nowRead1 == 0); + if (nowRead1 == -1) { + readEnd1 = true; + } else { + nowPos1 += nowRead1; + } + } + return readEnd1; + } + return false; + default: + return false; + } + case normal: + switch (lastState1) { + case normal: + switch (charArray1[nowCheck1]) { + case '\r': + nowCheck1++; + if (nowPos1 == nowCheck1) { + if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos1 = nowCheck1 = 0; + } + do { + nowRead1 = input1.read(charArray1, nowPos1, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1); + } while (nowRead1 == 0); + if (nowRead1 == -1) { + readEnd1 = true; + } else { + nowPos1 += nowRead1; + } + } + if (readEnd1) { + return true; + } else if (charArray1[nowCheck1] == '\n') { + nowCheck1++; + if (nowPos1 == nowCheck1) { + if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos1 = nowCheck1 = 0; + } + do { + nowRead1 = input1.read(charArray1, nowPos1, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1); + } while (nowRead1 == 0); + if (nowRead1 == -1) { + readEnd1 = true; + } else { + nowPos1 += nowRead1; + } + } + return readEnd1; + } + return false; + case '\n': + nowCheck1++; + if (nowPos1 == nowCheck1) { + if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos1 = nowCheck1 = 0; + } + do { + nowRead1 = input1.read(charArray1, nowPos1, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1); + } while (nowRead1 == 0); + if (nowRead1 == -1) { + readEnd1 = true; + } else { + nowPos1 += nowRead1; + } + } + return readEnd1; + default: + return false; + } + default: + return false; + } + default: + //shall never enter + } + } + + switch (charArray1[nowCheck1]) { + case '\r': + switch (charArray2[nowCheck2]) { + case '\r': + lastState1 = lastState2 = LastState.r; + nowCheck1++; + nowCheck2++; + continue; + case '\n': + nowCheck1++; + if (nowPos1 == nowCheck1) { + if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos1 = nowCheck1 = 0; + } + do { + nowRead1 = input1.read(charArray1, nowPos1, +CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1); + } while (nowRead1 == 0); + if (nowRead1 == -1) { + readEnd1 = true; + } else { + nowPos1 += nowRead1; + } + } + lastState1 = lastState2 = LastState.newLine; + nowCheck2++; + if (readEnd1) { + continue; + } + if (charArray1[nowCheck1] == '\n') { + nowCheck1++; + } + continue; + default: + return false; + } + case '\n': + switch (charArray2[nowCheck2]) { + case '\n': + lastState1 = lastState2 = LastState.newLine; + nowCheck1++; + nowCheck2++; + continue; + case '\r': + nowCheck2++; + if (nowPos2 == nowCheck2) { + if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) { + nowPos2 = nowCheck2 = 0; + } + do { + nowRead2 = input2.read(charArray2, nowPos2, + CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2); + } while (nowRead2 == 0); + if (nowRead2 == -1) { + readEnd2 = true; + } else { + nowPos2 += nowRead2; + } + } + lastState1 = lastState2 = LastState.newLine; + nowCheck1++; + if (readEnd2) { + continue; + } + if (charArray2[nowCheck2] == '\n') { + nowCheck2++; + } + continue; + default: + if (lastState1 == LastState.r) { + lastState1 = LastState.newLine; + nowCheck1++; + continue; + } else { + return false; + } + } + default: + switch (charArray2[nowCheck2]) { + case '\n': + if (lastState2 == LastState.r) { + lastState2 = LastState.newLine; + nowCheck2++; + continue; + } else { + return false; + } + case '\r': + return false; + default: + if (charArray1[nowCheck1] != charArray2[nowCheck2]) { + return false; + } + lastState1 = lastState2 = LastState.normal; + nowCheck1++; + nowCheck2++; + continue; + } + } } Review comment: This is one giant complex method, wow. I can't see wanting to maintain it myself. I am not sure if exploding methods like this from simple, short, and easy to understand to complex, large, and hard to grok is what we want. To make this worse from a maintenance POV, there are zero comments in hundreds of lines of code; I can imagine someone seeing this as an opportunity to refactoring all the the repeated patterns into reusable methods. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org