Search in sources :

Example 66 with PrintStream

use of java.io.PrintStream in project smile by haifengl.

the class ToyData method main.

public static void main(String[] argv) {
    ToyData toy = new ToyData();
    int n = 100;
    double[][] s = toy.sample(n);
    try (PrintStream p = new PrintStream(new FileOutputStream("toy-train.txt"))) {
        for (int i = 0; i < s.length; i++) {
            int label = i / n;
            p.format("%d\t% .4f\t% .4f\n", label, s[i][0], s[i][1]);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    n = 10000;
    s = toy.sample(n);
    try (PrintStream p = new PrintStream(new FileOutputStream("toy-test.txt"))) {
        for (int i = 0; i < s.length; i++) {
            int label = i / n;
            p.format("%d\t% .4f\t% .4f\n", label, s[i][0], s[i][1]);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException)

Example 67 with PrintStream

use of java.io.PrintStream in project h2o-3 by h2oai.

the class TeeOutputStream method startTestSuite.

public void startTestSuite(String testSuiteName) throws Exception {
    testSuiteStartTime = System.currentTimeMillis();
    document = docBuilder.newDocument();
    // test suite
    testSuiteElement = document.createElement("testsuite");
    document.appendChild(testSuiteElement);
    testSuiteElement.setAttribute("name", StringEscapeUtils.escapeXml(testSuiteName));
    testSuiteElement.setAttribute("timestamp", StringEscapeUtils.escapeXml(dateFormat.format(new Date(testSuiteStartTime))));
    testSuiteElement.setAttribute("hostname", StringEscapeUtils.escapeXml(InetAddress.getLocalHost().getHostName()));
    testSuiteElement.setAttribute("ncpu", StringEscapeUtils.escapeXml(Integer.toString(Runtime.getRuntime().availableProcessors())));
    // system properties
    Element propertiesElement = document.createElement("properties");
    testSuiteElement.appendChild(propertiesElement);
    for (String name : System.getProperties().stringPropertyNames()) {
        Element propertyElement = document.createElement("property");
        propertyElement.setAttribute("name", StringEscapeUtils.escapeXml(name));
        propertyElement.setAttribute("value", StringEscapeUtils.escapeXml(System.getProperty(name)));
        propertiesElement.appendChild(propertyElement);
    }
    // reset counters
    testCount = 0;
    successCount = 0;
    failureCount = 0;
    // redirect outputs
    stdOut = System.out;
    out = new ByteArrayOutputStream();
    System.setOut(new PrintStream(new TeeOutputStream(out, stdOut), true));
    stdErr = System.err;
    err = new ByteArrayOutputStream();
    System.setErr(new PrintStream(new TeeOutputStream(err, stdErr), true));
}
Also used : PrintStream(java.io.PrintStream) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date)

Example 68 with PrintStream

use of java.io.PrintStream in project jslint4java by happygiraffe.

the class StdioResource method captureStderr.

private void captureStderr() {
    origErr = System.err;
    stderrStream = new ByteArrayOutputStream();
    System.setErr(new PrintStream(stderrStream));
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 69 with PrintStream

use of java.io.PrintStream in project jslint4java by happygiraffe.

the class StdioResource method captureStdout.

private void captureStdout() {
    origOut = System.out;
    stdoutStream = new ByteArrayOutputStream();
    System.setOut(new PrintStream(stdoutStream));
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 70 with PrintStream

use of java.io.PrintStream in project Gaffer by gchq.

the class IngestUtils method createSplitsFile.

/**
     * Get the existing splits from a table in Accumulo and write a splits file.
     * The number of splits is returned.
     *
     * @param conn       - An existing connection to an Accumulo instance
     * @param table      - The table name
     * @param fs         - The FileSystem in which to create the splits file
     * @param splitsFile - A Path for the output splits file
     * @param maxSplits  - The maximum number of splits
     * @return The number of splits in the table
     * @throws IOException for any IO issues reading from the file system. Other accumulo exceptions are caught and wrapped in an IOException.
     */
public static int createSplitsFile(final Connector conn, final String table, final FileSystem fs, final Path splitsFile, final int maxSplits) throws IOException {
    LOGGER.info("Creating splits file in location {} from table {} with maximum splits {}", splitsFile, table, maxSplits);
    // Get the splits from the table
    Collection<Text> splits;
    try {
        splits = conn.tableOperations().listSplits(table, maxSplits);
    } catch (TableNotFoundException | AccumuloSecurityException | AccumuloException e) {
        throw new IOException(e.getMessage(), e);
    }
    // This should have returned at most maxSplits splits, but this is not implemented properly in MockInstance.
    if (splits.size() > maxSplits) {
        if (conn instanceof MockConnector) {
            LOGGER.info("Manually reducing the number of splits to {} due to MockInstance not implementing" + " listSplits(table, maxSplits) properly", maxSplits);
        } else {
            LOGGER.info("Manually reducing the number of splits to {} (number of splits was {})", maxSplits, splits.size());
        }
        final Collection<Text> filteredSplits = new TreeSet<>();
        final int outputEveryNth = splits.size() / maxSplits;
        LOGGER.info("Outputting every {}-th split from {} total", outputEveryNth, splits.size());
        int i = 0;
        for (final Text text : splits) {
            if (i % outputEveryNth == 0) {
                filteredSplits.add(text);
            }
            i++;
            if (filteredSplits.size() >= maxSplits) {
                break;
            }
        }
        splits = filteredSplits;
    }
    LOGGER.info("Found {} splits from table {}", splits.size(), table);
    try (final PrintStream out = new PrintStream(new BufferedOutputStream(fs.create(splitsFile, true)), false, CommonConstants.UTF_8)) {
        // Write the splits to file
        if (splits.isEmpty()) {
            out.close();
            return 0;
        }
        for (final Text split : splits) {
            out.println(new String(Base64.encodeBase64(split.getBytes()), CommonConstants.UTF_8));
        }
    }
    return splits.size();
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) PrintStream(java.io.PrintStream) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) TreeSet(java.util.TreeSet) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) MockConnector(org.apache.accumulo.core.client.mock.MockConnector) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

PrintStream (java.io.PrintStream)1828 ByteArrayOutputStream (java.io.ByteArrayOutputStream)805 Test (org.junit.Test)583 File (java.io.File)331 IOException (java.io.IOException)295 FileOutputStream (java.io.FileOutputStream)207 ArrayList (java.util.ArrayList)89 FileNotFoundException (java.io.FileNotFoundException)83 OutputStream (java.io.OutputStream)81 Before (org.junit.Before)66 BufferedReader (java.io.BufferedReader)53 BufferedOutputStream (java.io.BufferedOutputStream)49 Map (java.util.Map)49 Date (java.util.Date)46 Path (org.apache.hadoop.fs.Path)42 UnsupportedEncodingException (java.io.UnsupportedEncodingException)41 InputStreamReader (java.io.InputStreamReader)38 Matchers.anyString (org.mockito.Matchers.anyString)38 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)36 List (java.util.List)35