Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SampleOutPrinter.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package org.dartlang.vm.service;
15
16import com.google.common.base.Charsets;
17
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.InputStream;
21import java.io.InputStreamReader;
22
23/**
24 * Echo the content of a stream to {@link System.out} with the given prefix.
25 */
26public class SampleOutPrinter {
27 private class LinesReaderThread extends Thread {
28 public LinesReaderThread() {
29 setName("SampleOutPrinter.LinesReaderThread - " + prefix);
30 setDaemon(true);
31 }
32
33 @Override
34 public void run() {
35 while (true) {
36 String line;
37 try {
38 line = reader.readLine();
39 } catch (IOException e) {
40 System.out.println("Exception reading sample stream");
41 e.printStackTrace();
42 return;
43 }
44 // check for EOF
45 if (line == null) {
46 return;
47 }
48 synchronized (currentLineLock) {
49 currentLine = line;
50 currentLineLock.notifyAll();
51 }
52 System.out.println("[" + prefix + "] " + line);
53 }
54 }
55 }
56
57 private String currentLine;
58
59 private final Object currentLineLock = new Object();
60
61 private final String prefix;
62 private final BufferedReader reader;
63
64 public SampleOutPrinter(String prefix, InputStream stream) {
65 this.prefix = prefix;
66 this.reader = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8));
67 new LinesReaderThread().start();
68 }
69
70 public void assertEmpty() {
71 synchronized (currentLineLock) {
72 if (currentLine != null) {
73 throw new RuntimeException("Did not expect " + prefix + ": \"" + currentLine + "\"");
74 }
75 }
76 }
77
78 public void assertLastLine(String text) {
79 synchronized (currentLineLock) {
80 if (text == null) {
81 if (currentLine != null) {
82 throw new RuntimeException("Did not expect " + prefix + ": \"" + currentLine + "\"");
83 }
84 } else {
85 if (currentLine == null || !currentLine.contains(text)) {
86 throw new RuntimeException("Expected current line to contain text\n"
87 + "\nexpected: [" + text + "]"
88 + "\nactual: [" + currentLine + "]");
89 }
90 }
91 }
92 }
93}
SampleOutPrinter(String prefix, InputStream stream)
std::u16string text
Definition run.py:1