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

public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(bs);/*from   w w w.  j a  va2s. com*/

    // converts buffers content using Cp1047 character set
    String str = baos.toString("Cp1047");
    System.out.println(str);

    // converts buffers contents using UTF-8 character set
    str = baos.toString("UTF-8");
    System.out.println(str);

}

From source file:de.serverfrog.pw.crypt.SerpentUtil.java

/**
 * Print all Security providers and their algos
 *
 * @param args args//from w  w  w.  j a  v  a  2  s  .c om
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    SecretKeySpec keySpec = new SecretKeySpec("test".getBytes("UTF-8"), "AES");
    try (CipherOutputStream outputStream = SerpentUtil.getOutputStream(byteArrayOutputStream, keySpec)) {
        IOUtils.write("TEST", outputStream);
    }
    System.out.println(Arrays.toString(byteArrayOutputStream.toByteArray()));
    System.out.println(byteArrayOutputStream.toString("UTF-8"));
    byte[] toByteArray = byteArrayOutputStream.toByteArray();
    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(toByteArray);
    CipherInputStream inputStream = SerpentUtil.getInputStream(arrayInputStream,
            new SecretKeySpec("test".getBytes("UTF-8"), "AES"));
    byte[] bytes = new byte[1048576];

    IOUtils.read(inputStream, bytes);

    System.out.println(new String(bytes, "UTF-8").trim());

    //
    //        for (Provider provider : Security.getProviders()) {
    //            System.out.println("");
    //            System.out.println("");
    //            System.out.println("");
    //            System.out.println("-------------------------------");
    //            System.out.println("Name: " + provider.getName());
    //            System.out.println("Info: " + provider.getInfo());
    //            for (Map.Entry<Object, Object> entry : provider.entrySet()) {
    //                System.out.println("Key: Class:" + entry.getKey().getClass() + " String: " + entry.getKey());
    //                System.out.println("Value: Class:" + entry.getValue().getClass() + " String: " + entry.getValue());
    //            }
    //            for (Provider.Service service : provider.getServices()) {
    //                System.out.println("Service: Algorithm:" + service.getAlgorithm()
    //                        + " ClassName:" + service.getClassName()
    //                        + " Type:" + service.getType() + " toString:" + service.toString());
    //            }
    //            for (Object object : provider.values()) {
    //                System.out.println("Value: " + object.getClass() + " toString:" + object.toString());
    //
    //            }
    //            System.out.println("-------------------------------");
    //        }
}

From source file:eu.planets_project.tb.gui.backing.exp.ExpTypeMigrate.java

public static void main(String args[]) {

    Properties p = System.getProperties();

    ByteArrayOutputStream byos = new ByteArrayOutputStream();
    try {/*  w  w  w .j a  v  a 2s .c  om*/
        p.storeToXML(byos, "Automatically generated for PLANETS Service ", "UTF-8");
        String res = byos.toString("UTF-8");
        System.out.println(res);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // This.
    List<String> pl = new ArrayList<String>();
    for (Object key : p.keySet()) {
        pl.add((String) key);
    }
    Collections.sort(pl);

    //
    for (String key : pl) {
        System.out.println(key + " = " + p.getProperty(key));
    }

    /*
     * http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html#getCurrentThreadCpuTime()
     * 
     * http://www.java-forums.org/new-java/5303-how-determine-cpu-usage-using-java.html
     * 
     */

    ThreadMXBean TMB = ManagementFactory.getThreadMXBean();
    int mscale = 1000000;
    long time = 0, time2 = 0;
    long cput = 0, cput2 = 0;
    double cpuperc = -1;

    //Begin loop.
    for (int i = 0; i < 10; i++) {

        if (TMB.isThreadCpuTimeSupported()) {
            if (!TMB.isThreadCpuTimeEnabled()) {
                TMB.setThreadCpuTimeEnabled(true);
            }

            //                if(new Date().getTime() * mscale - time > 1000000000) //Reset once per second
            //                {
            System.out.println("Resetting...");
            time = System.currentTimeMillis() * mscale;
            cput = TMB.getCurrentThreadCpuTime();
            //                cput = TMB.getCurrentThreadUserTime();
            //                }

        }

        //Do cpu intensive stuff
        for (int k = 0; k < 10; k++) {
            for (int j = 0; j < 100000; j++) {
                double a = Math.pow(i, j);
                double b = a / j + Math.random();
                a = b * Math.random();
                b = a * Math.random();
            }

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        if (TMB.isThreadCpuTimeSupported()) {
            //                if(new Date().getTime() * mscale - time != 0) {
            cput2 = TMB.getCurrentThreadCpuTime();
            System.out.println("cpu: " + (cput2 - cput) / (1000.0 * mscale));
            //                cput2 = TMB.getCurrentThreadUserTime();

            time2 = System.currentTimeMillis() * mscale;
            System.out.println("time: " + (time2 - time) / (1000.0 * mscale));

            cpuperc = 100.0 * (cput2 - cput) / (double) (time2 - time);
            System.out.println("cpu perc = " + cpuperc);
            //                }
        }
        //End Loop
    }
    System.out.println("Done.");
}

From source file:Main.java

private static String asString(final PrintStream ps, ByteArrayOutputStream os)
        throws UnsupportedEncodingException {
    if (ps != null) {
        ps.flush();/*w w w  . j  a v a2 s  .c  o m*/
        return os.toString("UTF-8");
    } else {
        return "";
    }
}

From source file:Main.java

public static String inputStream2String(InputStream in) throws IOException {
    byte[] buf = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    for (int i; (i = in.read(buf)) != -1;) {
        baos.write(buf, 0, i);//from  w  w  w . j  ava2s . com
    }

    return baos.toString("UTF-8");
}

From source file:se.vgregion.pubsub.push.impl.HttpUtil.java

public static String readContent(HttpEntity entity) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    entity.writeTo(out);/* w w  w . j a  v a2 s  . c o m*/

    return out.toString("UTF-8");
}

From source file:Main.java

private static String getStringFromException(Throwable e) {
    String result = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps);/*from w ww . ja  v  a 2s. c  o m*/
    try {
        result = bos.toString("utf-8");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String elementToString(final Node node) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final Document document = factory.newDocumentBuilder().newDocument();
    final Node importedNode = document.importNode(node, true);
    document.appendChild(importedNode);//from   w ww. j  av a 2 s .c o  m
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(bos));
    return bos.toString("UTF-8");
}

From source file:Main.java

public static String getDocumentAsString(Node node, boolean prettyXml)
        throws TransformerException, UnsupportedEncodingException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();

    if (prettyXml) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }//from www.  ja  v a2  s. c  om
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(node), new StreamResult(baos));

    return baos.toString("UTF-8");
}

From source file:Main.java

/**
 * Marshal a jaxb object to obtain the xml format as a string in specific encoding.
 * // w ww .  j a v  a  2 s.  c om
 * @param jabxbObject the jaxb object
 * @param jaxbContextName the contect name where to look to find jaxb classes
 * @param cl : {@link ClassLoader} of the ObjectFactory
 * @return a string representing the jaxb object
 * @throws JAXBException
 * @throws UnsupportedEncodingException
 */
public static String marshal(Object jabxbObject, String jaxbContextName, ClassLoader cl, String encoding)
        throws JAXBException, UnsupportedEncodingException {
    Marshaller marshaller = createMarshaller(jaxbContextName, cl);

    final ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
    marshaller.marshal(jabxbObject, xmlStream);

    return xmlStream.toString(encoding);
}