Skip to content

Commit 43beed8

Browse files
author
Jagrut Kosti
committed
Modified stream read after command execution, using threads.
1 parent 5c9d813 commit 43beed8

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

src/main/java/multichain/command/builders/QueryBuilderCommon.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
import java.io.BufferedReader;
1111
import java.io.IOException;
12+
import java.io.InputStream;
1213
import java.io.InputStreamReader;
1314
import java.util.List;
1415

1516
import com.google.gson.Gson;
1617
import com.google.gson.GsonBuilder;
1718

1819
import multichain.command.MultichainException;
20+
import multichain.object.Stream;
1921

2022
/**
2123
* @author Ub - H. MARTEAU
@@ -138,11 +140,11 @@ private static String removeHeader(String result) {
138140
* @throws MultichainException
139141
*/
140142
protected static String execute(CommandEnum command, String... parameters) throws MultichainException {
141-
BufferedReader stdError = null;
143+
142144
if (!CHAIN.equals("")) {
143145
Runtime rt = Runtime.getRuntime();
144146
Process pr;
145-
String result = "";
147+
146148
try {
147149
if (parameters.length > 0) {
148150
String params = "";
@@ -153,34 +155,22 @@ protected static String execute(CommandEnum command, String... parameters) throw
153155
} else {
154156
pr = rt.exec("multichain-cli " + CHAIN + " " + command.toString().toLowerCase());
155157
}
158+
//Get the output from both error stream and output stream
159+
StreamGobbler errorGobbler = new StreamGobbler(pr.getErrorStream());
160+
StreamGobbler outputGobbler = new StreamGobbler(pr.getInputStream());
156161

157-
BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
162+
errorGobbler.start();
163+
outputGobbler.start();
158164

159-
stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
165+
pr.waitFor();
160166

161-
// read the output from the command
162-
String s;
163-
while ((s = stdInput.readLine()) != null) {
164-
result = result.concat(s + "\n");
165-
}
166-
} catch (IOException e) {
167-
// TODO Auto-generated catch block
168-
e.printStackTrace();
169-
}
167+
if(outputGobbler.output.length() > 0)
168+
return outputGobbler.output;
170169

171-
if (!result.isEmpty() && !result.equalsIgnoreCase("")) {
172-
return result;
173-
} else {
174-
// read any errors from the attempted command
175-
String s;
176-
try {
177-
while ((s = stdError.readLine()) != null) {
178-
result = result.concat(s + "\n");
179-
}
180-
} catch (IOException e) {
181-
e.printStackTrace();
182-
}
183-
throw new MultichainException(null, result);
170+
return errorGobbler.output;
171+
} catch (IOException | InterruptedException e) {
172+
e.printStackTrace();
173+
return "";
184174
}
185175
} else {
186176
return "ERROR, CHAIN NAME ARE EMPTY !";
@@ -291,5 +281,24 @@ protected static void setCHAIN(String cHAIN) {
291281
CHAIN = cHAIN;
292282
}
293283

284+
static class StreamGobbler extends Thread {
285+
private InputStream is;
286+
private String output = "";
287+
288+
StreamGobbler(InputStream is) {
289+
this.is = is;
290+
}
291+
292+
public void run() {
293+
try {
294+
BufferedReader br = new BufferedReader(new InputStreamReader(is));
295+
String line;
296+
while ( (line = br.readLine()) != null)
297+
output = output.concat(line + "\n");
298+
} catch (IOException ioe) {
299+
ioe.printStackTrace();
300+
}
301+
}
302+
}
294303

295304
}

src/test/java/command/CommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static void testListStreams() {
7878
private static void testPublishStream() {
7979
String transactionId = null;
8080
try {
81-
transactionId = StreamCommand.publishStream("unpublishedwork", "doc1", "5361792048656c6c6f20746f204d79204c6974746c6520467269656e64");
81+
transactionId = StreamCommand.publishStream("unpublishedwork", "FactomLedgerbyConsensus.pdf", "7B22746578744D696E48617368223A5B2D323134363834333833332C2D323134363831363436312C2D323134373135363731332C2D323134373333363033342C2D323134363932313539392C2D323134363735343332372C2D323134373334353731392C2D323134373431363833302C2D323134363532303837382C2D323134363738363035302C2D323134363533303432322C2D323134363435313237342C2D323134373330383130352C2D323134363736313432352C2D323134373339323835332C2D323134373434373734332C2D323134363633313732302C2D323134363936383036332C2D323134373239303937352C2D323134363236303933342C2D323134363538333736362C2D323134363835323833392C2D323134373036343333392C2D323134373337323337362C2D323134363930313230322C2D323134363636393631322C2D323134353739373437382C2D323134373337383531362C2D323134373337313135392C2D323134363634393731332C2D323134373030333939302C2D323134373135313839342C2D323134373134303037392C2D323134373436303238302C2D323134363938303933382C2D323134363535343338352C2D323134373339373834372C2D323134363833363831352C2D323134363932393539362C2D323134353032353634362C2D323134373033383332342C2D323134363934363832332C2D323134373139363731322C2D323134363736383635322C2D323134363032323837362C2D323134373435343230302C2D323134373033353235392C2D323134373036353933332C2D323134373034383035362C2D323134373230363537342C2D323134373332333737322C2D323134363234373231322C2D323134373033333436332C2D323134373133303531332C2D323134373030323632322C2D323134373035373833362C2D323134373138323432372C2D323134373336363539322C2D323134373330363637312C2D323134363730313338362C2D323134373039333131332C2D323134363636363235302C2D323134373030383633332C2D323134373433343530352C2D323134373230383437332C2D323134343837303838362C2D323134373430393236382C2D323134373431373236332C2D323134373038323435352C2D323134373438303530322C2D323134373434373734342C2D323134373330323132342C2D323134363933303030342C2D323134373136393537372C2D323134373230363933392C2D323134363539393839352C2D323134363635383136372C2D323134373433363535382C2D323134363831373834372C2D323134373432373731322C2D323134373138393033372C2D323134373131373338392C2D323134363939333831362C2D323134373138303038312C2D323134373137383336362C2D323134373431303732372C2D323134363137333031312C2D323134363739333338352C2D323134373430323437372C2D323134373031373036352C2D323134363437393434342C2D323134373130393530322C2D323134363939323439322C2D323134373232383038372C2D323134353733313738372C2D323134373336303737312C2D323134373036353730352C2D323134373133313831312C2D323134363632343737372C2D323134373335393837302C2D323134373038393932382C2D323134363933383439302C2D323134373132353930352C2D323134363631353133382C2D323134373035333131332C2D323134363131393332372C2D323134363935313734352C2D323134363939343035312C2D323134363639353733352C2D323134373239363033342C2D323134373130363136312C2D323134373035333532322C2D323134363131313636392C2D323134363233363538352C2D323134373031353333352C2D323134363534363338332C2D323134373434363831362C2D323134373232393734342C2D323134363936323331362C2D323134373133343332322C2D323134373339373330322C2D323134363832383036352C2D323134373332353932392C2D323134363430393033352C2D323134373335383538352C2D323134363839313132312C2D323134373437333434332C2D323134373434393630332C2D323134363135363736362C2D323134373434303435312C2D323134363534353839312C2D323134373232333336322C2D323134363532353836362C2D323134363730333334392C2D323134373030313133362C2D323134373237333432332C2D323134373438303533302C2D323134373331373134392C2D323134373235373332352C2D323134363835393636382C2D323134373434313630372C2D323134373034363831312C2D323134363636353838362C2D323134373333343630382C2D323134363830373038342C2D323134373132363936352C2D323134363734383536322C2D323134373232383832362C2D323134353337333930362C2D323134343734343133362C2D323134373237303732332C2D323134373331383539352C2D323134363534363535342C2D323134363634363334312C2D323134373137323739302C2D323134373037333231302C2D323134343432373833372C2D323134373038313930342C2D323134363935343837382C2D323134363736303039302C2D323134363637393335372C2D323134373136383937342C2D323134363132333539352C2D323134373039343530322C2D323134373334353436322C2D323134373338333932302C2D323134363931363132302C2D323134373435353734342C2D323134363730373233312C2D323134373433353638392C2D323134363631383436382C2D323134363537333835302C2D323134373436393031342C2D323134373032333534312C2D323134363836373731302C2D323134353439313638342C2D323134363632303633322C2D323134373036343634382C2D323134373036303938392C2D323134373336313536372C2D323134363635353530372C2D323134363632313530332C2D323134373234393933372C2D323134373134363737312C2D323134373437393733392C2D323134343837353534362C2D323134373339393031372C2D323134373039353836392C2D323134373237303436372C2D323134363637363735372C2D323134363037393233382C2D323134363131393432312C2D323134373335303732392C2D323134373437323735362C2D323134373138383333382C2D323134363933313836382C2D323134373336353233302C2D323134363431363834332C2D323134373333393130322C2D323134363533373133355D2C22696D61676548617368223A5B2230383261383864653833346132336364363139363038616531363130343062343962363662656363303432633561396236663662613762353339623337383063222C2237643362373431633765353865633931653764653264323631333436356266313064333533336237353135653733626532633230346366623035666664353830222C2237626565323131383832623166323238323138336135636666663338626261356562383961323162306238336132333930363737613636336136336261626639222C2233643235613331313235336664363562326233353966396637653739373734633763616436653333626230323838626262333737646534306161396662626665222C2232626561326361373462313636303233663161653232616430393561663738646363356431653737376161356132393063336636343565393334623935303530225D7D");
8282
System.out.println(transactionId);
8383
} catch (MultichainException e) {
8484
e.printStackTrace();
@@ -169,8 +169,8 @@ public static void main(String[] args) {
169169
ChainCommand.initializeChain("plagchain");
170170
//testgetAddresses();
171171
//testCreateStream();
172-
testListStreams();
173-
//testPublishStream();
172+
//testListStreams();
173+
testPublishStream();
174174
//testSubscribe();
175175
//testUnsubscribe();
176176
//testGetStreamItem();

0 commit comments

Comments
 (0)