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

@Deprecated
public synchronized String toString(int hibyte) 

Source Link

Document

Creates a newly allocated string.

Usage

From source file:Main.java

public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }/*from www.  j  a  v a2s.co m*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    return out.toString("ISO-8859-1");
}

From source file:Main.java

private static String getStringFromException(Throwable e) {
    String result = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps);//from w  ww. j a  va 2 s  .  co  m
    try {
        result = bos.toString("UTF-8");
    } catch (UnsupportedEncodingException e1) {
        // won't happen
    }
    return result;
}

From source file:com.github.robozonky.integrations.zonkoid.Util.java

public static String readEntity(final HttpEntity entity) {
    if (entity == null) {
        LOGGER.debug("Zonkoid sent an empty response.");
        return null;
    }/*from   w  w  w. j  a  va 2 s.co m*/
    final ByteArrayOutputStream outstream = new ByteArrayOutputStream(); // no need to close this one
    try {
        entity.writeTo(outstream);
        return outstream.toString(Defaults.CHARSET.displayName());
    } catch (final IOException ex) {
        LOGGER.debug("Failed reading Zonkoid response.", ex);
        return null;
    }
}

From source file:Streams.java

/**
 * This convenience method allows to read a
 * {@link org.apache.commons.fileupload.FileItemStream}'s
 * content into a string, using the given character encoding.
 * @param pStream The input stream to read.
 * @param pEncoding The character encoding, typically "UTF-8".
 * @see #asString(InputStream)/*  www .  j  av a  2s.  c  o  m*/
 * @return The streams contents, as a string.
 * @throws IOException An I/O error occurred.
 */
public static String asString(InputStream pStream, String pEncoding) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    copy(pStream, baos, true);
    return baos.toString(pEncoding);
}

From source file:Main.java

public static String transform(Source xsltStream, Source xmlStream, String charset)
        throws TransformerException {
    try {/*from  w w w. java 2 s . c om*/

        Transformer transformer = factory.newTransformer(xsltStream);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        transformer.transform(xmlStream, new StreamResult(outputStream));
        transformer = null;
        xmlStream = null;
        return outputStream.toString(charset);
    } catch (Exception e) {
        throw new TransformerException(e);
    }
}

From source file:com.quackware.handsfreemusic.YouTubeUtility.java

public static String calculateYouTubeUrl(String youtubeVideoId)
        throws IOException, ClientProtocolException, UnsupportedEncodingException {
    HttpClient client = new DefaultHttpClient();
    HttpGet getMethod = new HttpGet(YOUTUBE_VIDEO_INFORMATION_URL + youtubeVideoId);
    HttpResponse response = null;/* w w  w. j av a 2s .co  m*/

    response = client.execute(getMethod);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    String infoStr = null;
    response.getEntity().writeTo(bos);
    infoStr = new String(bos.toString("UTF-8"));

    String[] args = infoStr.split("&");
    Map<String, String> argMap = new HashMap<String, String>();
    for (int i = 0; i < args.length; i++) {
        String[] argValStrArr = args[i].split("=");
        if (argValStrArr != null) {
            if (argValStrArr.length >= 2) {
                argMap.put(argValStrArr[0], URLDecoder.decode(argValStrArr[1]));
            }
        }
    }
    String tokenStr = null;
    try {
        tokenStr = URLDecoder.decode(argMap.get("token"));
    } catch (Exception ex) {
        return null;
    }
    String uriStr = "http://www.youtube.com/get_video?video_id=" + youtubeVideoId + "&t="
            + URLEncoder.encode(tokenStr) + "&fmt=18";
    return uriStr;
}

From source file:com.seleniumtests.util.imaging.ImageProcessor.java

public static String toBase64(BufferedImage img) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    OutputStream b64 = new Base64.OutputStream(os);
    ImageIO.write(img, "png", b64);
    return os.toString("UTF-8");
}

From source file:framework.json2java.Example.java

private static void output(JCodeModel codeModel) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CodeWriter codeWriter = new SingleStreamCodeWriter(outputStream);
    codeModel.build(codeWriter);/* w w w  . j a  v a2  s .  co  m*/
    codeWriter.close();
    outputStream.close();
    String string = outputStream.toString("utf-8");
    System.out.println(string);
}

From source file:eu.modaclouds.sla.mediator.Utils.java

public static <E> String toString(E e) throws JAXBException {
    String charsetName = "utf-8";
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    print(e, os);//w  w  w.j  av a2 s  .  c o  m
    try {
        return os.toString(charsetName);
    } catch (UnsupportedEncodingException e1) {
        throw new IllegalArgumentException(charsetName + " is not supported");
    }
}

From source file:com.kolich.aws.services.sqs.impl.KolichSQSSigner.java

private static final String getSQSCanonicalQuery(final List<SortableBasicNameValuePair> params) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*w  w w.j  a v a2 s.  co m*/
        new UrlEncodedFormEntity(params, UTF_8).writeTo(os);
        return os.toString(UTF_8).replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
    } catch (Exception e) {
        throw new KolichAwsException("Failed to get SQS canonical " + "query string.", e);
    }
}