1

I have process and got an InputStream like:

myInputStream = getProcess().getInputStream();

I would like to somehow like to beable to call these methods

String[] getLines(int start, int stop); // Will return a String array of those lines    
String[] getLines(int start); // Will return a String array of lines from the line number to the end    
String[] getLines(); //Returns the entire output as an array seperated by the end of the line.   

Also I will have several processes running at once so storing the entire logs in memory the entire time wouldn't be the best I think.

4
  • Most InputStreams aren't suitable for multithreaded use, and you cannot find an arbitrary line in the file without finding all lines before it. It looks like you'd be best off reading all lines into an array first thing. Dec 26, 2014 at 10:32
  • But if my log gets really big (say the process has been running for over a day - this could get up to 1GB arrays?) Dec 26, 2014 at 10:34
  • Benchmark it and find out how long it takes. new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)) is the typical way to get a BufferedReader from an InputStream, and BufferedReader supports the readLine() operation. But for your basic line-separated text file, I don't think you're going to be able to do much better. Dec 26, 2014 at 10:41
  • Yeah it can do a 1GB file in around 3 seconds which is good. I don't know which answer to choose though? Dec 26, 2014 at 12:46

2 Answers 2

0

The Scanner class may be of help here since it can read lines from an InputStream. Note that I provided a simple test case to verify the contents.

I agree with @LouisWasserman's comment that InputStreams aren't really suitable for this but the following may work.

public class LogReaderTest {
    public static class LogReader {
        private final Scanner scanner;

        public LogReader(final InputStream stream) {
            scanner = new Scanner(stream);
        }

        public String[] getLines(int start, int stop) {
            fastForward(start);
            int stopVal = stop <= start ? Integer.MAX_VALUE : stop - start;

            final List<String> rows = new ArrayList<>();
            for (int i = 0; i < stopVal; i++) {
                if (scanner.hasNextLine()) {
                    rows.add(scanner.nextLine());
                } else {
                    break;
                }
            }

            return rows.toArray(new String[rows.size()]);
        }

        public String[] getLines(int start) {
            return getLines(start, -1);
        }

        public String[] getLines() {
            return getLines(0);
        }

        private void fastForward(final int lines) {
            for (int i = 0; i < lines; i++) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                } else {
                    throw new IllegalStateException("Unable to scan line");
                }
            }
        }
    }

    @Test
    public void testLogReader() throws IOException {
        final String[] lines = {"first", "second", "third", "fourth"};
        final String stringWithNewLines = Arrays.stream(lines).collect(Collectors.joining("\n"));

        Assert.assertEquals(
            4, new LogReader(
                new ByteArrayInputStream(stringWithNewLines.getBytes())).getLines().length);
        Assert.assertEquals(
            2, new LogReader(
                new ByteArrayInputStream(stringWithNewLines.getBytes())).getLines(2).length);
        Assert.assertEquals(
            3, new LogReader(
                new ByteArrayInputStream(stringWithNewLines.getBytes())).getLines(1, 4).length);
    }
}
1
  • If i weren't to use an InputStream what else would I use/ would be better suited? Dec 26, 2014 at 11:24
0

Use BufferedReader to read InputStream line by line. try this code or you can implement your own logic.

String[] getLines(){
    return getLines(0,0);
}
String[] getLines(int start){
    return getLines(start,0);
}
String[] getLines(int start,int stop){
    ArrayList<String> list = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new 
        InputStreamReader(myInputStream));
    String line;
    Integer count=1;
    try {
            while ((line = reader.readLine()) != null) {
                if(count>=start){
                    if(stop==0 || count<=stop)
                        list.add(line); 
                }
                count++;
            }
         reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }       
   return list.toArray(new String[list.size()]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.