Solved

bFile and getBinaryStream()

Posted on 2004-10-18
5
543 Views
Last Modified: 2012-06-27
     
      
hi!

can anybody explain why getBinaryStream()  is used and the purpose of variable 'offset' in the following function?
++++++++++++++++++++++
      public AssessTestType select(String typeId) {
            log.debug("start");
            String sql =
                  "SELECT test_type_title, test_template_xml, test_template_xsl"
                        + " FROM Assess_Test_Types WHERE assess_test_type_id="
                        + typeId;

            AssessTestType testType = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {
                  stmt = db.createStatement();
                  rs = stmt.executeQuery(sql);

                  if (rs.next()) {
                        testType = new AssessTestType();
                        testType.setTestTypeId(typeId);
                        testType.setTypeTitle(rs.getString("test_type_title"));

                        String[] bfileNames =
                              { "test_template_xml", "test_template_xsl" };
                        String[] xmlXslOut = new String[2];

                        try {
                              for (int i = 0; i < 2; i++) {
                                    StringBuffer tmpBuffer = new StringBuffer();
                                    // Note : Using Oracle Extension oracle.sql.BFILE to get BFILE Locator
                                    oracle.sql.BFILE bfile = null;
                                    bfile = ((OracleResultSet) rs).getBFILE(bfileNames[i]);
                                    
                                    if (bfile == null) {
                                          if ("test_template_xsl".equals(bfileNames[i])) {
                                                xmlXslOut[i] = getGenericXSL();
                                          }
                                    } else { // bfile is not null

                                          // Get the BFILE Locator
                                          // Open the file with openFile Method of oracle.sql.BFILE class
                                          bfile.openFile();

                                          InputStream is = bfile.getBinaryStream();
                                          int offset = 1024;
                                          int len = 0;
                                          byte[] buffer = new byte[offset];

                                          while ((len = is.read(buffer)) > 0) {
                                                tmpBuffer.append(new String(buffer, 0, len));
                                          }
                                          xmlXslOut[i] = tmpBuffer.toString();
                                          log.debug(
                                                "String Size: "
                                                      + xmlXslOut[i].getBytes().length);
                                          log.debug("Returned XML: \n" + xmlXslOut[i]);

                                          is.close();
                                          bfile.closeFile();

                                          is = null;
                                    }
                              }
                        } catch (SQLException e) {
                              log.error(e);
                        } catch (IOException e) {
                              log.error(e);
                        }

                        testType.setXmlTemplate(xmlXslOut[0]);
                        testType.setHtmlXSLFile(xmlXslOut[1]);

                  }
            } catch (SQLException e) {
                  log.error(e);
            } finally {
                  try {
                        if (rs != null)
                              rs.close();
                        if (stmt != null)
                              stmt.close();
                  } catch (SQLException e1) {
                        log.error(e1);
                  }
            }

            return testType;
      }
++++++++++++
thanks,
0
Comment
Question by:dkim18
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
These are binary fields to hold files that are being retrieved, hence the use of streams to get the files. Personally i think CLOB should be used or Unicode can't really be supported without doing one's own decoding
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
'offset' is a misleading name. All it does in the above is to specify the buffer size for the file read
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now