Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

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

Prototype

public synchronized void write(int b) 

Source Link

Document

Writes the specified byte to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;//from   ww w  .j a v  a2s. c  o m
    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: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 w  w. j a  v a2  s. 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:MainClass.java

public static void main(String args[]) throws Exception {
    ByteArrayOutputStream f = new ByteArrayOutputStream(12);
    System.out.println("Please 10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }//from  w w w . j a v  a2 s. c  o  m
    System.out.println("Buffer as a string");
    System.out.println(f.toString());
    System.out.println("Into array");
    byte b[] = f.toByteArray();
    for (int i = 0; i < b.length; i++) {
        System.out.print((char) b[i]);
    }
    System.out.println();
    OutputStream f2 = new FileOutputStream("test.txt");
    f.writeTo(f2);
    f.reset();
    System.out.println("10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }
    System.out.println("Done..");
}

From source file:Main.java

public static void writeString(String s, ByteArrayOutputStream os) throws IOException {
    os.write(s.getBytes());
    os.write(0); //Null terminator
}

From source file:Main.java

public static void writeUint32BE(ByteArrayOutputStream buffer, long value) {
    buffer.write((byte) ((value >> 24) & 0xff));
    buffer.write((byte) ((value >> 16) & 0xff));
    buffer.write((byte) ((value >> 8) & 0xff));
    buffer.write((byte) (value & 0xff));
}

From source file:Main.java

public static void writeUint32LE(ByteArrayOutputStream buffer, long value) {
    buffer.write((byte) (value & 0xff));
    buffer.write((byte) ((value >> 8) & 0xff));
    buffer.write((byte) ((value >> 16) & 0xff));
    buffer.write((byte) ((value >> 24) & 0xff));
}

From source file:Main.java

public static byte[] concatenateTwoByteArrays(byte a[], byte b[]) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(a);
    outputStream.write(b);//from w ww  .j  a  v a2s .  c om
    return outputStream.toByteArray();
}

From source file:MainClass.java

private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);//from  www .  j  a  v  a  2s  .c  om

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);
    baos.write(ciphertext);
    return baos.toByteArray();
}

From source file:Main.java

public static void writeUint64BE(ByteArrayOutputStream buffer, long value) {
    buffer.write((byte) ((value >> 56) & 0xff));
    buffer.write((byte) ((value >> 48) & 0xff));
    buffer.write((byte) ((value >> 40) & 0xff));
    buffer.write((byte) ((value >> 32) & 0xff));
    buffer.write((byte) ((value >> 24) & 0xff));
    buffer.write((byte) ((value >> 16) & 0xff));
    buffer.write((byte) ((value >> 8) & 0xff));
    buffer.write((byte) (value & 0xff));
}

From source file:Main.java

public static void writeUint64LE(ByteArrayOutputStream buffer, long value) {
    buffer.write((byte) (value & 0xff));
    buffer.write((byte) ((value >> 8) & 0xff));
    buffer.write((byte) ((value >> 16) & 0xff));
    buffer.write((byte) ((value >> 24) & 0xff));
    buffer.write((byte) ((value >> 32) & 0xff));
    buffer.write((byte) ((value >> 40) & 0xff));
    buffer.write((byte) ((value >> 48) & 0xff));
    buffer.write((byte) ((value >> 56) & 0xff));
}