Java Code Examples for java.io.OutputStream.toString()

The following are Jave code examples for showing how to use toString() of the java.io.OutputStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: framework   File: JaxbParser.java   View Source Code Vote up 6 votes
/**
 * 转为xml串
 *
 * @param obj
 * @return
 */
public String toXML(Object obj) {
	String result = null;
	try {
		JAXBContext context = JAXBContext.newInstance(obj.getClass());
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉报文头
		OutputStream os = new ByteArrayOutputStream();
		XMLSerializer serializer = getXMLSerializer(os);
		m.marshal(obj, serializer.asContentHandler());
		result = os.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
	logger.info("response text:" + result);
	return result;
}
 
Example 2
Project: minikube-build-tools-for-java   File: BlobTest.java   View Source Code Vote up 6 votes
/** Checks that the {@link Blob} streams the expected string. */
private void verifyBlobWriteTo(String expected, Blob blob) throws IOException {
  OutputStream outputStream = new ByteArrayOutputStream();
  BlobDescriptor blobDescriptor = blob.writeTo(outputStream);

  String output = outputStream.toString();
  Assert.assertEquals(expected, output);

  byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8);
  Assert.assertEquals(expectedBytes.length, blobDescriptor.getSize());

  CountingDigestOutputStream countingDigestOutputStream =
      new CountingDigestOutputStream(Mockito.mock(OutputStream.class));
  countingDigestOutputStream.write(expectedBytes);
  DescriptorDigest expectedDigest = countingDigestOutputStream.toBlobDescriptor().getDigest();
  Assert.assertEquals(expectedDigest, blobDescriptor.getDigest());
}
 
Example 3
Project: joai-project   File: PageDesc.java   View Source Code Vote up 6 votes
void printtoString(InputStream istm) {

		int k;
		int aBuffSize = 11000;
		String StringFromWS = "";
		byte buff[] = new byte[aBuffSize];
		OutputStream xOutputStream = new ByteArrayOutputStream(aBuffSize);

		try {
			while ((k = istm.read(buff)) != -1)
				xOutputStream.write(buff, 0, k);

			// I can now grab the string I want
			StringFromWS = StringFromWS + xOutputStream.toString();
			System.out.println("\n\n\n\n\n String Content of the page is:");
			System.out.println(StringFromWS);
			System.out.println("\n\n\n\n\n\n\n");
		} catch (IOException e) {

			System.out.println("Exception in printtoString method. :" + e);

		}
	}
 
Example 4
Project: oneops   File: TransUtil.java   View Source Code Vote up 6 votes
public Map<String,String> keyGen(String passPhrase, String pubDesc) {
	JSch jsch=new JSch();
       String passphrase= (passPhrase == null) ? "" : passPhrase;
       Map<String,String> result = new HashMap<String,String>();
       try{
        KeyPair kpair=KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048);
        kpair.setPassphrase(passphrase);
        OutputStream prkos = new ByteArrayOutputStream();
        kpair.writePrivateKey(prkos);
        String privateKey = prkos.toString();
        //removing "\n" at the end of the string
        result.put("private", privateKey.substring(0, privateKey.length() - 1));
        OutputStream pubkos = new ByteArrayOutputStream();
        kpair.writePublicKey(pubkos, pubDesc);
        String pubKey = pubkos.toString();
        //removing "\n" at the end of the string
        result.put("public", pubKey.substring(0, pubKey.length() - 1));
        kpair.dispose();
        return result;
       } catch(Exception e){
       	System.out.println(e);
       	logger.error(e.getMessage());
       	throw new TransistorException(CmsError.TRANSISTOR_EXCEPTION, e.getMessage());
       }
}
 
Example 5
Project: secondbase   File: PrometheusWebConsoleTest.java   View Source Code Vote up 6 votes
@Test
public void parseMetrics() throws IOException {
    final Counter counter = Counter
            .build("testcounter", "testdescription")
            .labelNames("one", "two")
            .register();
    counter.labels("1", "2").inc();
    final HttpExchange mockExchange = mock(HttpExchange.class);
    final OutputStream outputStream = new ByteArrayOutputStream();
    when(mockExchange.getResponseBody()).thenReturn(outputStream);
    final Headers headers = new Headers();
    when(mockExchange.getResponseHeaders()).thenReturn(headers);

    new PrometheusWebConsole().getServlet().handle(mockExchange);

    assertEquals(TextFormat.CONTENT_TYPE_004, headers.get("Content-Type").get(0));

    final String response = outputStream.toString();
    assertTrue(response.contains("# HELP testcounter testdescription"));
    assertTrue(response.contains("# TYPE testcounter counter"));
    assertTrue(response.contains("one=\"1\""));
    assertTrue(response.contains("two=\"2\""));
}
 
