Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream toString.

Prototype

public synchronized String toString() 

Source Link

Document

Converts the buffer's contents into a string decoding bytes using the platform's default character set.

Usage

From source file:io.wcm.caravan.io.jsontransform.JsonTestHelper.java

public static String toString(final Source source) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try (JacksonStreamSink sink = new JacksonStreamSink(output);) {
        while (source.hasNext()) {
            sink.write(source.next());/*ww  w  .  ja  v  a 2s. c  o  m*/
        }
    }
    return output.toString();
}

From source file:jp.co.conit.sss.sp.ex1.util.HttpUtil.java

/**
 * POST??????/* w  w w.j ava 2 s.  c  o  m*/
 * 
 * @param url API?URL
 * @param httpEntity ?
 * @return
 */
public static String post(String url, UrlEncodedFormEntity httpEntity) throws Exception {

    if (url == null) {
        throw new IllegalArgumentException("'url' must not be null.");
    }
    if (httpEntity == null) {
        throw new IllegalArgumentException("'httpEntity' must not be null.");
    }

    String result = null;

    DefaultHttpClient httpclient = new DefaultHttpClient(getHttpParam());
    HttpPost httpPost = new HttpPost(url);

    try {
        httpPost.setEntity(httpEntity);
        HttpResponse response = httpclient.execute(httpPost);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        response.getEntity().writeTo(byteArrayOutputStream);
        result = byteArrayOutputStream.toString();
    } catch (ClientProtocolException e) {
        throw new Exception();
    } catch (IllegalStateException e) {
        throw new Exception();
    } catch (IOException e) {
        throw new Exception();
    }

    return result;
}

From source file:Main.java

public static String readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/*  ww  w .jav  a2 s.com*/
    outStream.close();
    inStream.close();
    return outStream.toString();
}

From source file:Main.java

public static synchronized String formatXml(String xml) {

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {//from w ww  .  j  av  a 2 s .c o  m
        readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output));
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to format XML", e);
    }
    return output.toString();

}

From source file:com.github.walterfan.util.http.URLHelper.java

public static String URL2String(String strUrl) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {//from w  ww .  ja v a2  s . c o m
        URL2Stream(strUrl, output);
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
    return output.toString();
}

From source file:com.zenoss.zenpacks.zenjmx.call.Utility.java

public static void debugStack(Throwable e) {
    if (!_logger.isDebugEnabled())
        return;/*from w w  w  .j av a2 s  . co  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    e.printStackTrace(ps);
    ps.flush();
    String stackTrace = baos.toString();
    _logger.debug(e.getMessage() + "\n" + stackTrace);
}

From source file:com.screenslicer.webapp.WebUtil.java

public static String applyTemplate(String template, Object dataModel) {
    try {//from  ww  w . j a v a 2s . c om
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(out);
        if (!WebApp.DEV) {
            templateCache.get(template + ".ftl").process(dataModel, writer);
            return out.toString();
        }
        templateConfig.getTemplate(template + ".ftl").process(dataModel, writer);
        return out.toString();
    } catch (Exception e) {
        Log.exception(e);
        return "";
    }
}

From source file:jp.terasoluna.fw.util.ExceptionUtil.java

/**
 * ?????/*from www . j  av  a2s .c o  m*/
 * 
 * <p>
 *  ??????????????
 *  ????????
 *  ???getRootCause()????????ServletException??
 * </p>
 *
 * @param throwable 
 * @return ???
 */
public static String getStackTrace(Throwable throwable) {
    StringBuilder sb = new StringBuilder();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (throwable != null) {
        baos.reset();
        throwable.printStackTrace(new PrintStream(baos));
        sb.append(baos.toString());

        //throwable?Class??
        Class<? extends Throwable> throwableClass = throwable.getClass();

        // ServletException ?? getRootCause ?
        if (SERVLET_EXCEPTION_NAME.equals(throwableClass.getName())) {
            try {
                //throwable = ((ServletException) throwable).getRootCause()
                //Class??????
                Method method = throwableClass.getMethod(GET_ROOT_CAUSE);
                throwable = (Throwable) method.invoke(throwable);
            } catch (NoSuchMethodException e) {
                //???????
                log.error(e.getMessage());
                throwable = null;
            } catch (IllegalAccessException e) {
                //?????????
                log.error(e.getMessage());
                throwable = null;
            } catch (InvocationTargetException e) {
                //?????
                log.error(e.getMessage());
                throwable = null;
            }

        } else {
            throwable = throwable.getCause();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String streamToString(InputStream inputStream) {
    try {/*from  w ww  . java  2  s . c  om*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            out.flush();
        }

        String result = out.toString();
        out.close();
        inputStream.close();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:hudson.plugins.ansicolor.AnsiColorNote.java

/**
 * Process a string, convert ANSI markup to HTML.
 * @param data string that may contain ANSI escape characters
 * @return HTML string//from  ww  w  . j  a v a 2s  .  c om
 * @throws IOException
 */
public static String colorize(String data, final AnsiColorMap colorMap) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    AnsiColorizer colorizer = new AnsiColorizer(out, Charset.defaultCharset(), colorMap);
    byte[] bytes = data.getBytes();
    colorizer.eol(bytes, bytes.length);
    return out.toString();
}