Example usage for java.io OutputStream OutputStream

List of usage examples for java.io OutputStream OutputStream

Introduction

In this page you can find the example usage for java.io OutputStream OutputStream.

Prototype

OutputStream

Source Link

Usage

From source file:org.kchine.rpf.PoolUtils.java

public static void redirectIO() {
    final JTextArea area = new JTextArea();
    JFrame f = new JFrame("out/err");
    f.add(new JScrollPane(area), BorderLayout.CENTER);
    f.pack();//from   ww  w. j  a  v  a2  s . com
    f.setVisible(true);
    f.setSize(500, 500);
    f.setLocation(100, 100);

    PrintStream ps = new PrintStream(new OutputStream() {
        public void write(final int b) throws IOException {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setText(area.getText() + new String(new byte[] { (byte) b }));
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setCaretPosition(area.getText().length());
                    area.repaint();
                }
            });
        }

        public void write(final byte[] b) throws IOException {

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setText(area.getText() + new String(b));
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setCaretPosition(area.getText().length());
                    area.repaint();
                }
            });
        }

        public void write(byte[] b, int off, int len) throws IOException {
            final byte[] r = new byte[len];
            for (int i = 0; i < len; ++i)
                r[i] = b[off + i];

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setText(area.getText() + new String(r));
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setCaretPosition(area.getText().length());
                    area.repaint();
                }
            });
        }
    });
    System.setOut(ps);
    System.setErr(ps);
}

From source file:org.commoncrawl.hadoop.mergeutils.MergeSortSpillWriter.java

private static OutputStream newOutputStream(final ByteBuffer buf) {
    return new OutputStream() {

        @Override//from www .j a va  2s. c om
        public void write(int b) throws IOException {
            buf.put((byte) (b & 0xff));
        }

        public void write(byte src[], int off, int len) throws IOException {
            buf.put(src, off, len);
        }
    };
}

From source file:ir.ac.iust.nlp.postagger.POSTaggerForm.java

private void btnStartTaggingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartTaggingActionPerformed

    wordModel.clear();/*from  w w w  .ja v  a 2  s. c  o m*/
    predModel.clear();
    goldModel.clear();
    txtTagLog.setText("");
    btnStartTagging.setEnabled(false);
    tabTag.setSelectedIndex(0);
    tabTag.setEnabledAt(1, false);

    File modelFrom = new File(txtModelPath.getText());
    File modelTo = new File(System.getProperty("user.dir") + File.separator + modelFrom.getName());
    if (!modelFrom.equals(modelTo)) {
        modelTo.mkdirs();
        copyDirectory(modelFrom, modelTo);
    }

    PrintStream out = new PrintStream(new OutputStream() {

        private StringBuffer buffer = new StringBuffer();

        @Override
        public void write(int b) throws IOException {
            this.buffer.append((char) b);
            txtTagLog.setText(buffer.toString());
            txtTagLog.setCaretPosition(txtTagLog.getDocument().getLength() - 1);
        }
    });
    RunnableTagging.out = out;

    String gold = null;
    if (chkGoldFile.isSelected() == true) {
        gold = txtGoldFile.getText();
    }

    // Run in a new thread
    Runnable job = new RunnableTagging(this, modelFrom.getName(), txtInputFile.getText(),
            txtOutputPath.getText() + "tagged_output.lbl", gold);
    ExecutorService threadPool = Executors.newFixedThreadPool(1);
    threadPool.execute(job);
    threadPool.shutdown();
}

From source file:ir.ac.iust.nlp.postagger.POSTaggerForm.java

private void btnStartTrainingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartTrainingActionPerformed

    try {//from   w ww. j  av a  2s .  c o m
        txtTrainLog.setText("");
        btnStartTraining.setEnabled(false);

        PrintStream out = new PrintStream(new OutputStream() {

            private StringBuffer buffer = new StringBuffer();

            @Override
            public void write(int b) throws IOException {
                this.buffer.append((char) b);
                txtTrainLog.setText(buffer.toString());
                txtTrainLog.setCaretPosition(txtTrainLog.getDocument().getLength() - 1);
            }
        });
        RunnableTrain.out = out;

        File trainFrom = new File(txtTrainFile.getText());
        File trainTo = new File(System.getProperty("user.dir") + File.separator + trainFrom.getName());
        if (!trainFrom.equals(trainTo))
            FileUtils.copyFile(trainFrom, trainTo);

        File modelTo = new File(txtTrainModelPath.getText());
        File modelFrom = new File(System.getProperty("user.dir") + File.separator + modelTo.getName());
        modelFrom.mkdirs();

        // Run in a new thread
        Runnable job = new RunnableTrain(this, modelTo.getName(), trainFrom.getName(),
                Integer.parseInt(spMaxIters.getValue().toString()));
        ExecutorService threadPool = Executors.newFixedThreadPool(1);
        threadPool.execute(job);
        threadPool.shutdown();
    } catch (IOException | NumberFormatException ex) {
    }
}