Example 6
Project: ares   File: AppConfigurationServiceTest.java   View Source Code Vote up 6 votes
/**
 * Tests the app configuration serialization to a resource.
 * In this test the configuration file provides with complete default settings.
 */
@Test
public void test_to_default() throws IOException {
  InputStream injson = AppConfigurationServiceTest.class.getResourceAsStream("/config/default.check.json");
  InputStream inyaml = AppConfigurationServiceTest.class.getResourceAsStream("/config/default.check.yaml");

  AppConfiguration config = new AppConfiguration();

  OutputStream outjson = new ByteArrayOutputStream();
  OutputStream outyaml = new ByteArrayOutputStream();
  AppConfigurationService.to(AppConfigurationFormat.JSON, outjson, config);
  AppConfigurationService.to(AppConfigurationFormat.YAML, outyaml, config);
  String actualJson = outjson.toString();
  String actualYaml = outyaml.toString();

  String expectedJson = IOUtils.toString(injson, Charset.defaultCharset());
  String expectedYaml = IOUtils.toString(inyaml, Charset.defaultCharset());

  Assert.assertEquals(expectedJson, actualJson);
  Assert.assertEquals(expectedYaml, actualYaml);
}
 
Example 7
Project: letv   File: iw.java   View Source Code Vote up 5 votes
public String a(InputStream inputStream) throws IOException {
    if (inputStream == null) {
        return null;
    }
    OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    jn.a(inputStream, byteArrayOutputStream);
    return byteArrayOutputStream.toString();
}
 
Example 8
Project: letv   File: HttpRequest.java   View Source Code Vote up 5 votes
public String body(String charset) throws HttpRequestException {
    OutputStream output = byteStream();
    try {
        copy(buffer(), output);
        return output.toString(getValidCharset(charset));
    } catch (IOException e) {
        throw new HttpRequestException(e);
    }
}
 
Example 9
Project: ares   File: AppConfigurationServiceTest.java   View Source Code Vote up 4 votes
/**
 * Tests the app configuration serialization from a resource.
 * In this test the configuration file provides with complete custom settings.
 * The configuration file has non-null values and template string (${RES}).
 */
@Test
public void test_to_custom() throws IOException, ParseException {
  InputStream injson = AppConfigurationServiceTest.class.getResourceAsStream("/config/custom.check.json");
  InputStream inyaml = AppConfigurationServiceTest.class.getResourceAsStream("/config/custom.check.yaml");

  AppConfiguration expected = new AppConfiguration();
  expected.setCnfInfo(true);
  expected.setTgtInfo(true);
  expected.setSysInfo(false);
  expected.setNetInfo(false);
  expected.setPolling(new Interval(10, 15, TimeUnit.SECONDS));
  expected.setReconnections(5L);
  expected.setReconnectionWait(new Interval(10, 15, TimeUnit.SECONDS));
  expected.setProxy(new HttpProxy("192.168.0.1", 8080));
  expected.setAuthentication(new HashMap<String,String>(){{put("User-Agent", "CustomUserAgent");}});
  List<Controller> controllers = new ArrayList<>();
  Controller controller1 = new Controller(
      "init", "cmd", "log");
  Controller controller2 = new Controller(
      "init2", "cmd2", "log2",
      new Interval(10, 20, TimeUnit.SECONDS),
      10L,
      new Interval(10, 20, TimeUnit.SECONDS),
      HttpProxy.NONE,
      null,
      null);
  Controller controller3 = new Controller(
      "init3", "cmd3", "log3",
      new Interval(10, 20, TimeUnit.SECONDS),
      10L,
      new Interval(10, 20, TimeUnit.SECONDS),
      new HttpProxy("192.168.0.1", 8080),
      "* * * ? * SAT,SUN",
      new HashMap<String,String>(){{put("User-Agent", "CustomUserAgent");}});
  controllers.add(controller1);
  controllers.add(controller2);
  controllers.add(controller3);
  expected.setControllers(controllers);

  OutputStream outjson = new ByteArrayOutputStream();
  OutputStream outyaml = new ByteArrayOutputStream();
  AppConfigurationService.to(AppConfigurationFormat.JSON, outjson, expected);
  AppConfigurationService.to(AppConfigurationFormat.YAML, outyaml, expected);
  String actualJson = outjson.toString();
  String actualYaml = outyaml.toString();

  String expectedJson = IOUtils.toString(injson, Charset.defaultCharset());
  String expectedYaml = IOUtils.toString(inyaml, Charset.defaultCharset());

  Assert.assertEquals(expectedJson, actualJson);
  Assert.assertEquals(expectedYaml, actualYaml);
}