Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Comparing two OracleBlobInputStream

    Hi Isomorphic,

    can you please help me with this.
    I have to compare if two blob data are the same.
    Using v12.0p_2019-04-03/PowerEdition Deployment (built 2019-04-03) and ORACLE database.

    ds.xml.
    Code:
            <field name="DATA" type="binary" required="true" maxFileSize="8388608" />
    I fetch two rows. Both rows contain the same "DATA"-field.

    Server side
    Code:
    DSRequest request = new DSRequest("datasource", DataSource.OP_FETCH);
    DSResponse response = request.execute();
    
    Map<String, Object> firstMap = (Map<String, Object>) response.getRecords().get(0);
    Map<String, Object> secondMap = (Map<String, Object>) response.getRecords().get(1);
    
    //type of the o1 is: oracle.jdbc.driver.OracleBlobInputStream@66cd9398
    Object o1= firstMap.get("DATA");
    
    //type of the o2 is oracle.jdbc.driver.OracleBlobInputStream@7f3fe447
    Object o2= secondMap.get("DATA");
    
    //I need somehow get byte[] from OracleBlobInputStream
    //byte[] byte1=o1.toString.getBytes(); is ofcourse wrong
    
    ..
    ..
    ..
    Do you have some hint?

    Thanks in advance!

    Pavo

    #2
    Since OracleBlobInputStream is an InputStream, you would just use standard Java techniques for comparing input streams. We don't know what you're storing here so not sure if it would be appropriate to place it into a String (via StringWriter) or what.

    Comment


      #3
      Hi Isomorphic,

      thank you for your answer. Stored can be literally all, customer stores what he/she wants.

      I just can't read bytes from InputStream (I always get -1: no byte is available because the end of the stream has been reached). So I'm not sure can I handle OracleBlobInputStream as standard InputStream.

      Here is one way how to compare two InputStreams
      Code:
      //code from post #1
      
      InputStream x = ((InputStream) o1);
      InputStream x = ((InputStream) o2);
      compare(o1,o2);
      
          private static boolean compare(InputStream input1, InputStream input2) throws IOException {
              boolean error = false;
              try {
                  byte[] buffer1 = new byte[1024];
                  byte[] buffer2 = new byte[1024];
                  try {
                      int numRead1 = 0;
                      int numRead2 = 0;
                      while (true) {
                          numRead1 = input1.read(buffer1);
                          numRead2 = input2.read(buffer2);
                          if (numRead1 > -1) {
                              if (numRead2 != numRead1)
                                  return false;
                              // Otherwise same number of bytes read
                              if (!Arrays.equals(buffer1, buffer2))
                                  return false;
                              // Otherwise same bytes read, so continue ...
                          } else {
                              // Nothing more in stream 1 ...
                              return numRead2 < 0;
                          }
                      }
                  } finally {
                      input1.close();
                  }
              } catch (IOException e) {
                  error = true; // this error should be thrown, even if there is an error closing stream 2
                  throw e;
              } catch (RuntimeException e) {
                  error = true; // this error should be thrown, even if there is an error closing stream 2
                  throw e;
              } finally {
                  try {
                      input2.close();
                  } catch (IOException e) {
                      if (!error)
                          throw e;
                  }
              }
          }
      That's why I think I have to use some other technique, to cast it into some other type or something like that.
      But it does not work with OracleBlobInputStream. Or I'm wrong?

      Thanks in advance!

      Pavo

      Comment


        #4
        You seem to have changed your question - rather than wanting to know how to compare data, you now seem to be saying that you can't retrieve data from the DB at all.

        If that's true then please submit a bug report showing how this can be reproduced. Right now we are showing that DataSource operations on binary columns are behaving as expected.

        Comment


          #5
          Hi Isomorphic,

          I'm sorry I'm not exactly precise.
          I can retrieve data from DB. Type of "DATA" is OracleBlobInputStream (oracle.jdbc.driver.OracleBlobInputStream@1f37c3a4).

          But I can only read bytes from "DATA" this way:
          Code:
          byte[] byte1=o1.toString.getBytes();
          Which is of course wrong.

          The standard way of reading bytes from InputStream is just not working! And I don't know why.
          Code:
                                  InputStream is = ((InputStream) o1);
                                  BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                  String line = null;
                                  while ((line = reader.readLine()) != null) {
                                      System.out.println(line);
                                  }
                                  reader.close();
          But in any case thank you for your help!

          Best regards
          Pavo
          Last edited by pavo123; 12 Apr 2019, 07:49. Reason: Formatting

          Comment

          Working...
          X