Search in sources :

Example 86 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project sling by apache.

the class DistributionPackageUtilsTest method testInfoEmptyStreams.

@Test
public void testInfoEmptyStreams() {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Map<String, Object> info = new HashMap<String, Object>();
    DistributionPackageUtils.writeInfo(outputStream, info);
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    Map<String, Object> resultInfo = new HashMap<String, Object>();
    DistributionPackageUtils.readInfo(inputStream, resultInfo);
    assertEquals(info.size(), resultInfo.size());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Test(org.junit.Test)

Example 87 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project gocd by gocd.

the class TestUtils method suppressConsoleOutput.

public static void suppressConsoleOutput() {
    console = new ByteArrayOutputStream();
    PrintStream collector = new PrintStream(console);
    systemOut = System.out;
    systemErr = System.err;
    System.setOut(collector);
    System.setErr(collector);
}
Also used : ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 88 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project syncany by syncany.

the class TestCliUtil method runAndCaptureOutput.

public static String[] runAndCaptureOutput(CommandLineClient cli) throws Exception {
    ByteArrayOutputStream bufferedCliOut = new ByteArrayOutputStream();
    cli.setOut(new SplitOutputStream(bufferedCliOut, System.out));
    cli.start();
    logger.log(Level.INFO, "CLI output: ");
    logger.log(Level.INFO, toString(bufferedCliOut));
    return toStringArray(bufferedCliOut);
}
Also used : ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 89 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project gradle by gradle.

the class PropertiesUtils method store.

/**
 * Writes {@link java.util.Properties} in a way that the results can be expected to be reproducible.
 *
 * <p>There are a number of differences compared to {@link java.util.Properties#store(java.io.Writer, String)}:</p>
 * <ul>
 *     <li>no timestamp comment is generated at the beginning of the file</li>
 *     <li>the lines in the resulting files are separated by a pre-set separator instead of the system default line separator</li>
 *     <li>the properties are sorted alphabetically</li>
 * </ul>
 *
 * <p>Like with {@link java.util.Properties#store(java.io.OutputStream, String)}, Unicode characters are
 * escaped when using the default Latin-1 (ISO-8559-1) encoding.</p>
 */
public static void store(Properties properties, OutputStream outputStream, @Nullable String comment, Charset charset, String lineSeparator) throws IOException {
    String rawContents;
    if (charset.equals(Charsets.ISO_8859_1)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        properties.store(out, comment);
        rawContents = new String(out.toByteArray(), Charsets.ISO_8859_1);
    } else {
        StringWriter out = new StringWriter();
        properties.store(out, comment);
        rawContents = out.toString();
    }
    String systemLineSeparator = SystemProperties.getInstance().getLineSeparator();
    List<String> lines = Lists.newArrayList(Splitter.on(systemLineSeparator).omitEmptyStrings().split(rawContents));
    int lastCommentLine = -1;
    for (int lineNo = 0, len = lines.size(); lineNo < len; lineNo++) {
        String line = lines.get(lineNo);
        if (line.startsWith("#")) {
            lastCommentLine = lineNo;
        }
    }
    // The last comment line is the timestamp
    List<String> nonCommentLines;
    if (lastCommentLine != -1) {
        lines.remove(lastCommentLine);
        nonCommentLines = lines.subList(lastCommentLine, lines.size());
    } else {
        nonCommentLines = lines;
    }
    Collections.sort(nonCommentLines);
    StringBuilder builder = new StringBuilder();
    for (String line : lines) {
        builder.append(line);
        builder.append(lineSeparator);
    }
    outputStream.write(builder.toString().getBytes(charset));
}
Also used : StringWriter(java.io.StringWriter) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Example 90 with ByteArrayOutputStream

use of org.apache.commons.io.output.ByteArrayOutputStream in project metron by apache.

the class SerDeUtils method toBytes.

/**
 * Serialize a profile measurement's value.
 *
 * The value produced by a Profile definition can be any numeric data type.  The data
 * type depends on how the profile is defined by the user.  The user should be able to
 * choose the data type that is most suitable for their use case.
 *
 * @param value The value to serialize.
 */
public static byte[] toBytes(Object value) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Output output = new Output(bos);
        kryo.get().writeClassAndObject(output, value);
        output.flush();
        bos.flush();
        return bos.toByteArray();
    } catch (Throwable t) {
        LOG.error("Unable to serialize: " + value + " because " + t.getMessage(), t);
        throw new IllegalStateException("Unable to serialize " + value + " because " + t.getMessage(), t);
    }
}
Also used : Output(com.esotericsoftware.kryo.io.Output) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream)

Aggregations

ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)92 Test (org.junit.Test)36 DataOutputStream (java.io.DataOutputStream)15 IOException (java.io.IOException)14 HashSet (java.util.HashSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 Configuration (org.apache.hadoop.conf.Configuration)12 PrintStream (java.io.PrintStream)10 SparkConf (org.apache.spark.SparkConf)10 Edge (uk.gov.gchq.gaffer.data.element.Edge)10 Element (uk.gov.gchq.gaffer.data.element.Element)10 Entity (uk.gov.gchq.gaffer.data.element.Entity)10 Graph (uk.gov.gchq.gaffer.graph.Graph)10 User (uk.gov.gchq.gaffer.user.User)10 File (java.io.File)9 HashMap (java.util.HashMap)8 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)6 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)6