From source file:homenetapp.HomeNetAppGui.java

private void redirectSystemStreams() {

    javax.swing.text.Style style = consoleTextPane.addStyle("Red", null);
    javax.swing.text.StyleConstants.setForeground(style, java.awt.Color.red);
    final javax.swing.text.Style redStyle = style;

    style = consoleTextPane.addStyle("White", null);
    javax.swing.text.StyleConstants.setForeground(style, java.awt.Color.white);
    final javax.swing.text.Style whiteStyle = style;

    OutputStream out = new OutputStream() {

        @Override/*from  w  w  w  . j  a  v a 2 s  . c  om*/
        public void write(final int b) throws IOException {
            updateTextPane(String.valueOf((char) b), whiteStyle);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            updateTextPane(new String(b, off, len), whiteStyle);
        }

        @Override
        public void write(byte[] b) throws IOException {
            write(b, 0, b.length);
        }
    };

    OutputStream err = new OutputStream() {

        @Override
        public void write(final int b) throws IOException {
            updateTextPane(String.valueOf((char) b), redStyle);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            updateTextPane(new String(b, off, len), redStyle);
        }

        @Override
        public void write(byte[] b) throws IOException {
            write(b, 0, b.length);
        }
    };

    System.setOut(new PrintStream(out, true));
    System.setErr(new PrintStream(err, true));
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

/**
 * write an object in java Serializable form with a SERIALIZABLE DSCODE so that it can be
 * deserialized with DataSerializer.readObject()
 * /*w ww  .  j a v  a 2s .co  m*/
 * @param o the object to serialize
 * @param out the data output to serialize to
 */
public static void writeSerializableObject(Object o, DataOutput out) throws IOException {
    out.writeByte(SERIALIZABLE);
    if (out instanceof ObjectOutputStream) {
        ((ObjectOutputStream) out).writeObject(o);
    } else {
        OutputStream stream;
        if (out instanceof OutputStream) {
            stream = (OutputStream) out;

        } else {
            final DataOutput out2 = out;
            stream = new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    out2.write(b);
                }
            };
        }
        boolean wasDoNotCopy = false;
        if (out instanceof HeapDataOutputStream) {
            // To fix bug 52197 disable doNotCopy mode
            // while serialize with an ObjectOutputStream.
            // The problem is that ObjectOutputStream keeps
            // an internal byte array that it reuses while serializing.
            wasDoNotCopy = ((HeapDataOutputStream) out).setDoNotCopy(false);
        }
        try {
            ObjectOutput oos = new ObjectOutputStream(stream);
            if (stream instanceof VersionedDataStream) {
                Version v = ((VersionedDataStream) stream).getVersion();
                if (v != null && v != Version.CURRENT) {
                    oos = new VersionedObjectOutput(oos, v);
                }
            }
            oos.writeObject(o);
            // To fix bug 35568 just call flush. We can't call close because
            // it calls close on the wrapped OutputStream.
            oos.flush();
        } finally {
            if (wasDoNotCopy) {
                ((HeapDataOutputStream) out).setDoNotCopy(true);
            }
        }
    }
}

From source file:org.apache.nifi.cluster.manager.impl.WebClusterManager.java

/**
 * Drains the node responses off of the socket to ensure that the socket is appropriately cleaned-up.
 *
 * @param nodeResponses the collection of node responses
 *//*  w  ww  .  j  a va2s  .  c  om*/
private void drainResponses(final Collection<NodeResponse> nodeResponses) {
    // fail fast if nothing to do
    if (nodeResponses.isEmpty()) {
        return;
    }

    final ExecutorService executorService = Executors
            .newFixedThreadPool(properties.getClusterManagerProtocolThreads());
    final CompletionService<Void> completionService = new ExecutorCompletionService<>(executorService);
    for (final NodeResponse nodeResponse : nodeResponses) {
        // if we received a response, then clear out the response data
        if (!nodeResponse.hasThrowable()) {
            completionService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        try (final OutputStream drain = new OutputStream() {
                            @Override
                            public void write(final int b) {
                                /* drain response */ }
                        }) {
                            ((StreamingOutput) nodeResponse.getResponse().getEntity()).write(drain);
                        }
                    } catch (final IOException | WebApplicationException ex) {
                        logger.info("Failed clearing out non-client response buffer due to: " + ex, ex);
                    }
                }
            }, null);
        }
    }

    executorService.shutdown();
}