11.3. Accessing Binary Files

Accessing binary-based files is a little bit odd with Oracle's Java — if the file is too large it will send the server's CPU spinning at 100 percent. As such, when accessing a file, you need to do so in small chunks. The following code takes a filename as its first parameter and a file offset as its second parameter. It then reads 512 bytes from that offset.

SET ESCAPE ON
SET ESCAPE "\"
SET SERVEROUTPUT ON

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "JAVAREADBINFILE" AS
import java.lang.*;
import java.io.*;

public class JAVAREADBINFILE
{
        public static void readbinfile(String f, int start) throws
IOException
      {
             FileInputStream fis;
            DataInputStream dis;
            try
            {
                  int i;
                  int ih,il;
                  int cnt = 1, h=0,l=0;
                  String hex[] = {"0", "1", "2","3", "4", "5", "6", "7",
"8","9", "A", "B", "C", "D", "E","F"};

                  RandomAccessFile raf = new RandomAccessFile (f, "r");
                  raf.seek (start);
                  for(i=0; i<=512; i++)
                  {
                        ih = il  = raf.readByte() \& 0xFF;
                        h = ih >> 4;
                        l = il \& 0x0F;

                        System.out.print("\\\\x" + hex[h] + hex[l]);
                        if(cnt \% 16 == 0)
System.out.println();
                  cnt ++;
            }


      }
      catch (EOFException eof)
            {
            System.out.println();
            System.out.println( "EOF reached " );
      }
      catch (IOException ioe)
      {
            System.out.println( "IO error: " + ioe );
      }
   }
}
/
show errors
/
CREATE OR REPLACE PROCEDURE JAVAREADBINFILEPROC (p_filename IN
VARCHAR2, p_start in number)
AS LANGUAGE JAVA
NAME 'JAVAREADBINFILE.readbinfile (java.lang.String, int)';
/
show errors
/

By directly accessing the Oracle datafiles using ...

Get The Oracle® Hacker's Handbook: Hacking and Defending Oracle now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.