Search in sources :

Example 76 with PipedOutputStream

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();
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 77 with PipedOutputStream

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();
    }
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream)

Example 78 with PipedOutputStream

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();
        }
    }
}
Also used : JLineInteractiveModeConsole(org.openksavi.sponge.standalone.interactive.JLineInteractiveModeConsole) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 79 with PipedOutputStream

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);
}
Also used : DigestInputStream(java.security.DigestInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) FileInputStream(java.io.FileInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PipedOutputStream(java.io.PipedOutputStream) DigestOutputStream(java.security.DigestOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AuthenticationException(javax.naming.AuthenticationException) DecoderException(org.apache.commons.codec.DecoderException) IOException(java.io.IOException) TimeLimitExceededException(javax.naming.TimeLimitExceededException) GZIPOutputStream(java.util.zip.GZIPOutputStream) MessageDigest(java.security.MessageDigest)

Example 80 with PipedOutputStream

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);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) FileInputStream(java.io.FileInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) PipedOutputStream(java.io.PipedOutputStream) DigestOutputStream(java.security.DigestOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AuthenticationException(javax.naming.AuthenticationException) DecoderException(org.apache.commons.codec.DecoderException) IOException(java.io.IOException) TimeLimitExceededException(javax.naming.TimeLimitExceededException) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest)

Aggregations

PipedOutputStream (java.io.PipedOutputStream)221 PipedInputStream (java.io.PipedInputStream)199 IOException (java.io.IOException)89 Test (org.junit.Test)54 InputStream (java.io.InputStream)30 OutputStream (java.io.OutputStream)23 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)21 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)21 PrintStream (java.io.PrintStream)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)17 TypeToken (com.google.common.reflect.TypeToken)17 InputStreamReader (java.io.InputStreamReader)16 DataInputStream (java.io.DataInputStream)14 DataOutputStream (java.io.DataOutputStream)14 BufferedReader (java.io.BufferedReader)13 Before (org.junit.Before)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ExecutorService (java.util.concurrent.ExecutorService)9 ArrayList (java.util.ArrayList)7