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

@Deprecated
public synchronized String toString(int hibyte) 

Source Link

Document

Creates a newly allocated string.

Usage

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, using the given character encoding.
 *
 * @param inputStream The input stream to read.
 * @param encoding The character encoding, typically "UTF-8".
 * @see #asString(InputStream)//from  w  ww  .  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 inputStream, String encoding) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    copy(inputStream, baos, true);
    return baos.toString(encoding);
}

From source file:Main.java

public static String readFully(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = is.read(buffer)) != -1) {
        baos.write(buffer, 0, length);/*from ww w .  jav a 2s .  co  m*/
    }
    return baos.toString("UTF-8");
}

From source file:Main.java

public static String readInputStreamAsString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[4096];
    int lenRead;/*from   ww w  .ja  v  a 2 s . com*/
    while ((lenRead = is.read(bytes)) != -1) {
        if (lenRead > 0)
            baos.write(bytes, 0, lenRead);
    }

    if (baos.size() > 0)
        return baos.toString("utf-8");
    return null;
}

From source file:Main.java

public static String document2XmlString(Document xmldoc) {
    try {/*from  w  w  w  .j  a  va  2 s . c  o m*/
        Source src = new DOMSource(xmldoc);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); // identity transformer
        transformer.transform(src, new StreamResult(bout));
        return bout.toString("UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.smartsheet.api.internal.util.StreamUtil.java

/**
 * generate a String of UTF-8 characters (or hex-digits if byteStream isn't UTF-8 chars) from byteStream,
 * truncating to maxLen (with "..." added if the result is truncated)
 * @param byteStream the source of bytes to be converted to a UTF-8 String
 * @param maxLen     the point at which to truncate the string (-1 means don't truncate) in which case "..." is appended
 * @return the String read from the stream
 *///w w  w. jav a2  s.  co m
public static String toUtf8StringOrHex(ByteArrayOutputStream byteStream, int maxLen) {
    if (maxLen == -1) {
        maxLen = Integer.MAX_VALUE;
    }

    String result;
    try {
        result = byteStream.toString("UTF-8");
    } catch (Exception notUtf8) {
        result = Hex.encodeHexString(byteStream.toByteArray());
    }

    final int resultLen = result != null ? result.length() : 0;
    final String suffix = resultLen > maxLen ? "..." : "";
    return resultLen == 0 ? "" : result.substring(0, Math.min(resultLen, maxLen)) + suffix;
}

From source file:Main.java

/**
 * Prints a Node tree recursively./*from   w w  w.j  av  a 2 s . c o m*/
 * @param node A DOM tree Node
 * @param encoding character encoding
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node, String encoding) {
    if (node == null) {
        return null;
    }

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

From source file:com.meltmedia.dropwizard.crypto.Mocks.java

public static Callable<String> mockOutput(Namespace namespace) throws UnsupportedEncodingException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    when(namespace.get(Commands.OUTFILE)).thenReturn(new PrintStream(out, true, "UTF-8"));
    return new Callable<String>() {
        @Override/*from   w  ww  .jav a  2s . c o  m*/
        public String call() throws Exception {
            return out.toString("UTF-8");
        }
    };
}

From source file:com.azaptree.services.json.JsonUtils.java

public static String serialize(final Object obj) {
    Assert.notNull(obj, "obj is required");
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
    try {//w w  w  .  j a v a  2 s  .  c o m
        JsonUtils.objectMapper.writeValue(bos, obj);
        return bos.toString("UTF-8");
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.kixeye.chassis.scala.transport.serde.ScalaCaseClassTest.java

private static void dumpToLog(MessageSerDe serDe, byte[] data) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    HexDump.dump(data, 0, baos, 0);//  w w  w .java 2  s  . c o m

    logger.info("Serialized object using [{}] to: \n{}", serDe.getMessageFormatName(),
            baos.toString(Charsets.UTF_8.name()).trim());
}

From source file:com.azaptree.services.json.JsonUtils.java

public static String serializeNormalized(final Object obj) {
    Assert.notNull(obj, "obj is required");
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
    try {//from w ww  .j  a  va 2s. co  m
        JsonUtils.normalizingObjectMapper.writeValue(bos, obj);
        return bos.toString("UTF-8");
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}