如何让我的InputStream给我作为String数组的行?

时间:2014-12-26 10:27:02

标签: java arrays inputstream

我有一个进程并得到一个类似于:

的InputStream
myInputStream = getProcess().getInputStream();

我想以某种方式想要称呼这些方法

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.   

此外,我将同时运行多个进程,因此将整个日志存储在内存中并不是我认为最好的。

2 个答案:

答案 0 :(得分:0)

Scanner类可能对此有所帮助,因为它可以读取InputStream中的行。请注意,我提供了一个简单的测试用例来验证内容。

我同意@ LouisWasserman的评论InputStream并不是真的适合这种情况,但以下内容可能有效。

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 :(得分:0)

使用BufferedReader逐行读取InputStream。尝试这段代码,或者你可以实现自己的逻辑。

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