import java.io.*; import java.util.*; import java.sql.*; import org.postgresql.fastpath.Fastpath; import org.postgresql.fastpath.FastpathArg; public class BlobTestFunction { private Connection con = null; public BlobTestFunction() { try { Class.forName("org.postgresql.Driver"); con = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/files", "blind", ""); 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) { File file = new File(filename); byte[] l_buf = new byte[8000]; int l_size = 0; System.out.println("store start " + System.currentTimeMillis() + " file.name:" + file.getName() + " file.length:"+file.length()); try { FileInputStream fis = new FileInputStream(file); con.setAutoCommit(false); Fastpath l_fp = ((org.postgresql.Connection)con).getFastpathAPI(); // Now get the function oid's for the api Statement l_stmt = null; ResultSet l_rset = null; try { try { l_stmt = con.createStatement(); l_rset = l_stmt.executeQuery("SELECT PRONAME, OID FROM PG_PROC " + "WHERE PRONAME = 'bytea_write'"); if(l_rset == null) { System.err.println("Missing funciton bytea_write"); } l_fp.addFunctions(l_rset); } finally { if (l_rset != null) { l_rset.close(); } if (l_stmt != null) { l_stmt.close(); } } } catch (SQLException l_se) { System.err.println("Store content: " + l_se.getMessage()); } FastpathArg args[] = new FastpathArg[3]; while ( l_size < file.length()) { fis.read(l_buf); args[0] = new FastpathArg(filename); args[1] = new FastpathArg(l_size); args[2] = new FastpathArg(l_buf, 0, 8000); l_size += 8000; l_fp.fastpath("bytea_write", true, args); } fis.close(); con.commit(); System.out.println("store end " + System.currentTimeMillis()); } catch (SQLException sqle) { System.err.println("Store content: " + sqle.getMessage()); } catch (IOException ioe) { System.err.println("Store content: " + ioe.getMessage()); } } private void retrieve(String filename) { Statement s = null; ResultSet r = null; int byteSum = 0; byte[] buffer; try { System.out.println("read start " + System.currentTimeMillis() + " Trying to write: " +filename + ".test"); FileOutputStream out = new FileOutputStream(filename + ".test"); s = con.createStatement(); r = s.executeQuery("SELECT data FROM tblob1 WHERE filename = '" + filename +"' order by lastbyte"); if (r != null) { while (r.next()) { buffer = r.getBytes(1); out.write(buffer); byteSum += buffer.length; } } out.close(); System.out.println("read end " + System.currentTimeMillis() + " 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) { BlobTestFunction bt = new BlobTestFunction(); bt.store(args[0]); bt.retrieve(args[0]); } }