﻿CREATE OR REPLACE JAVA SOURCE NAMED "BlobHandler"
      AS import java.lang.*;
    import java.sql.*;
    import oracle.sql.*;
    import java.io.*;
    public class BlobHandler {
      public static void ExportBlob(String myFile, BLOB myBlob) throws Exception {
        File binaryFile = new File(myFile);
        FileOutputStream outStream = new FileOutputStream(binaryFile);
      InputStream inStream = myBlob.getBinaryStream();
      int size;
      if (myBlob.length()> 20000000) {  // tune this to whatever appropriate value
        size = 20000000;
      } else {
        size = (int)myBlob.length();
      }
      byte[] buffer = new byte[size];
      int length = -1;
      while ((length = inStream.read(buffer)) != -1)
      {
        outStream.write(buffer, 0, length);
        outStream.flush();
      }
      inStream.close();
      outStream.close();
    }
  }
  /
ALTER JAVA SOURCE "BlobHandler" COMPILE;

CREATE OR REPLACE PROCEDURE ExportBlobJava (p_file   IN VARCHAR2,
                                            p_blob   IN BLOB)
    AS
       LANGUAGE JAVA
       NAME 'BlobHandler.ExportBlob(java.lang.String, oracle.sql.BLOB)';
    /  
	

EXEC Dbms_Java.Grant_Permission( 'TRACKER', 'SYS:java.io.FilePermission','/mnt/issuetracker/text.xml', 'read,write' )
EXEC Dbms_Java.Grant_Permission( 'TRACKER', 'SYS:java.io.FilePermission','/mnt/issuetracker/*', 'write' )

