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 InputStream2String(final InputStream is) {
    try {//from www .ja v  a2  s  .  com
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:fi.vm.sade.log.client.LoggerJms.java

/**
 * Encode messages to string (xml)./*from   www .  j a  va 2 s . c om*/
 *
 * @param event
 * @return
 */
public static String encode(LogEvent event) {
    if (event == null) {
        return null;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder xmlEncoder = new XMLEncoder(baos);
    xmlEncoder.writeObject(event);
    xmlEncoder.close();

    return baos.toString();
}

From source file:au.gov.ansto.bragg.nbi.restlet.fileupload.util.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 inputStream The input stream to read.
 * @see #asString(InputStream, String)//from  w w  w .  j  ava  2  s. c  om
 * @return The streams contents, as a string.
 * @throws IOException An I/O error occurred.
 */
public static String asString(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    copy(inputStream, baos, true);
    return baos.toString();
}

From source file:hudson.slaves.ComputerLauncherTest.java

private static void assertChecked(String text, String spec) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ComputerLauncher.checkJavaVersion(new PrintStream(os), "bin/java",
            new BufferedReader(new StringReader(text)));
    String logged = os.toString();
    assertTrue(logged, logged.contains(Messages.ComputerLauncher_JavaVersionResult("bin/java", spec)));
}

From source file:StreamsUtils.java

public static String readString(InputStream stream) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        b.write(buf, 0, readedBytes);//from  w ww . ja  va  2 s.c om
    }
    b.close();
    return b.toString();
}

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

public static String base64UrlEncode(byte[] data) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from   w w  w  . ja v  a  2 s  .co m*/
        new FileSystemSafeBase64UrlEncoder().encode(data, 0, data.length, out);
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException("Exception when base64url encoding data", e);
    }
}

From source file:Main.java

public static String getStringFromInput(InputStream is) throws IOException {
    ByteArrayOutputStream byteOus = new ByteArrayOutputStream();
    byte[] tmp = new byte[1024];
    int size = 0;
    while ((size = is.read(tmp)) != -1) {
        byteOus.write(tmp, 0, size);//from  w w  w  . j a va 2 s  .c o  m
    }
    byteOus.flush();
    is.close();
    return byteOus.toString();
}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static String serializePublicationIdentifier(PublicationIdentifier identifier) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);

    so.writeObject(identifier);/*  ww w . j a  va2s.c  o m*/
    so.flush();

    String serialization = bo.toString();

    so.close();
    bo.close();

    return serialization;
}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static String serializeAuthorIdentifier(AuthorIdentifier identifier) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);

    so.writeObject(identifier);//from w w  w .  j  av a2s.  c om
    so.flush();

    String serialization = bo.toString();

    so.close();
    bo.close();

    return serialization;
}

From source file:Main.java

public static void writeDocument(Document doc, Writer out) throws IOException {
    // happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer 
    // approach to work. 
    //    OutputFormat format = new OutputFormat(doc);
    //    format.setIndenting(true);
    //    format.setIndent(2);
    //    XMLSerializer serializer = new XMLSerializer(out, format);
    //    serializer.serialize(doc);

    DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation();
    LSSerializer writer = impl.createLSSerializer();
    DOMConfiguration config = writer.getDomConfig();

    if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
        config.setParameter("format-pretty-print", Boolean.TRUE);
    }/*from  w ww.  ja va  2s  .c o m*/

    // what a crappy way to force the stream to be UTF-8.  yuck!
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    LSOutput output = impl.createLSOutput();
    output.setEncoding("UTF-8");
    output.setByteStream(baos);

    writer.write(doc, output);

    out.write(baos.toString());
    out.flush();
}