0

I have created Java class within Oracle but not sure how to create a function to return value. Any help would be appreciated.

create or replace and compile java source named "ErrorMessage" 
as 
 import java.util.zip.Inflater;
 public class ErrorMessage {
    public static String uncompressMessage(byte[] cdata, int        uncompressedLength){
        String uncompData2 = null;
         try{

            Inflater inflater = new Inflater();
            inflater.setInput(cdata);
            Byte [] uncompData = new byte[uncompressedLength];
            int n = inflater.inflate(uncompData);
      uncompData2 = uncompData.toString();
            inflater.end();

        }catch (Exception e){
            e.getMessage();
        }
        return uncompData2;
    }
}

create or replace 
function getErrorMessage(msgdata blob, msgdatalength integer)
return varchar2
as language java
name 'ErrorMessage.uncompressMessage(byte[],int)
return java.lang.String';
MT0
  • 143,790
  • 11
  • 59
  • 117
ken78
  • 3
  • 2
  • Your data types do not match - if you are passing a BLOB then the argument should have the data type `oracle.sql.BLOB` and not `byte[]` – MT0 Jun 13 '18 at 19:59

1 Answers1

1

Something like:

create or replace and compile java source named "ErrorMessage" 
as 
  import java.util.zip.Inflater;
  import java.lang.Byte;
  import java.sql.Blob;

  public class ErrorMessage {
    public static String uncompressMessage( Blob data, int uncompressedLength ){
      String uncompData2 = null;
      try{
        final long size = data.length();
        final byte[] cdata = data.getBytes( 0L, (int) size );
        Inflater inflater = new Inflater();
        inflater.setInput( cdata );
        byte[] uncompData = new byte[uncompressedLength];
        int n = inflater.inflate(uncompData);
        uncompData2 = new String( uncompData, "UTF-8" );
        inflater.end();
      }catch (Exception e){
        e.getMessage();
      }
      return uncompData2;
    }
}
/

create or replace function getErrorMessage(msgdata blob, msgdatalength number) return varchar2
as language java
name 'ErrorMessage.uncompressMessage(java.sql.Blob,int) return java.lang.String';
MT0
  • 143,790
  • 11
  • 59
  • 117
  • Really appreciated for quick response. When I ran the select statement than It returned NULL instead of uncompressmessage. I can see length of the message. Any suggestion ? – ken78 Jun 13 '18 at 21:28
  • select getErrorMessage(msgdata,msglength) from table – ken78 Jun 13 '18 at 21:28