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());
}
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);
}
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);
}
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));
}
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);
}
}
Aggregations