Example usage for java.io ByteArrayInputStream ByteArrayInputStream

List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream ByteArrayInputStream.

Prototype

public ByteArrayInputStream(byte buf[]) 

Source Link

Document

Creates a ByteArrayInputStream so that it uses buf as its buffer array.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String[] a = new String[] { "a", "b", "c" };

    Vector v = new Vector(Arrays.asList());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);// w  w  w  . ja v a  2  s  .co m
    oos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Vector v2 = (Vector) ois.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:SaveVector.java

public static void main(String args[]) throws Exception {
    String data[] = { "Java", "Source", "and", "Support", "." };
    Vector v = new Vector(Arrays.asList(data));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(v);/*from   w  w  w  .java  2s.c o m*/
    o.close();
    ByteArrayInputStream bb = new ByteArrayInputStream(b.toByteArray());
    ObjectInputStream oo = new ObjectInputStream(bb);
    Vector v2 = (Vector) oo.readObject();
    Enumeration e = v.elements();
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    MyBean sc = new MyBean("Test1", "Test2");
    System.out.println("Before:\n" + sc);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(buf);
    o.writeObject(sc);//from  w ww  .j av  a2 s .co m

    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
    MyBean sc2 = (MyBean) in.readObject();
    System.out.println("After:\n" + sc2);
}

From source file:Main.java

public static void main(String[] arg) throws Throwable {
    File f = new File(arg[0]);
    InputStream in = new FileInputStream(f);

    byte[] buff = new byte[8000];

    int bytesRead = 0;

    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    while ((bytesRead = in.read(buff)) != -1) {
        bao.write(buff, 0, bytesRead);/*from w w w.j a  v  a  2s.c om*/
    }
    in.close();

    byte[] data = bao.toByteArray();

    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    System.out.println(bin.available());
}

From source file:Main.java

public static void main(String[] args) {
    String data = "<root><row><column name='username' length='6'>admin</column>"
            + "<column name='password' length='1'>p</column></row><row>"
            + "<column name='username' length='6'>j</column>"
            + "<column name='password' length='8'>q</column></row></root>";

    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new ByteArrayInputStream(data.getBytes()));

    Element root = document.getRootElement();

    List rows = root.getChildren("row");
    for (int i = 0; i < rows.size(); i++) {
        Element row = (Element) rows.get(i);
        List columns = row.getChildren("column");
        for (int j = 0; j < columns.size(); j++) {
            Element column = (Element) columns.get(j);
            String name = column.getAttribute("name").getValue();
            String value = column.getText();
            int length = column.getAttribute("length").getIntValue();

            System.out.println("name = " + name);
            System.out.println("value = " + value);
            System.out.println("length = " + length);
        }//from  w  w w . j a  va 2  s .  c  om
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a manifest from a file
    InputStream fis = new FileInputStream("manifestfile");
    Manifest manifest = new Manifest(fis);

    // Construct a string version of a manifest
    StringBuffer sbuf = new StringBuffer();
    sbuf.append("Manifest-Version: 1.0\n");
    sbuf.append("\n");
    sbuf.append("Name: javax/swing/JScrollPane.class\n");
    sbuf.append("Java-Bean: True\n");

    // Convert the string to a input stream
    InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));

    // Create the manifest
    manifest = new Manifest(is);
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    String initial = "<root><param value=\"abc\"/><param value=\"bc\"/></root>";
    ByteArrayInputStream is = new ByteArrayInputStream(initial.getBytes());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);// w  ww  .j av a2 s .c  o m

    // Create the new xml fragment
    Text a = doc.createTextNode("asdf");
    Node p = doc.createElement("parameterDesc");
    p.appendChild(a);
    Node i = doc.createElement("insert");
    i.appendChild(p);
    Element r = doc.getDocumentElement();
    r.insertBefore(i, r.getFirstChild());
    r.normalize();

    // Format the xml for output
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    // initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);

    System.out.println(result.getWriter().toString());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    System.out.println("input     : " + new String(input));
    MessageDigest hash = MessageDigest.getInstance("SHA1");

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input);
    DigestInputStream digestInputStream = new DigestInputStream(byteArrayInputStream, hash);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int ch;//from  w  ww  .j av  a 2s  . c om
    while ((ch = digestInputStream.read()) >= 0) {
        byteArrayOutputStream.write(ch);
    }

    byte[] newInput = byteArrayOutputStream.toByteArray();
    System.out.println("in digest : " + new String(digestInputStream.getMessageDigest().digest()));

    byteArrayOutputStream = new ByteArrayOutputStream();
    DigestOutputStream digestOutputStream = new DigestOutputStream(byteArrayOutputStream, hash);
    digestOutputStream.write(newInput);
    digestOutputStream.close();

    System.out.println("out digest: " + new String(digestOutputStream.getMessageDigest().digest()));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String s = "<p>" + "  <media type='audio' id='au008093' rights='aaa'>" + "    <title>title</title>"
            + "  this is a test." + "</p>";
    InputStream is = new ByteArrayInputStream(s.getBytes());

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document d = db.parse(is);/*from w w  w.jav a2  s .  c  o  m*/

    Node rootElement = d.getDocumentElement();
    System.out.println(nodeToString(rootElement));
}

From source file:Main.java

public static void main(String... args) throws IOException, SAXException, ParserConfigurationException {

    String xml = "<T rn='0'>"
            + "<I><P>UNKNOWN</P><K>UNKNOWN</K><N><O>UNKNOWN</O><P>UNKNOWN</P><U>UNKNOWN</U><N>UNKNOWN</N></N></I></T>";

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

    print(doc.getDocumentElement(), "");
}