修剪字符串

时间:2019-05-20 09:58:23

标签: java string stream

我正在尝试读取由我的应用程序创建的进程(webtorrent-cli,在NodeJS上运行)的输出。我正在使用this库进行流程管理。 API允许传递OutputStream,该ByteArrayOutputStream将接收来自流程的输出。我正在传递String的实例,每n秒读取其内容为UTF-8 import org.zeroturnaround.exec.ProcessExecutor; import org.zeroturnaround.exec.ProcessResult; import java.io.*; import java.util.concurrent.*; public class General { private static Thread runProcess() { final Object LOCK = new Object(); return new Thread(() -> { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ProcessExecutor executor = new ProcessExecutor() .command("\"node/webtorrent.cmd\"", "download", "\"magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel\"", "--select 1") .destroyOnExit() .redirectOutput(baos) .readOutput(true); Future <ProcessResult> ft = executor.start().getFuture(); // Schedule a service to print the content of baos each second final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(() -> { synchronized (LOCK) { try { String str = baos.toString("UTF-8"); System.out.println("-----------------------------------------------------------------------"); System.out.println(str); System.out.println("-------TRIMMED"); System.out.println(str.trim()); System.out.println("-----------------------------------------------------------------------"); baos.reset(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }, 0, 1, TimeUnit.SECONDS); ft.get(); // Shutdown service.shutdown(); } catch (IOException | InterruptedException | ExecutionException ioe) { ioe.printStackTrace(); } }); } public static void main(String[] args) throws InterruptedException { Thread process = runProcess(); process.start(); process.join(); } } ,然后进一步处理它。这是简化的代码:

-----------------------------------------------------------------------
Downloading: Sintel
Server running at: http://localhost:8000/5
Downloading to: D:\justtest
Speed: 167 KB/s  Downloaded: 786 KB/129 MB  Uploaded: 0 B
Running time: 7 seconds  Time remaining: 13 minutes  Peers: 6/7

S   196.221.61.235:6881       0 B        0 B/s        0 B/s       

... and 6 more

-------TRIMMED
[32mDownloading: Sintel
Server running at: http://localhost:8000/5
Downloading to: D:\justtest
Speed: 167 KB/s  Downloaded: 786 KB/129 MB  Uploaded: 0 B
Running time: 7 seconds  Time remaining: 13 minutes  Peers: 6/7

S   196.221.61.235:6881       0 B        0 B/s        0 B/s       

... and 6 more
-----------------------------------------------------------------------

下载开始后,这是输出的快照:

[32m

请注意,当修剪字符串时,在开头会附加ByteArrayOutputStream。我不知道那是哪里来的。我猜测[32m中已经存在垃圾,但是我应该如何摆脱它呢?这确实让我很难受。我必须用换行符分隔字符串并提取每列的值(例如:下载,服务器在等运行)。如果我分割字符串并继续前进,事情将变得更加混乱。对于每一列的值,我得到[32m,而这次{"{{"errorResponse":{"statusCode":"400", "status":"Bad Request", "messageCode":"MANAGE-INVALIDPAYLOAD", "message":"MANAGE-INVALIDPAYLOAD: (err:FOER0000) Payload has errors in structure, content-type or values. XDMP-VALIDATEUNEXPECTED: (err:XQDY0027) validate strict { fn:trace($struct, $trace-event) } -- Invalid node: Found tprop:directory-scope but expected ((tprop :document-content|tprop:any-property-content|tprop:any-custom-property-content|tprop:property-content),tprop:when?) at fn:doc(\"\")/tprop:trigger-properties/tprop:event/tprop:data-ev ent/tprop:directory-scope using schema \"manage-trigger-properties.xsd\""}} }"} 变得不同(例如23,它是2-4个随机字母,其中肯定包含数字和可选的其他字符,例如[])。如何清理输出?

0 个答案:

没有答案
相关问题