import java.io.*;
import java.util.*;
import java.sql.*;


public class BlobTest {

    private Connection con = null;

    public BlobTest() {
        try {
            Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(
                    "jdbc:postgresql:repository", "candle", "");
            if (con != null) {
               System.out.println("Connected to database system.\n");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            System.out.println("Could not connect to database system.\n");
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found...:-(\n");
        }
    }
    
    private void store(String filename) {
        PreparedStatement ps = null;
        ResultSet r = null;
        File file = new File(filename);
        System.out.println("file.name:" + file.getName() + "file.length:"+file.length());
        try {
            FileInputStream fis = new FileInputStream(file);
            con.setAutoCommit(false);
            ps = con.prepareStatement(
                "INSERT INTO blobs VALUES (?, ?)");
            ps.setString(1, filename);
            System.out.println("File.length(int): " + (int) file.length());
            ps.setBinaryStream(2, fis, (int) file.length());
            ps.executeUpdate();
            ps.close();
            fis.close();
            con.commit();
        } catch (SQLException sqle) {
            System.err.println("Store content: " + sqle.getMessage());
        } catch (IOException ioe) {
        }
    }

    private void retrieve(String filename) {
        Statement s = null;
        ResultSet r = null;
        int byteSum = 0;
        int bytesRead = 0;
        byte[] buffer = new byte[8 * 1924];
        try {
            System.out.println("Trying to write: " +filename + "test");
            FileOutputStream out = new FileOutputStream(filename + "test");
            con.setAutoCommit(false);
            s = con.createStatement();
            r = s.executeQuery("SELECT data FROM blobs WHERE filename = '"
                               + filename +"'");
            if (r != null) {
                while (r.next()) {
                    System.out.println("We have a result!");
                    InputStream is = r.getBinaryStream(1);
                    while ((bytesRead = is.read(buffer)) != -1) {
                        byteSum += bytesRead;
                        out.write(buffer, 0, bytesRead);
                    }
                    is.close();
                }
            }
            out.close();
            System.out.println("bytes written: " + byteSum);
            con.commit();
        } catch (SQLException sqle) {
            System.err.println("Retrieve content: " + sqle.getMessage());
        } catch (Exception ioe) {
            System.err.println("Writing stuff: " + ioe.getMessage());
        }
    }

    public static void main(String[] args) {
        BlobTest bt = new BlobTest();
        bt.store(args[0]);
        bt.retrieve(args[0]);
    }
    
}
