dan-s1 commented on code in PR #7952:
URL: https://github.com/apache/nifi/pull/7952#discussion_r1414512781


##########
nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/AbstractCSVRecordReader.java:
##########
@@ -158,4 +180,46 @@ protected String trim(String value) {
     public RecordSchema getSchema() {
         return schema;
     }
+
+    /**
+     * This method searches using the specified Reader character-by-character 
until the
+     * record separator is found.
+     * @param reader the Reader providing the input
+     * @param recordSeparator the String specifying the end of a record in the 
input
+     * @throws IOException if an error occurs during reading, including not 
finding the record separator in the input
+     */
+    protected void readNextRecord(Reader reader, String recordSeparator) 
throws IOException {
+        int indexIntoSeparator = 0;
+        int recordSeparatorLength = recordSeparator.length();
+        int code = reader.read();
+        while (code != -1) {
+            char nextChar = (char)code;
+            if (recordSeparator.charAt(indexIntoSeparator) == nextChar) {
+                if (++indexIntoSeparator == recordSeparatorLength) {
+                    // We have matched the separator, return the string built 
so far
+                    return;
+                }
+            } else {
+                // The character didn't match the expected one in the record 
separator, reset the separator matcher
+                // and check if it is the first character of the separator.
+                indexIntoSeparator = 0;
+                if (recordSeparator.charAt(indexIntoSeparator) == nextChar) {
+                    // This character is the beginning of the record 
separator, keep it
+                    if (++indexIntoSeparator == recordSeparatorLength) {
+                        // We have matched the separator, return the string 
built so far
+                        return;
+                    }
+                }
+            }

Review Comment:
   Instead of all the comments regarding reaching the end of a record, make a 
method which conveys the intention of what you are trying to accomplish.
   
   
   ```
   char nextChar = (char)code;
   if (recordSeparator.charAt(indexIntoSeparator) == nextChar) {
       ++indexIntoSeparator;
       if (hasReachedEndOfRecord(indexIntoSeparator, recordSeparatorLength) ) {
           return;
       }
   } else {
       // The character didn't match the expected one in the record separator, 
reset the separator matcher
       // and check if it is the first character of the separator.
       indexIntoSeparator = 0;
       if (recordSeparator.charAt(indexIntoSeparator) == nextChar) {
           ++indexIntoSeparator;
           if (hasReachedEndOfRecord(indexIntoSeparator, recordSeparatorLength) 
) {
            return;
           }
       }
   }
   
   private boolean hasReachedEndOfRecord(int indexIntoSeparator, int 
recordSeparatorLength) {
           return indexIntoSeparator == recordSeparatorLength;
   }
   ```
   



-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to