Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

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

Prototype

public ByteArrayOutputStream() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:House.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    House house = new House();
    List animals = new ArrayList();
    animals.add(new Animal("Bosco the dog", house));
    animals.add(new Animal("Ralph the hamster", house));
    animals.add(new Animal("Fronk the cat", house));
    System.out.println("animals: " + animals);
    ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
    ObjectOutputStream o1 = new ObjectOutputStream(buf1);
    o1.writeObject(animals);//from   w  w w  .  j av a2  s  . co  m
    o1.writeObject(animals); // Write a 2nd set
    // Write to a different stream:
    ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
    ObjectOutputStream o2 = new ObjectOutputStream(buf2);
    o2.writeObject(animals);
    // Now get them back:
    ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
    ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
    List animals1 = (List) in1.readObject(), animals2 = (List) in1.readObject(),
            animals3 = (List) in2.readObject();
    System.out.println("animals1: " + animals1);
    System.out.println("animals2: " + animals2);
    System.out.println("animals3: " + animals3);
}

From source file:net.itransformers.bgpPeeringMap.ThirdTransformation.java

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

    ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();

    XsltTransformer transformer = new XsltTransformer();
    File xsltFileName1 = new File("/Users/niau/trunk/bgpPeeringMap/conf/xslt/reorder.xslt");

    byte[] rawData = readRawDataFile(
            "/Users/niau/trunk/bgpPeeringMap/src/main/resources/bgpPeeringMap.graphml");
    ByteArrayInputStream inputStream1 = new ByteArrayInputStream(rawData);
    transformer.transformXML(inputStream1, xsltFileName1, outputStream1);
    File outputFile1 = new File("/Users/niau/trunk/bgpPeeringMap/src/main/resources",
            "bgpPeeringMap2.graphxml");

    FileUtils.writeStringToFile(outputFile1, new String(outputStream1.toByteArray()));

}

From source file:HelloWorldAddMetadata.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
    document.open();//from  www  .j av  a2  s.  c om
    document.add(new Paragraph("Hello World"));
    document.close();

    PdfReader reader = new PdfReader("HelloWorld.pdf");
    System.out.println("Tampered? " + reader.isTampered());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloWorldStampedMetadata.pdf"));
    System.out.println("Tampered? " + reader.isTampered());
    HashMap<String, String> info = reader.getInfo();
    info.put("Subject", "Hello World");
    info.put("Author", "your name");
    info.put("Keywords", "iText pdf");
    info.put("Title", "Hello World stamped");
    info.put("Creator", "your name");
    stamper.setMoreInfo(info);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmpWriter xmp = new XmpWriter(baos, info);
    xmp.close();
    stamper.setXmpMetadata(baos.toByteArray());
    stamper.close();
}

From source file:net.itransformers.bgpPeeringMap.DavidPiegzaFormatTransformer.java

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();

    XsltTransformer transformer = new XsltTransformer();
    File xsltFileName1 = new File("/Users/niau/trunk/bgpPeeringMap/conf/xslt/david_piegza.xslt");
    byte[] rawData = readRawDataFile("/Users/niau/test1/network/version1/undirected/imap.graphml");
    ByteArrayInputStream inputStream1 = new ByteArrayInputStream(rawData);
    transformer.transformXML(inputStream1, xsltFileName1, outputStream1, null, null);
    File outputFile1 = new File("/Users/niau/trunk/bgpPeeringMap/src/main/resources", "imap.xml");
    FileUtils.writeStringToFile(outputFile1, new String(outputStream1.toByteArray()));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    userdom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = userdom.createElement("users");
    Node adoptNode = userdom.adoptNode(root);
    userdom.appendChild(adoptNode);//from   w w w  .  ja  v  a 2s. c om
    Element e = userdom.createElement("user");

    e.setAttribute("id", "blah");
    e.setAttribute("username", "kermit");
    e.setAttribute("password", "bunnies in the air");
    e.setAttribute("login", "kermmi");

    userdom.getFirstChild().appendChild(e);
    System.out.println(e);
    System.out.println(userdom.getFirstChild() + "|" + userdom.getFirstChild().getFirstChild());
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    System.out.println(userdom.getFirstChild() + "|" + userdom.getFirstChild().getFirstChild());
    DOMSource ds = new DOMSource(userdom);

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        StreamResult sr = new StreamResult(baos);
        trans.transform(ds, sr);
        System.out.println(new String(baos.toByteArray()));
    }
}

From source file:net.sf.jabb.util.text.test.KeywordMatcherExample.java

/**
 * @param args/*  w  ww  .java  2  s.  c  om*/
 * @throws IOException 
 * @throws ClassNotFoundException 
 */
public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("====  ====");
    KeywordMatcher m = showExample(null);

    System.out.println("==== ? ====");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(m);
    byte[] binary = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(binary);
    ObjectInputStream ois = new ObjectInputStream(bais);
    KeywordMatcher m2 = (KeywordMatcher) ois.readObject();
    showExample(m2);

    System.out.println("==== ? ====");
    KeywordMatcher m3 = new KeywordMatcher(m);
    showExample(m3);

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
            0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x01 };/*w  w  w . j a va 2 s  . co  m*/

    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    System.out.println("input : " + new String(input));

    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    ByteArrayInputStream bIn = new ByteArrayInputStream(input);
    CipherInputStream cIn = new CipherInputStream(bIn, cipher);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    int ch;
    while ((ch = cIn.read()) >= 0) {
        bOut.write(ch);
    }

    byte[] cipherText = bOut.toByteArray();

    System.out.println("cipher: " + new String(cipherText));

    // decryption pass
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    bOut = new ByteArrayOutputStream();
    CipherOutputStream cOut = new CipherOutputStream(bOut, cipher);
    cOut.write(cipherText);
    cOut.close();
    System.out.println("plain : " + new String(bOut.toByteArray()));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from   w  w w .ja va2s.c om*/
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rs = null;
    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();
    createXMLTable(stmt);
    File f = new File("build.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    String SQL = "INSERT INTO XML_Data VALUES (?,?)";
    pstmt = conn.prepareStatement(SQL);
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    SQL = "SELECT Data FROM XML_Data WHERE id=100";
    rs = stmt.executeQuery(SQL);
    if (rs.next()) {
        InputStream xmlInputStream = rs.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    rs.close();
    stmt.close();
    pstmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);//w w  w .j  a  v a 2s. co m

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob(3);

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");

    Statement stmt = conn.createStatement();

    createBlobClobTables(stmt);/* w w w  .j  a v  a  2 s . c  o  m*/

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());

    file = new File("clob.txt");
    fis = new FileInputStream(file);
    pstmt.setAsciiStream(2, fis, (int) file.length());
    fis.close();

    pstmt.execute();

    ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");
    rs.next();

    java.sql.Blob blob = rs.getBlob(2);
    java.sql.Clob clob = rs.getClob("myClobColumn");

    byte blobVal[] = new byte[(int) blob.length()];
    InputStream blobIs = blob.getBinaryStream();
    blobIs.read(blobVal);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(blobVal);
    blobIs.close();

    char clobVal[] = new char[(int) clob.length()];
    Reader r = clob.getCharacterStream();
    r.read(clobVal);
    StringWriter sw = new StringWriter();
    sw.write(clobVal);

    r.close();
    conn.close();
}