java.io.ByteArrayOutputStream.toString()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(243)

本文整理了Java中java.io.ByteArrayOutputStream.toString()方法的一些代码示例,展示了ByteArrayOutputStream.toString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteArrayOutputStream.toString()方法的具体详情如下:
包路径:java.io.ByteArrayOutputStream
类名称:ByteArrayOutputStream
方法名:toString

ByteArrayOutputStream.toString介绍

[英]Returns the contents of this ByteArrayOutputStream as a string. Any changes made to the receiver after returning will not be reflected in the string returned to the caller.
[中]以字符串形式返回此ByteArrayOutputStream的内容。返回后对接收方所做的任何更改都不会反映在返回给调用方的字符串中。

代码示例

代码示例来源:origin: stackoverflow.com

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");

代码示例来源:origin: spring-projects/spring-framework

@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);
  }
}

代码示例来源:origin: alibaba/arthas

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");
}

代码示例来源:origin: neo4j/neo4j

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

代码示例来源:origin: neo4j/neo4j

@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 );
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadOneByte() throws Exception {
  assertEquals('a', tee.read());
  assertEquals("a", new String(output.toString(ASCII)));
}

代码示例来源:origin: prestodb/presto

@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);
    }
  }
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testReadNothing() throws Exception {
  assertEquals("", new String(output.toString(ASCII)));
}

代码示例来源:origin: stanfordnlp/CoreNLP

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

代码示例来源:origin: Alluxio/alluxio

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

代码示例来源:origin: EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

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

代码示例来源:origin: eclipse-vertx/vert.x

@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");
}

代码示例来源:origin: gocd/gocd

@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));
}

代码示例来源:origin: oblac/jodd

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

代码示例来源:origin: stackoverflow.com

// 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());

代码示例来源:origin: JessYanCoding/MVPArms

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;
  }
}

代码示例来源:origin: neo4j/neo4j

@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 );
}

代码示例来源:origin: commons-io/commons-io

@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)));
}

代码示例来源:origin: apache/kylin

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"));
}

代码示例来源:origin: neo4j/neo4j

@Test
public void canFormatEmptyObject()
{
  json.assemble( new MappingRepresentation( "empty" )
  {
    @Override
    protected void serialize( MappingSerializer serializer )
    {
    }
  } );
  assertEquals( JsonHelper.createJsonFrom( Collections.emptyMap() ), stream.toString() );
}

相关文章