ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8");
@Override public String convert(Properties source) { try { ByteArrayOutputStream os = new ByteArrayOutputStream(256); source.store(os, null); return os.toString("ISO-8859-1"); } catch (IOException ex) { // Should never happen. throw new IllegalArgumentException("Failed to store [" + source + "] into String", ex); } }
public static String toString(InputStream inputStream) throws IOException { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); }
void assertClosed() { if ( !c.isClosed() ) { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream( out ); for ( StackTraceElement traceElement : stackTrace ) { printStream.println( "\tat " + traceElement ); } printStream.println(); throw new IllegalStateException( format( "Closeable %s was not closed!\n%s", c, out.toString() ) ); } } }
@Test public void shouldSerializeResponseWithNoCommitUriResultsOrErrors() throws Exception { // given ByteArrayOutputStream output = new ByteArrayOutputStream(); ExecutionResultSerializer serializer = getSerializerWith( output ); // when serializer.finish(); // then String result = output.toString( UTF_8.name() ); assertEquals( "{\"results\":[],\"errors\":[]}", result ); }
@Test public void testReadOneByte() throws Exception { assertEquals('a', tee.read()); assertEquals("a", new String(output.toString(ASCII))); }
@Override public <T> void postEvent(T event) { try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); JsonGenerator generator = factory.createGenerator(buffer, JsonEncoding.UTF8); serializer.serialize(event, generator); out.println(buffer.toString().trim()); } catch (IOException e) { throw new UncheckedIOException(e); } } }
@Test public void testReadNothing() throws Exception { assertEquals("", new String(output.toString(ASCII))); }
/** * Returns the string value of the stack trace for the given Throwable. */ public static String getStackTraceString(Throwable t) { ByteArrayOutputStream bs = new ByteArrayOutputStream(); t.printStackTrace(new PrintStream(bs)); return bs.toString(); }
@Override public void evaluate() throws Throwable { System.out.println("2048"); Assert.assertEquals("2048\n", OUTPUT.toString()); OUTPUT.reset(); System.out.println("1234"); Assert.assertEquals("1234\n", OUTPUT.toString()); } };
private void doFizzBuzz(final int n, final String s) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(baos); System.setOut(new PrintStream(bos)); this.fb.fizzBuzz(n); System.out.flush(); String platformDependentExpectedResult = s.replaceAll("\\n", System.getProperty("line.separator")); assertEquals(platformDependentExpectedResult, baos.toString()); }
@Test public void testOldBare() throws InterruptedException, IOException { record(); cli.dispatch(new String[]{"-ha"}); assertWaitUntil(() -> error.toString().contains("A quorum has been obtained.")); stop(); assertThat(error.toString()) .contains("Starting clustering...") .contains("No cluster-host specified") .contains("Any deploymentIDs waiting on a quorum will now be deployed"); }
@Test public void shouldSupportEmptyPipelineGroup() throws Exception { PipelineConfigs group = new BasicPipelineConfigs("defaultGroup", new Authorization()); CruiseConfig config = new BasicCruiseConfig(group); ByteArrayOutputStream stream = new ByteArrayOutputStream(); new MagicalGoConfigXmlWriter(configCache, ConfigElementImplementationRegistryMother.withNoPlugins()).write(config, stream, true); GoConfigHolder configHolder = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()) .loadConfigHolder(stream.toString()); assertThat(configHolder.config.findGroup("defaultGroup"), is(group)); }
private ProcessRunner.ProcessResult writeException(final ByteArrayOutputStream baos, final Exception ex) { try { baos.write(errPrefix.getBytes()); } catch (IOException ignore) { } ex.printStackTrace(new PrintStream(baos)); return new ProcessRunner.ProcessResult(-1, baos.toString()); } }
// Create a stream to hold the output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System.out! PrintStream old = System.out; // Tell Java to use your special stream System.setOut(ps); // Print some output: goes to your special stream System.out.println("Foofoofoo!"); // Put things back System.out.flush(); System.setOut(old); // Show what happened System.out.println("Here: " + baos.toString());
public static String bytyToString(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int num = 0; while ((num = in.read(buf)) != -1) { out.write(buf, 0, buf.length); } String result = out.toString(); out.close(); return result; } }
@Test public void shouldSerializeResponseWithCommitUriOnly() throws Exception { // given ByteArrayOutputStream output = new ByteArrayOutputStream(); ExecutionResultSerializer serializer = getSerializerWith( output ); // when serializer.transactionCommitUri( URI.create( "commit/uri/1" ) ); serializer.finish(); // then String result = output.toString( UTF_8.name() ); assertEquals( "{\"commit\":\"commit/uri/1\",\"results\":[],\"errors\":[]}", result ); }
@Test public void testReadToArray() throws Exception { final byte[] buffer = new byte[8]; assertEquals(3, tee.read(buffer)); assertEquals('a', buffer[0]); assertEquals('b', buffer[1]); assertEquals('c', buffer[2]); assertEquals(-1, tee.read(buffer)); assertEquals("abc", new String(output.toString(ASCII))); }
public static void main(String[] args) throws Exception { // KylinConfig.setSandboxEnvIfPossible(); KylinConfig config = KylinConfig.getInstanceFromEnv(); DataModelDesc kylinModel = generateKylinModelForMetricsQuery("ADMIN", config, new HiveSinkTool()); ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(buf); MODELDESC_SERIALIZER.serialize(kylinModel, dout); dout.close(); buf.close(); System.out.println(buf.toString("UTF-8")); }
@Test public void canFormatEmptyObject() { json.assemble( new MappingRepresentation( "empty" ) { @Override protected void serialize( MappingSerializer serializer ) { } } ); assertEquals( JsonHelper.createJsonFrom( Collections.emptyMap() ), stream.toString() ); }