utf-8 oracle clob, write/read pdf in java?

1.3k views Asked by At

my mission is to change 2 php pages with a java webapp that writes an upload pdf file to a clob and reads it when user ask for a download.

I threated the pdf as a byte array and have been able to read/write it correctly

the big problem is the backward compatibility: files written by php are not readable by my java webapp and vice-versa

thanks in advance for help

NOTE: Do not answer me to use a Blob, I know it is the easy way but in this case we have to assume we cannot make an alter table on the db due to backward compatibility

Here's my code to read the clob into a byte array:

byte[] result = null;
InputStream            is = null;
ByteArrayOutputStream bos = null;
//...prepare the query to get clob as first column in the resultset
ResultSet rs = stmt.executeQuery();
int len;
int size = 1024;
byte[] buf;
if(rs.next()) {
      is = rs.getBinaryStream(1);
      bos = new ByteArrayOutputStream();
      buf = new byte[size];
      while ((len = is.read(buf, 0, size)) != -1)
         bos.write(buf, 0, len);
      bos.flush();
      bos.close();
      is.close();
      result = bos.toByteArray();
}

rs.close();

here's the code to write the byte array into clob:

//...some other sql stuff here...
stmt = conn.prepareStatement("SELECT clob_col FROM my_table WHERE prim_key = ? FOR UPDATE");
stmt.setString(1, param);

ResultSet rs = stmt.executeQuery();
Clob myclob=null;
if(rs.next())
    myclob=rs.getClob(1);

OutputStream writer = myclob.setAsciiStream(1L);
writer.write(allegato);
writer.flush();
writer.close();
stmt = conn.prepareStatement("UPDATE my_table SET clob_col = ? WHERE prim_key = ? ");
stmt.setClob(1, myclob);
stmt.setString(2, param);
stmt.executeUpdate();

oracle encoding is ITALIAN_ITALY.WE8ISO8859P1 php encoding is ITALIAN_ITALY.UTF8

1

There are 1 answers

0
Nik Developer On

A possible solution: write to clob the hex rapresentation of the byte array, and do the same in read phase

the main advantage are - few changes in php and java - no changes to db (alter table) - indipendent from db encoding

in php we use bin2hex and hex2bin functions before to write and after read the clob

in java we implement 2 easy equivalent functions of bin2hex and hex2bin:

    public static byte[] HexToBin(String str){
        try{
            byte[] result=new byte[str.length()/2];
            for (int i = 0; i < result.length; i++)
                result[i]=(byte) Integer.parseInt(str.substring(2*i, (2*i)+2), 16);
            return result;
        }catch(Exception x){
            x.printStackTrace();
            return null;
        }
    }

    public static String BinToHex(byte[] b){
        try{
            StringBuffer sb=new StringBuffer();
            for (byte bb:b) {
                String hexStr = String.format("%02x", bb);
                sb.append((hexStr.length()<2)?"0":"");
                sb.append(hexStr);
            }
            return sb.toString();
        }catch(Exception x){
            x.printStackTrace();
            return null;
        }
    }