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:StringUtil.java

/**
 * Get the stack trace of the supplied exception.
 * /*from  w  w  w  .j  a v a  2s  . co  m*/
 * @param throwable the exception for which the stack trace is to be returned
 * @return the stack trace, or null if the supplied exception is null
 */
public static String getStackTrace(Throwable throwable) {
    if (throwable == null)
        return null;
    final ByteArrayOutputStream bas = new ByteArrayOutputStream();
    final PrintWriter pw = new PrintWriter(bas);
    throwable.printStackTrace(pw);
    pw.close();
    return bas.toString();
}

From source file:StreamUtility.java

/**
 * Read an entire stream and return it as a String
 *
 * @param sourceStream the InputStream to read.
 * @return a String containing the contents of the stream.
 * @exception IOException from java.io calls.
 *///  ww w  . j  a v a2 s .  c  o m
public static String readStreamAsString(InputStream sourceStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(sourceStream));
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    copyStream(sourceStream, output);

    return output.toString();
}

From source file:org.radiognu.radiognu.utils.utilsradiognu.java

public static final String getResponseServiceAPI(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;/*  w  w w.  ja  v  a 2  s . c  o m*/
    String responseString = null;
    try {
        // make a HTTP request
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            // request successful - read the response and close the connection
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else {
            // request failed - close the connection
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (Exception e) {
    }
    return responseString;
}

From source file:Main.java

public static String ConvertToString(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;//from ww  w.j a v  a2s  .c o m
    try {
        while ((i = is.read()) != -1) {
            baos.write(i);

        }
        return baos.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return "";
}

From source file:Main.java

public static String readFromStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;/*from   w ww . j ava 2s .  com*/
    byte[] buf = new byte[1024];
    if ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    String result = out.toString();
    in.close();
    out.close();
    return result;
}

From source file:com.Kuesty.services.JSONRequest.java

public static void execute(String uri, JSONRequestTaskHandler rsh) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;/*from   www  .  ja v  a  2s  .c o  m*/
    String responseString = null;

    try {
        response = httpclient.execute(new HttpGet(uri));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();

        } else {
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        rsh.onError(e.getMessage());
    } catch (IOException e) {
        rsh.onError(e.getMessage());
    }
    try {
        JSONObject response1 = new JSONObject(responseString);
        rsh.onSuccess(response1);

    } catch (JSONException e) {
        rsh.onError(e.getMessage());
    }
}

From source file:Streams.java

/**
 * This convenience method allows to read a
 * {@link org.apache.commons.fileupload.FileItemStream}'s
 * content into a string. The platform's default character encoding
 * is used for converting bytes into characters.
 * @param pStream The input stream to read.
 * @see #asString(InputStream, String)/*from  w w  w.  j  a  v a2 s  .c o  m*/
 * @return The streams contents, as a string.
 * @throws IOException An I/O error occurred.
 */
public static String asString(InputStream pStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    copy(pStream, baos, true);
    return baos.toString();
}

From source file:com.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPExterna() {
    String ip = null;/*w  w  w.  j  a v  a 2s .c  om*/
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(GET_EXTERNAL_HOST).openConnection();
        DataInputStream entrada = new DataInputStream(con.getInputStream());
        int length;
        byte[] chunk = new byte[64];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = entrada.read(chunk)) != -1)
            baos.write(chunk, 0, length);
        ip = baos.toString();
        baos.close();
        entrada.close();
        con.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP exterior: " + ip);
    return ip;
}

From source file:com.sec.ose.osi.util.tools.Tools.java

public static String getPrintStackTraceInfoString(Exception e) {
    String returnStackTraceString = e.toString();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(out);
    e.printStackTrace(printStream);/*  w w w  .j  a  v  a  2 s .  com*/
    returnStackTraceString = out.toString();
    return returnStackTraceString;
}

From source file:Main.java

/**
 * Print a Node tree recursively./* w  w  w .  j  a  va 2 s.co m*/
 * 
 * @param node
 *            A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");
        DOMSource source = new DOMSource(node);
        ByteArrayOutputStream os = new ByteArrayOutputStream(2000);
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
        return os.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}