Github user markap14 commented on a diff in the pull request: https://github.com/apache/nifi/pull/280#discussion_r56329194 --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitText.java --- @@ -143,72 +199,82 @@ protected void init(final ProcessorInitializationContext context) { return properties; } - private int readLines(final InputStream in, final int maxNumLines, final OutputStream out, final boolean keepAllNewLines) throws IOException { + private int readLines(final InputStream in, final int maxNumLines, final long maxByteCount, final OutputStream out) throws IOException { int numLines = 0; + long totalBytes = 0L; for (int i = 0; i < maxNumLines; i++) { - final long bytes = countBytesToSplitPoint(in, out, keepAllNewLines || (i != maxNumLines - 1)); + final long bytes = countBytesToSplitPoint(in, out, totalBytes, maxByteCount); + totalBytes += bytes; if (bytes <= 0) { return numLines; } - numLines++; + if (totalBytes >= maxByteCount && numLines > maxNumLines) { + break; + } } - return numLines; } - private long countBytesToSplitPoint(final InputStream in, final OutputStream out, final boolean includeLineDelimiter) throws IOException { - int lastByte = -1; + private long countBytesToSplitPoint(final InputStream in, final OutputStream out, final long bytesReadSoFar, final long maxSize) throws IOException { long bytesRead = 0L; + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + in.mark(Integer.MAX_VALUE); while (true) { - in.mark(1); final int nextByte = in.read(); - // if we hit end of stream or new line we're done + // if we hit end of stream we're done if (nextByte == -1) { - if (lastByte == '\r') { - return includeLineDelimiter ? bytesRead : bytesRead - 1; - } else { - return bytesRead; + if (out != null) { + buffer.writeTo(out); } + buffer.close(); + return bytesRead; } - // if there's an OutputStream to copy the data to, copy it, if appropriate. - // "if appropriate" means that it's not a line delimiter or that we want to copy line delimiters + // buffer the output bytesRead++; - if (out != null && (includeLineDelimiter || (nextByte != '\n' && nextByte != '\r'))) { - out.write(nextByte); + buffer.write(nextByte); + + // check the size limit + if (bytesRead > (maxSize-bytesReadSoFar) && bytesReadSoFar > 0) { + in.reset(); + buffer.close(); + return -1; } // if we have a new line, then we're done if (nextByte == '\n') { - if (includeLineDelimiter) { - return bytesRead; - } else { - return (lastByte == '\r') ? bytesRead - 2 : bytesRead - 1; + if (out != null) { + buffer.writeTo(out); } + buffer.close(); + return bytesRead; } - // we didn't get a new line but if last byte was carriage return we've reached a new-line. - // so we roll back the last byte that we read and return - if (lastByte == '\r') { - in.reset(); - bytesRead--; // we reset the stream by 1 byte so decrement the number of bytes read by 1 - return includeLineDelimiter ? bytesRead : bytesRead - 1; + // Determine if \n follows \r; for both cases, end of line has been reached + if (nextByte == '\r') { + buffer.writeTo(out); + buffer.close(); + in.mark(1); + final int lookAheadByte = in.read(); + if (lookAheadByte == '\n') { + out.write(lookAheadByte); --- End diff -- This will throw a NullPointerException if out == null
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---