use of java.io.PipedOutputStream in project i2p.i2p by i2p.
the class LogSettingsTest method testCrit.
public void testCrit() throws IOException {
p.setProperty("logger.record.net.i2p.util.LogSettingsTest", Log.toLevelString(Log.CRIT));
p.setProperty("logger.minimumOnScreenLevel", Log.toLevelString(Log.DEBUG));
DataHelper.storeProps(p, f);
_context.logManager().rereadConfig();
PipedInputStream pin = new PipedInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(pin));
PrintStream systemOut = System.out;
PrintStream pout = new PrintStream(new PipedOutputStream(pin));
System.setOut(pout);
try {
log.debug("CRIT" + ": debug");
log.info("CRIT" + ": info");
log.warn("CRIT" + ": warn");
log.error("CRIT" + ": error");
log.log(Log.CRIT, "CRIT" + ": crit");
_context.logManager().flush();
// the test doesn't hang on failure
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
pout.println("");
pout.flush();
String l1 = in.readLine();
assertTrue(l1.matches(".*CRIT: crit"));
} finally {
System.setOut(systemOut);
pout.close();
}
}
use of java.io.PipedOutputStream in project i2p.i2p by i2p.
the class LogSettingsTest method testWarn.
public void testWarn() throws IOException {
p.setProperty("logger.record.net.i2p.util.LogSettingsTest", Log.toLevelString(Log.WARN));
p.setProperty("logger.minimumOnScreenLevel", Log.toLevelString(Log.DEBUG));
DataHelper.storeProps(p, f);
_context.logManager().rereadConfig();
PipedInputStream pin = new PipedInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(pin));
PrintStream systemOut = System.out;
PrintStream pout = new PrintStream(new PipedOutputStream(pin));
System.setOut(pout);
try {
log.debug("WARN" + ": debug");
log.info("WARN" + ": info");
log.warn("WARN" + ": warn");
log.error("WARN" + ": error");
log.log(Log.CRIT, "WARN" + ": crit");
_context.logManager().flush();
// the test doesn't hang on failure
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
for (int i = 0; i < 3; i++) pout.println("");
pout.flush();
String l1 = in.readLine();
String l2 = in.readLine();
String l3 = in.readLine();
assertTrue(l1.matches(".*WARN: warn") && l2.matches(".*WARN: error") && l3.matches(".*WARN: crit"));
} finally {
System.setOut(systemOut);
pout.close();
}
}
use of java.io.PipedOutputStream in project sponge by softelnet.
the class InteractiveModeStandaloneTest method testInteractive.
@Test
public void testInteractive() throws Exception {
PipedInputStream in = new PipedInputStream();
try (PipedOutputStream pipedOutputStream = new PipedOutputStream(in)) {
outIn = pipedOutputStream;
ByteArrayOutputStream out = new ByteArrayOutputStream();
engine = StandaloneSpongeEngine.builder().commandLineArgs("-k", "examples/standalone/interactive.py", "-i").interactiveModeConsoleSupplier(() -> {
JLineInteractiveModeConsole console = new JLineInteractiveModeConsole();
console.setTerminalBuilder(TerminalBuilder.builder().streams(in, out));
return console;
}).build();
SpongeUtils.executeConcurrentlyOnce(engine, () -> {
engine.startup();
engine.getInteractiveMode().loop();
});
await().atMost(10, TimeUnit.SECONDS).until(() -> engine != null && engine.isRunning() && engine.getInteractiveMode().isRunning());
// Print the message.
write("print 'Starting interactive mode tests.'");
// Send the alarm.
write("EPS.event(\"alarm\").send()");
await().atMost(10, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable(Number.class, "alarms").intValue() >= 1);
// Create trigger and send event.
writeMulti("class T(Trigger):\\");
writeMulti(" def onConfigure(self):\\");
writeMulti(" self.event = \"notification\"\\");
writeMulti(" def onRun(self, event):\\");
writeMulti(" EPS.getVariable(\"notifications\").incrementAndGet()\\");
writeMulti(" print \"Received the notification!\"");
write("");
write("EPS.enable(T)");
write("EPS.event(\"notification\").send()");
await().atMost(10, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable(Number.class, "notifications").intValue() >= 1);
assertFalse(engine.isError());
} finally {
if (engine != null) {
engine.shutdown();
}
}
}
use of java.io.PipedOutputStream in project ovirt-engine by oVirt.
the class SSHClient method sendFile.
/**
* Send file using compression and digest check.
*
* We read the file content into gzip and then pipe it into the ssh. Calculating the remoteDigest on the fly.
*
* The digest is printed into stderr for us to collect.
*
* @param file1
* source.
* @param file2
* destination.
*/
public void sendFile(String file1, String file2) throws Exception {
log.debug("Sending: '{}' '{}'", file1, file2);
remoteFileName(file2);
MessageDigest localDigest = MessageDigest.getInstance("MD5");
// file1->{}->digest->in->out->pout->pin->stdin
Thread t = null;
try (final InputStream in = new DigestInputStream(new FileInputStream(file1), localDigest);
final PipedInputStream pin = new PipedInputStream(STREAM_BUFFER_SIZE);
final OutputStream pout = new PipedOutputStream(pin);
final OutputStream dummy = new ConstraintByteArrayOutputStream(CONSTRAINT_BUFFER_SIZE);
final ByteArrayOutputStream remoteDigest = new ConstraintByteArrayOutputStream(CONSTRAINT_BUFFER_SIZE)) {
t = new Thread(() -> {
try (OutputStream out = new GZIPOutputStream(pout)) {
byte[] b = new byte[STREAM_BUFFER_SIZE];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
} catch (IOException e) {
log.debug("Exceution during stream processing", e);
}
}, "SSHClient.compress " + file1);
t.start();
executeCommand(String.format(COMMAND_FILE_SEND, "gunzip -q", file2), pin, dummy, remoteDigest);
t.join(THREAD_JOIN_WAIT_TIME);
if (t.getState() != Thread.State.TERMINATED) {
throw new IllegalStateException("Cannot stop SSH stream thread");
}
validateDigest(localDigest, new String(remoteDigest.toByteArray(), StandardCharsets.UTF_8).trim());
} catch (Exception e) {
log.debug("Send failed", e);
throw e;
} finally {
if (t != null) {
t.interrupt();
}
}
log.debug("Sent: '{}' '{}'", file1, file2);
}
use of java.io.PipedOutputStream in project ovirt-engine by oVirt.
the class SSHClient method receiveFile.
/**
* Receive file using compression and localDigest check.
*
* We read the stream and pipe into gunzip, and write into the file. Calculating the remoteDigest on the fly.
*
* The localDigest is printed into stderr for us to collect.
*
* @param file1
* source.
* @param file2
* destination.
*/
public void receiveFile(String file1, String file2) throws Exception {
log.debug("Receiving: '{}' '{}'", file1, file2);
remoteFileName(file1);
MessageDigest localDigest = MessageDigest.getInstance("MD5");
// stdout->pout->pin->in->out->digest->{}->file2
Thread t = null;
try (final PipedOutputStream pout = new PipedOutputStream();
final InputStream pin = new PipedInputStream(pout, STREAM_BUFFER_SIZE);
final OutputStream out = new DigestOutputStream(new FileOutputStream(file2), localDigest);
final InputStream empty = new ByteArrayInputStream(new byte[0]);
final ByteArrayOutputStream remoteDigest = new ConstraintByteArrayOutputStream(CONSTRAINT_BUFFER_SIZE)) {
t = new Thread(() -> {
try (final InputStream in = new GZIPInputStream(pin)) {
byte[] b = new byte[STREAM_BUFFER_SIZE];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
} catch (IOException e) {
log.debug("Exceution during stream processing", e);
}
}, "SSHClient.decompress " + file2);
t.start();
executeCommand(String.format(COMMAND_FILE_RECEIVE, "gzip -q", file1), empty, pout, remoteDigest);
t.join(THREAD_JOIN_WAIT_TIME);
if (t.getState() != Thread.State.TERMINATED) {
throw new IllegalStateException("Cannot stop SSH stream thread");
}
validateDigest(localDigest, new String(remoteDigest.toByteArray(), StandardCharsets.UTF_8).trim());
} catch (Exception e) {
log.debug("Receive failed", e);
throw e;
} finally {
if (t != null) {
t.interrupt();
}
}
log.debug("Received: '{}' '{}'", file1, file2);
}
Aggregations