forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStdoutJsonParser.java
120 lines (101 loc) · 3.24 KB
/
StdoutJsonParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.utils;
import java.util.ArrayList;
import java.util.List;
/**
* A class to process regular text output intermixed with newline-delimited JSON.
* <p>
* JSON lines starting with [{ are never split into multiple lines even if they
* are emitted over the course of multiple calls to appendOutput. Regular lines
* on the other hand are emitted immediately so users do not have to wait for
* debug output.
*/
public class StdoutJsonParser {
private final StringBuilder buffer = new StringBuilder();
private boolean bufferIsJson = false;
private final List<String> lines = new ArrayList<>();
private boolean eatNextEol = false;
private boolean isPotentialWindowsReturn = false;
/**
* Write new output to this [StdoutJsonParser].
*/
public void appendOutput(String string) {
for (int i = 0; i < string.length(); ++i) {
final char c = string.charAt(i);
if (eatNextEol) {
eatNextEol = false;
if (c == '\n') {
continue;
}
if (c == '\r' && !isPotentialWindowsReturn) {
eatNextEol = true;
isPotentialWindowsReturn = true;
continue;
}
}
if (isPotentialWindowsReturn) {
isPotentialWindowsReturn = false;
if (c != '\n') {
flushLine();
}
}
buffer.append(c);
if (!bufferIsJson && buffer.length() == 2 && buffer.charAt(0) == '[' && c == '{') {
bufferIsJson = true;
}
else if (bufferIsJson && c == ']' && possiblyTerminatesJson(buffer, string, i)) {
flushLine();
}
if (c == '\n') {
flushLine();
}
if (c == '\r') {
// Wait and decide whether to flush depending on next character.
isPotentialWindowsReturn = true;
}
}
// Eagerly flush if we are not within JSON so regular log text is written as soon as possible.
if (!bufferIsJson) {
flushLine();
}
else if (buffer.toString().endsWith("}]")) {
eatNextEol = true;
flushLine();
}
}
private boolean possiblyTerminatesJson(StringBuilder output, String input, int inputIndex) {
// This is an approximate approach to look for json message terminations inside of strings -
// where the normally terminating eol gets separated from the json.
if (output.length() < 2 || inputIndex + 1 >= input.length()) {
return false;
}
// Look for '}', ']', and a letter
final char prev = output.charAt(output.length() - 2);
final char current = output.charAt(output.length() - 1);
final char next = input.charAt(inputIndex + 1);
return prev == '}' && current == ']' && Character.isAlphabetic(next);
}
private void flushLine() {
if (buffer.length() > 0) {
synchronized (lines) {
lines.add(buffer.toString());
}
buffer.setLength(0);
}
bufferIsJson = false;
}
/**
* Read any lines available from the processed output.
*/
public List<String> getAvailableLines() {
synchronized (lines) {
final List<String> copy = new ArrayList<>(lines);
lines.clear();
return copy;
}
}
}