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

public static void main(String[] args) {

    byte[] arrByte = new byte[1024];

    byte[] byteArray = new byte[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    // create object of PushbackInputStream class for specified stream
    InputStream is = new ByteArrayInputStream(byteArray);
    PushbackInputStream pis = new PushbackInputStream(is);

    try {//from   www  . ja va 2  s  .c  o m

        // read a char into our array
        pis.read(arrByte, 0, 3);

        // print arrByte
        for (int i = 0; i < 3; i++) {
            System.out.println((char) arrByte[i]);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    byte[] arrByte = new byte[1024];
    byte[] byteArray = new byte[] { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    InputStream is = new ByteArrayInputStream(byteArray);
    PushbackInputStream pis = new PushbackInputStream(is, 10);

    try {/*  w  w  w  . j  a v  a  2s . co  m*/
        // read from the buffer one character at a time
        for (int i = 0; i < byteArray.length; i++) {
            // read a char into our array
            arrByte[i] = (byte) pis.read();
            System.out.println((char) arrByte[i]);
        }
        // unread a char
        pis.unread('F');

        // read again from the buffer 
        arrByte[1] = (byte) pis.read();
        System.out.println((char) arrByte[1]);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    byte[] picture = new byte[2048]; // your image data

    InputStream in = new ByteArrayInputStream(picture);

    BufferedImage buf = ImageIO.read(in);
    ColorModel model = buf.getColorModel();
    int height = buf.getHeight();
    int width = buf.getWidth();
}

From source file:BufferedInputStreamDemo.java

public static void main(String args[]) throws IOException {
    String s = "This is a &copy; copyright symbol " + "but this is &copy not.\n";
    byte buf[] = s.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(buf);
    BufferedInputStream f = new BufferedInputStream(in);
    int c;/*from   w  ww.j a va  2  s .  c  o  m*/
    boolean marked = false;

    while ((c = f.read()) != -1) {
        switch (c) {
        case '&':
            if (!marked) {
                f.mark(32);
                marked = true;
            } else {
                marked = false;
            }
            break;
        case ';':
            if (marked) {
                marked = false;
                System.out.print("(c)");
            } else
                System.out.print((char) c);
            break;
        case ' ':
            if (marked) {
                marked = false;
                f.reset();
                System.out.print("&");
            } else
                System.out.print((char) c);
            break;
        default:
            if (!marked)
                System.out.print((char) c);
            break;
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] data = new byte[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        data[i] = (byte) (64 + (i % 32));
    }//from  w  ww  . j a v  a 2  s .  c  o  m
    ByteArrayInputStream stream = new ByteArrayInputStream(data);
    // DriverManager.registerDriver(new OracleDriver());
    Connection c = DriverManager.getConnection("jdbc:oracle:thin:@some_database", "user", "password");

    String sql = "DECLARE\n" + //
            "  l_line    CLOB;\n" + //
            "BEGIN\n" + //
            "  l_line := ?;\n" + //
            "  UPDATE table SET log = log || l_line || CHR(10) WHERE id = ?;\n" + //
            "END;\n"; //

    PreparedStatement stmt = c.prepareStatement(sql);
    stmt.setAsciiStream(1, stream, SIZE);
    stmt.setInt(2, 1);
    stmt.execute();
    stmt.close();
    c.commit();
    c.close();
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/*from  ww  w  .  ja v  a  2 s .com*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from   w  w  w .j ava  2 s . c om
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());

    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();

    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);

    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:ByteArrayIOApp.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from w  ww  .j  a v  a  2s.  c om
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:SaveVector1.java

public static void main(String args[]) throws Exception {
    Vector v = new Vector(Arrays.asList(args));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);/*www.j a  va2s .  c o  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:MainClass.java

public static void main(String args[]) throws Exception {
    Vector v = new Vector(Arrays.asList("a", "b", "c"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(v);//w  ww  . j av  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());
    }
}