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:info.guardianproject.net.http.HttpManager.java

public static String doGet(String serviceEndpoint, Properties props) throws Exception {

    HttpClient httpClient = new SocksHttpClient();

    StringBuilder uriBuilder = new StringBuilder(serviceEndpoint);

    StringBuffer sbResponse = new StringBuffer();

    Enumeration<Object> enumProps = props.keys();
    String key, value = null;/*  w ww  . ja  v  a  2  s  . c o m*/

    uriBuilder.append('?');

    while (enumProps.hasMoreElements()) {
        key = (String) enumProps.nextElement();
        value = (String) props.get(key);
        uriBuilder.append(key);
        uriBuilder.append('=');
        uriBuilder.append(java.net.URLEncoder.encode(value));
        uriBuilder.append('&');

    }

    HttpGet request = new HttpGet(uriBuilder.toString());
    HttpResponse response = httpClient.execute(request);

    int status = response.getStatusLine().getStatusCode();

    // we assume that the response body contains the error message
    if (status != HttpStatus.SC_OK) {
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        response.getEntity().writeTo(ostream);
        Log.e("HTTP CLIENT", ostream.toString());

        return null;

    } else {
        InputStream content = response.getEntity().getContent();
        // <consume response>

        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String line;

        while ((line = reader.readLine()) != null)
            sbResponse.append(line);

        content.close(); // this will also close the connection
    }

    return sbResponse.toString();

}

From source file:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java

private static String readResponse(HttpResponse response) throws IOException {
    InputStream instream = response.getEntity().getContent();
    Header contentEncoding = response.getFirstHeader("Content-Encoding");
    if (Config.LOGV) //NOPMD
        if (contentEncoding != null) //NOPMD
            Log.v(TAG, "Response content encoding was '" + contentEncoding.getValue() + "'");
    if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        if (Config.LOGD)
            Log.d(TAG, "Handling GZIP response.");
        instream = new GZIPInputStream(instream);
    }/* ww  w.j  av  a  2 s.  c o m*/

    BufferedInputStream bis = new BufferedInputStream(instream);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int read = 0;
    while ((read = bis.read(buf)) > 0)
        baos.write(buf, 0, read);
    String body = baos.toString();
    if (Config.LOGV)
        Log.v(TAG, "Response:  " + body);
    return body;
}

From source file:com.openideals.android.net.HttpManager.java

public static String doGet(String serviceEndpoint, Properties props) throws Exception {

    HttpClient httpClient = new DefaultHttpClient();

    StringBuilder uriBuilder = new StringBuilder(serviceEndpoint);

    StringBuffer sbResponse = new StringBuffer();

    Enumeration<Object> enumProps = props.keys();
    String key, value = null;/*from  ww  w  .jav  a 2s. c  o  m*/

    uriBuilder.append('?');

    while (enumProps.hasMoreElements()) {
        key = (String) enumProps.nextElement();
        value = (String) props.get(key);
        uriBuilder.append(key);
        uriBuilder.append('=');
        uriBuilder.append(java.net.URLEncoder.encode(value));
        uriBuilder.append('&');

    }

    HttpGet request = new HttpGet(uriBuilder.toString());
    HttpResponse response = httpClient.execute(request);

    int status = response.getStatusLine().getStatusCode();

    // we assume that the response body contains the error message
    if (status != HttpStatus.SC_OK) {
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        response.getEntity().writeTo(ostream);
        Log.e("HTTP CLIENT", ostream.toString());

        return null;

    } else {
        InputStream content = response.getEntity().getContent();
        // <consume response>

        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String line;

        while ((line = reader.readLine()) != null)
            sbResponse.append(line);

        content.close(); // this will also close the connection
    }

    return sbResponse.toString();

}

From source file:Main.java

public static String readInStream(InputStream inStream) {
    try {//from  w  w w.  ja v a  2 s.c om
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }

        outStream.close();
        inStream.close();
        return outStream.toString();
    } catch (IOException e) {
        Log.i("FileTest", e.getMessage());
    }
    return null;
}

