简体   繁体   中英

How do I get my InputStream to give me lines as a String array?

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.

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 InputStream s 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);
    }
}

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()]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM