1package com.android.bugreportsender;
2
3import android.util.Log;
4
5import java.io.BufferedReader;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.InputStreamReader;
9
10/**
11 * Utility class for parsing a bugreport into its sections.
12 */
13public final class BugReportParser {
14    private static final int BUFFER_SIZE = 8*1024;
15    private static final String SECTION_HEADER = "------";
16    private static final int MAX_LINES = 1000; // just in case we miss the end of the section.
17
18    // utility class
19    private BugReportParser() {}
20
21    public static String extractSystemLogs(InputStream in, String section) throws IOException {
22        final String sectionWithHeader = SECTION_HEADER + " " + section;
23        StringBuilder sb = new StringBuilder();
24        // open a reader around the provided inputstream.
25        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), BUFFER_SIZE);
26        boolean inSection = false;
27        int numLines = 0;
28        // read file contents.  loop until we get to the appropriate section header.
29        // once we reach that header, accumulate all lines until we get to the next section.
30        String line = null;
31        while ((line = reader.readLine()) != null) {
32            if (inSection) {
33                // finish when we get to:
34                // -----
35                if (line.startsWith(SECTION_HEADER) || (numLines > MAX_LINES)) {
36                    break;
37                }
38                sb.append(line);
39                sb.append("\n");
40                ++numLines;
41            } else if (line.startsWith(sectionWithHeader)) {
42                sb.append(line);
43                sb.append("\n");
44                inSection = true;
45            }
46        }
47        return sb.toString();
48    }
49}
50