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

public static String readFromFileInput(Context context, String fileName) throws IOException {

    InputStream inputStream = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;//from w  ww  . jav  a  2 s .  c o m
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
}

From source file:Main.java

public static String readFromAssets(Context context, String fileName) throws IOException {

    InputStream inputStream = context.getAssets().open(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;/*from w  w w  .  j  a  v a2 s.co  m*/
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArrayOutputStream.toString();
}

From source file:brainleg.app.util.AppWeb.java

private static String readFrom(InputStream is) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int c;// www .  j a  v  a2 s  .  co m
    while ((c = is.read()) != -1) {
        out.write(c);
    }
    String s = out.toString();
    out.close();
    return s;
}

From source file:au.com.jwatmuff.genericp2p.windows.ExecUtil.java

public static String getStdoutForCommand(String cmd) {
    CommandLine cmdLine = CommandLine.parse(cmd);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWatchdog(new ExecuteWatchdog(60000));
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(stdout, null));
    try {// w w w .ja v a2  s.  c om
        executor.execute(cmdLine);
    } catch (ExecuteException e) {
        log.error("Exception executing '" + cmd + "'", e);
    } catch (IOException e) {
        log.error("IOException executing '" + cmd + "'", e);
    }

    return stdout.toString();
}

From source file:com.example.CrmTest.java

private static String getStringFromInputStream(InputStream in) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int c = 0;/*w w  w  . j av  a  2  s . c o m*/
    while ((c = in.read()) != -1) {
        bos.write(c);
    }
    in.close();
    bos.close();
    return bos.toString();
}

From source file:net.panthema.BispanningGame.GraphString.java

static int readInt(PushbackReader pr) throws IOException {
    int c;/*from   w  w w.ja  v a2s.  c om*/
    ByteArrayOutputStream ba = new ByteArrayOutputStream();
    while ((c = pr.read()) != 0 && (Character.isDigit(c) || c == '-')) {
        ba.write(c);
    }
    pr.unread(c);
    try {
        return Integer.parseInt(ba.toString());
    } catch (NumberFormatException e) {
        throw (new IOException("Error in Graph String: integer format error"));
    }
}

From source file:Main.java

public static String tryFormattingString(String s) {
    try {//  ww w. j a v a 2s.  co  m
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

            @Override
            public void warning(SAXParseException exception) throws SAXException {
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
            }

            @Override
            public void error(SAXParseException exception) throws SAXException {
            }
        });
        Document doc = builder.parse(in);
        TransformerFactory tf = TransformerFactory.newInstance();
        // tf.setAttribute("indent-number", new Integer(2));
        Transformer trans = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(out);
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        trans.transform(source, result);
        return out.toString();

    } catch (Exception e) {
        // Console.println("Could not validate the soapmessage, although I'll show it anyway..");
    }
    return s;
}

From source file:$.CrmTest.java

private static String getStringFromInputStream(InputStream in) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int c = 0;
        while ((c = in.read()) != -1) {
            bos.write(c);/*from  w  w  w  . j a  v  a  2s.c o m*/
        }
        in.close();
        bos.close();
        return bos.toString();
    }

From source file:software.uncharted.util.HTTPUtil.java

public static String post(String urlToRead, String postData) {
    URL url;/*from ww  w . j a va 2 s . co m*/
    HttpURLConnection conn;
    try {
        url = new URL(urlToRead);
        conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");

        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postData);
        wr.flush();
        wr.close();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        InputStream is = conn.getInputStream();
        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Failed to read URL: " + urlToRead + " <" + e.getMessage() + ">");
    }
    return null;
}

From source file:at.uni_salzburg.cs.ckgroup.cscpp.utils.HttpQueryUtils.java

public static String simpleQuery(String url) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/*  www  . ja  v  a 2 s  . c  om*/

    response = httpclient.execute(httpget);
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
            bo.write(tmp, 0, l);
        }
    }

    return bo.toString();
}