Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    String sql = "SELECT name, description, image FROM pictures ";
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File image = new File("D:\\java.gif");
        FileOutputStream fos = new FileOutputStream(image);

        byte[] buffer = new byte[1];
        InputStream is = resultSet.getBinaryStream(3);
        while (is.read(buffer) > 0) {
            fos.write(buffer);/*from w w w .j ava  2s .c om*/
        }
        fos.close();
    }
    conn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Socket s = new Socket(args[0], 13);
    InputStream is = s.getInputStream();
    while (true) {
        byte b[] = new byte[100];
        int i = is.read(b);
        if (i == -1)
            break;
        System.out.print(new String(b, 0, i));
    }//from  www  . j  a va2s  .c  o m
}

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);//from   w  ww.  java 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(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);//  ww  w  .  j  a v  a  2  s . 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("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();
}

From source file:Main.java

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

    byte[] buffer = new byte[5];

    // new input stream created
    InputStream is = new FileInputStream("C://test.txt");

    System.out.println("Characters printed:");

    // read stream data into buffer
    is.read(buffer);

    // for each byte in the buffer
    for (byte b : buffer) {
        // convert byte to character
        char c = (char) b;

        // prints character
        System.out.print(c);/*  w  ww  .j  av a 2 s . c o m*/
    }
    is.close();
}

From source file:GetURL.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    InputStream urlstream = url.openStream();
    byte[] buffer = new byte[0];
    byte[] chunk = new byte[4096];
    int count;/*  ww w  . j ava  2s .  c o  m*/

    while ((count = urlstream.read(chunk)) >= 0) {
        byte[] t = new byte[buffer.length + count];
        System.arraycopy(buffer, 0, t, 0, buffer.length);
        System.arraycopy(chunk, 0, t, buffer.length, count);
        buffer = t;
    }

    String filename = (url.getFile()).replace('/', File.separatorChar);
    File f1 = new File(filename);
    filename = f1.getName();
    FileOutputStream f = null;
    f = new FileOutputStream(filename);
    f.write(buffer);
    f.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    try {/*from w  w w. j a v  a 2  s .c  o m*/

        URL url = new URL("http://www.java2s.com");

        // Obtain output stream
        InputStream is = url.openStream();

        // Read and display data from url
        byte buffer[] = new byte[1024];
        int i;
        while ((i = is.read(buffer)) != -1) {
            System.out.write(buffer, 0, i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL u = new URL("http://www.google.com");
    InputStream in = u.openStream();
    MessageDigest sha = MessageDigest.getInstance("SHA");
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        sha.update(data, 0, bytesRead);//from  w  ww.  j  a  v a 2s.c  o m
    }
    byte[] result = sha.digest();
    System.out.println(Arrays.toString(result));
    System.out.println(new BigInteger(result));
}

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 ww . j  ava2 s  .  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) throws IOException {
    URL url = new URL("http://www.java2s.com/style/download.png");

    InputStream inputStream = url.openStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    int n = 0;//from w w w.  ja  v  a 2  s .  co m
    while (-1 != (n = inputStream.read(buffer))) {
        output.write(buffer, 0, n);
    }
    inputStream.close();

    byte[] data = output.toByteArray();

    OutputStream out = new FileOutputStream("data.png");
    out.write(data);
    out.close();

    for (byte b : data) {
        System.out.printf("0x%x ", b);
    }
}