/***************************************************************************
  * Copyright (c) 2005, Northwest Summit. All Rights Reserved.
  *
  * This software is the proprietary information of Northwest Summit.
  * Use is subject to license terms.
  ***************************************************************************/
 
 import java.io.*;
 import java.sql.*;
 
 /**
  * Test to produce an ArrayIndexOutOfBoundsException using Oracle 10.x JDBC
  * driver. Run the test with the corresponding test-21.data file to produce
  * the error.
  * <p/>
  * Usage: java OraTestLobs test-21.data
  */
 public class OraTestLobs {
 
     /**
      * Create the table and stored procedure necessary for the test.
      */
     public static void createTable(Statement stmt) throws SQLException
     {
         ResultSet rs = stmt.executeQuery
             ("SELECT num_rows FROM user_tables WHERE table_name='LOBTEST'");
         try 
         {
             if (!rs.next())
             {
                 // create the table
                 stmt.execute("CREATE TABLE lobtest (id NUMBER, sz INTEGER, text CLOB, binary BLOB)");
 
                 // create the stored procedure for insertion
                 stmt.execute("CREATE OR REPLACE PROCEDURE c_lobtest (p_id NUMBER, p_size INTEGER, p_text CLOB, p_bin BLOB) AS BEGIN INSERT INTO lobtest VALUES (p_id, p_size, p_text, p_bin); END;");
             }
         }
         finally
         {
             rs.close();
         }
     }
 
     /**
      * Usage: java OraTestLobs test-21.data
      */
     public static void main(String[] args) throws Exception
     {
         String filename = args[0];
 
         Class.forName(oracle.jdbc.driver.OracleDriver.class.getName());
         String jdbcUrl = "jdbc:oracle:thin:@localhost:1orcl";
         Connection con =
           DriverManager.getConnection(jdbcUrl, "scott", "tiger");
 
         FileInputStream fis = null;
         Statement stmt = null;
         PreparedStatement pstmt = null;
         try 
         {
             stmt = con.createStatement();
             createTable(stmt);
 
             pstmt = con.prepareCall("{call c_lobtest(?,?,?,?)}");
 
             // insert text
             String text = "serial 1\nmode active-up\n";
             ByteArrayInputStream bain =
                 new ByteArrayInputStream(text.getBytes());
 
             long id = System.currentTimeMillis();
             int size = text.length();
 
             pstmt.setLong(1, id);
             pstmt.setInt(2, size);
             pstmt.setCharacterStream(3, new InputStreamReader(bain), size);
             pstmt.setBytes(4, null);
             pstmt.execute();
 
             // insert binary
             File file = new File(filename);
             fis = new FileInputStream(file);
 
             id += 1;
             size = (int)file.length();
 
             pstmt.setLong(1, id);
             pstmt.setInt(2, size);
             pstmt.setString(3, null);
             pstmt.setBinaryStream(4, fis, size);
             pstmt.execute();
         }
         finally
         {
             if (stmt != null)
             {
                 try { stmt.close(); }
                 catch (SQLException ignore) {}
             }
             if (pstmt != null)
             {
                 try { pstmt.close(); }
                 catch (SQLException ignore) {}
             }
             if (fis != null) {
               try { fis.close(); }
               catch (IOException ignore) {}
             }
 
             con.close();
         }
     }
 }