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';
1
  • 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[] Commented Jun 13, 2018 at 19:59

1 Answer 1

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';
Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
select getErrorMessage(msgdata,msglength) from table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.