Read lines from file, calling a consumer function for each line I wrote a function for iterating over a large file with performance in mind. It takes in an InputStream and reads until it reaches the end of the file. Whenever it detects an end of line it will call the UnsafeConsumer#accept with the line.


UnsafeConsumer is the same as Consumer&lt;T&gt; but has a throws declaration on accept function
stream has to be closed outside the function


I am am not sure if this is the fastest way to do this. Is there anything different that I could do?

public static void fileLines(InputStream stream, UnsafeConsumer&lt;String,IOException&gt; cons) throws IOException{
    StringBuilder lineBuild=new StringBuilder();
    int character;

    while(true){ //read file
        while(true){ //read line
            character=stream.read();

            if(character==-1){
                cons.accept(lineBuild.toString());
                lineBuild.setLength(0);
                return;
            }
            if(character=='\n'){
                cons.accept(lineBuild.toString());
                lineBuild.setLength(0);

                character=stream.read();

                if(character==-1) return;
                else if(character!='\r'&amp;&amp;character!='\n') lineBuild.append((char)character);
                break;
            }
            lineBuild.append((char)character);
        }
    }
}
You are conflating bytes with characters.  An InputStream produces bytes, but you implicitly interpret each byte as a character when you do character=stream.read() followed by the cast lineBuild.append((char)character).  To be clear:


Strings consist of chars, not bytes.
InputStreams and OutputStreams deal with bytes.
Readers and Writers deal with characters.


To read lines as strings, first convert stream to a Reader using InputStreamReader.

BufferedReader br = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"));


Then, BufferedReader offers a .readLine() method.  Or, since you seem to be interested in a stream-based approach, call .lines(), which produces a Stream&lt;String&gt;.