From source file:net.sourceforge.jweb.maven.mojo.InWarMinifyMojo.java

public static String buildStack(Throwable t) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(arrayOutputStream);
    t.printStackTrace(printStream);/*from  www.  ja  v a2 s.c o m*/
    String ret = arrayOutputStream.toString();
    try {
        arrayOutputStream.close();
        printStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:com.opengamma.analytics.example.coupledfokkerplank.CoupledFokkerPlankExample.java

public static void runCoupledFokkerPlank(PrintStream out) throws FileNotFoundException, IOException {
    final ExtendedCoupledFiniteDifference solver = new ExtendedCoupledFiniteDifference(0.5);
    final int tNodes = 50;
    final int xNodes = 150;

    final MeshingFunction timeMesh = new ExponentialMeshing(0, T, tNodes, 5.0);
    final MeshingFunction spaceMesh = new HyperbolicMeshing(LOWER.getLevel(), UPPER.getLevel(), SPOT, xNodes,
            0.01);//from w  w w  . j a va2 s .c o  m

    final double[] timeGrid = new double[tNodes];
    for (int n = 0; n < tNodes; n++) {
        timeGrid[n] = timeMesh.evaluate(n);
    }

    final double[] spaceGrid = new double[xNodes];
    for (int i = 0; i < xNodes; i++) {
        spaceGrid[i] = spaceMesh.evaluate(i);
    }
    final PDEGrid1D grid = new PDEGrid1D(timeGrid, spaceGrid);

    final PDEResults1D[] res = solver.solve(DATA1, DATA2, grid, LOWER, UPPER, LOWER, UPPER, null);
    final PDEFullResults1D res1 = (PDEFullResults1D) res[0];
    final PDEFullResults1D res2 = (PDEFullResults1D) res[1];

    // output in JSON format without using a JSON library to save dependencies
    StrBuilder buf = new StrBuilder(2048).append('{');

    ByteArrayOutputStream state_1_stream = new ByteArrayOutputStream();
    PrintStream state_1_out = new PrintStream(state_1_stream, true);
    PDEUtilityTools.printSurface("State 1 density", res1, state_1_out);
    state_1_out.close();
    buf.append("\"state_1_data\":\"").append(state_1_stream.toString()).append("\",");

    ByteArrayOutputStream state_2_stream = new ByteArrayOutputStream();
    PrintStream state_2_out = new PrintStream(state_2_stream, true);
    PDEUtilityTools.printSurface("State 2 density", res2, state_2_out);
    state_2_out.close();
    buf.append("\"state_2_data\":\"").append(state_2_stream.toString()).append("\"}");

    buf.replaceAll("\t", "\\t").replaceAll("\r\n", "\\r\\n").replaceAll("\n", "\\n");

    out.print(buf.toString());
}

From source file:com.dbmojo.Util.java

/** GZIP encode a String */
public static String gzipString(String inStr) throws Exception {
    byte[] strBytes = inStr.getBytes();
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);

    try {/*from   www  .  j  a  v  a  2 s .  co  m*/
        gout.write(strBytes, 0, strBytes.length);
    } finally {
        gout.close();
    }
    return bout.toString();
}

From source file:com.github.jrialland.ajpclient.servlet.TestServletProxy.java

private static String slurp(final InputStream is) throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final byte[] buff = new byte[4096];
    int c = 0;//w  w  w  .jav a 2 s . com
    while ((c = is.read(buff)) > -1) {
        baos.write(buff, 0, c);
    }
    return baos.toString();
}

From source file:Main.java

/**
 * returns an XML string./*from   w  w  w. j a  v a  2s .  com*/
 *
 * @param pNode         Node XML Document node
 * @return              String XML string
 */
public static String getXML(Node pNode) {
    String retString = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(pNode), new StreamResult(out));
        retString = out.toString();
        out.close();
    } catch (Exception ex) {
    }
    return retString;
}

From source file:XMLUtils.java

public static String toString(Node node) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writeTo(node, out);//from   w  w  w .ja  v  a 2s.c o  m
    return out.toString();
}