CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "myOracle/WriteToFile" as
package myOracle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.logging.Logger;
import oracle.sql.BFILE;
import oracle.sql.BLOB;
import oracle.sql.CHAR;
import oracle.sql.CLOB;
import oracle.sql.NUMBER;
/**
* This class is a useful way to write Oracle types to file
* */
public class WriteToFile {
private static Logger l = Logger.getLogger(WriteToFile.class.getName());
/**
* Writes an array of bytes to file
* @param myFile The file name
* @param data The bytes array to write
* @param append When true the file is open in append mode
* */
public static void writeBytesToFile(String myFile, byte[] data,
Boolean append) throws Exception {
if ((data != null) && (data.length > 0)) {
FileOutputStream outStream =
new FileOutputStream(myFile, append.booleanValue());
try {
outStream.write(data, 0, data.length);
outStream.flush();
} finally {
outStream.close();
}
}
}
/**
* Writes an Oracle BLOB to file
* @param myFile The file name
* @param myBlob The Oracle BLob to write
* @param append When true the file is open in append mode
* */
public static void writeBlobToFile(String myFile, BLOB myBlob,
NUMBER append) throws Exception {
try {
FileOutputStream outStream =
new FileOutputStream(myFile, append.booleanValue());
try {
StaticAccess81.bLobWriteToStream(myBlob, outStream);
} finally {
outStream.close();
}
} catch(Exception ex) {
String s = "Writing blob to file " + myFile;
l.severe(s);
System.out.println(s);
throw ex;
}
}
/**
* Writes an Oracle BFIL to file
* @param myFile The file name
* @param myBlob The Oracle BLob to write
* @param append When true the file is open in append mode
* */
public static void writeBfileToFile(String myFile, BFILE myBlob,
NUMBER append, int buffSize) throws Exception {
FileOutputStream outStream = new FileOutputStream(myFile,
append.booleanValue());
try {
StaticAccess81.bFileWriteToStream(myBlob, outStream, buffSize);
} finally {
outStream.close();
}
}
/**
* Writes an Oracle CLOB to file
* @param myFile The file name
* @param myBlob The Oracle CLob to write
* @param append When true the file is open in append mode
* */
public static void writeClobToFile(String myFile, CLOB myBlob,
NUMBER append) throws Exception {
FileWriter outStream = new FileWriter(myFile, append.booleanValue());
try {
StaticAccess81.cLobWriteToWriter(myBlob, outStream);
} finally {
outStream.close();
}
}
/**
* Writes an Oracle Char to file, an Oracle Varchar2 is allowed too
* @param myFile The file name
* @param myBlob The Oracle Varchar2 to write
* @param append When true the file is open in append mode
*
create or replace procedure writeStringToFile(
fileName varchar2, text varchar2, append number) as
LANGUAGE JAVA NAME 'myOracle.WriteToFile.writeCharToFile(java.lang.String, oracle.sql.CHAR, oracle.sql.NUMBER)';
begin
writeStringToFile('/tmp/g', 'pippo', 0);
end;
* */
public static void writeCharToFile(String myFile, CHAR myBlob,
NUMBER append) throws Exception {
writeDatumToFile(myFile, myBlob, append, 512);
}
/**
* Writes a generic Oracle Datum to file
* @param myFile The file name
* @param myBlob The Oracle Datum to write
* @param append When true the file is open in append mode
* */
public static void writeDatumToFile(String myFile, oracle.sql.Datum myBlob,
NUMBER append,
int bufferSize) throws Exception {
FileOutputStream outStream = new FileOutputStream(myFile,
append.booleanValue());
try {
myOracle.StaticAccess81.writeDatumToStream(myBlob, outStream,
bufferSize);
} finally {
outStream.close();
}
}
/**
* @param dirs
* @return 1 when success 0 when failed
*/
public static int mkdirs(String dirs) {
File f = new File(dirs);
return f.mkdirs()?1:0;
}
}