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

/**
 * //from   ww  w .j av a 2s  .c  o  m
 * @param in
 * @return
 * @throws IOException
 */
public static String readFormStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

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

    String result = out.toString();
    in.close();
    out.close();

    return result;
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL/*from   w  ww.  j av a  2  s .  c om*/
 * 
 * @param doc
 *            The document to read
 * @param iS_xsl
 *            An InputStream with the XSL which allows transformate XML into
 *            String
 * 
 * @return An String with the DOM transformated
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String_With_An_XSL_InputStream(Document doc, InputStream iS_xsl)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory.newTransformer(new StreamSource(iS_xsl));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL/*w  ww .j av a  2  s.c o  m*/
 * 
 * @param doc
 *            The document to read
 * @param xsl
 *            An string with the XSL which allows transformate XML into
 *            String
 * 
 * @return An String with the DOM transformated
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String_With_An_XSL_String(Document doc, String xsl)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory
            .newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

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

public static String obtenerIPServidorRemoto() {
    String ip = null;//from ww w.j  a  v a 2 s.c  o  m
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(REMOTE_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);
        JSONObject json = new JSONObject(baos.toString());
        baos.close();
        entrada.close();
        con.disconnect();
        ip = json.getString("content");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP servidor remoto: " + ip);
    return ip;
}

From source file:ar.com.zauber.commons.xmpp.message.XMPPMessageAttributes.java

/** lee un resource en un string */
private static String read(final Resource resource) {
    Validate.notNull(resource);/*from  w  w  w . jav  a 2 s . c om*/
    final InputStream is = resource.getInputStream();
    final ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        IOUtils.copy(is, os);
        return os.toString();
    } catch (IOException e) {
        throw new UnhandledException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

private static String inputStream2String(InputStream is) {
    try {//from ww w  .  j  a v  a  2s.co m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] array = new byte[1024];
        int len;
        while ((len = is.read(array, 0, array.length)) != -1) {
            baos.write(array, 0, len);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readFromStream(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;/*  w  w w  . j  av  a2 s.  c o  m*/
    while ((len = is.read(buff)) != -1) {
        bos.write(buff, 0, len);
    }
    is.close();
    String result = bos.toString();
    bos.close();
    return result;
}

From source file:Main.java

/**
 * unzip some gzip compressed data//  www  .  j a v a2  s  .  com
 * @param bytes the data to uncompress
 * @return the uncompressed data
 */
public static String gzipDecompress(byte[] bytes) {
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream gs = new GZIPInputStream(stream);
        BufferedInputStream bufis = new BufferedInputStream(gs);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0) {
            bos.write(buf, 0, len);
        }
        return bos.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static String xmlToString(Document xml) throws TransformerConfigurationException, TransformerException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Transformer transformer = getTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(new DOMSource(xml), new StreamResult(bos));
    return bos.toString();
}

From source file:net.ripe.rpki.commons.crypto.util.KeyPairUtil.java

static String hexEncodeHashData(byte[] keyHashData) {
    HexEncoder hexEncoder = new HexEncoder();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*w w  w. j  a  va 2s .c om*/
        hexEncoder.encode(keyHashData, 0, keyHashData.length, out);
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException("Exception hex encoding data", e);
    }
}