summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java')
-rw-r--r--plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java87
1 files changed, 87 insertions, 0 deletions
diff --git a/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java
new file mode 100644
index 0000000..111f607
--- /dev/null
+++ b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java
@@ -0,0 +1,87 @@
1/*******************************************************************************
2 * Copyright (c) 2013 Intel Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Intel - initial API and implementation
10 *******************************************************************************/
11package org.yocto.remote.utils;
12
13import java.util.ArrayList;
14import java.util.List;
15import java.util.regex.Pattern;
16
17public class ProcessStreamBuffer {
18 private static final String WHITESPACES = "\\s+";
19 List<String> errorLines;
20 List<String> outputLines;
21 boolean isTerminal;
22
23 ProcessStreamBuffer(boolean isTerminal){
24 this.isTerminal = isTerminal;
25 errorLines = new ArrayList<String>();
26 outputLines = new ArrayList<String>();
27 }
28
29 public void addErrorLine(String line){
30 errorLines.add(line);
31 }
32 public void addOutputLine(String line){
33 outputLines.add(line);
34 }
35
36 public List<String> getOutputLines(){
37 return outputLines;
38 }
39
40 public List<String> getErrorLines(){
41 return errorLines;
42 }
43
44 public String getMergedOutputLines(){
45 String returnVal = "";
46 for (int i = 0; i < outputLines.size(); i++) {
47 String line = outputLines.get(i);
48 returnVal += line;
49 if (outputLines.size() > 1 && i != outputLines.size() - 1)
50 returnVal += "\n";
51 }
52 return returnVal;
53 }
54
55 public boolean hasErrors() {
56 return errorLines.size() != 0;
57 }
58
59 public String getLastOutputLineContaining(String str) {
60 if (!errorLines.isEmpty())
61 return null;
62 for (int i = outputLines.size() - 1; i >= 0; i--){
63 String line = outputLines.get(i);
64 if (line.replaceAll(WHITESPACES, "").contains(str.replaceAll(WHITESPACES, "")))
65 return line;
66 }
67 return null;
68 }
69
70 public String getOutputLineContaining(String arg, String pattern) {
71 List<String> lines = null;
72 if (isTerminal)
73 lines = errorLines;
74 else
75 lines = outputLines;
76 for (int i = lines.size() - 1; i >= 0; i--){
77 String line = lines.get(i);
78 if (line.contains(arg)) {
79 String[] tokens = line.split("\\s+");
80 if (Pattern.matches(pattern, tokens[0])) {
81 return tokens[0];
82 }
83 }
84 }
85 return "";
86 }
87}