Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4364373

JdbcOdbcInputStream's read(byte[], int, int) method returns incorrect count

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • core-libs
    • beta
    • x86
    • windows_nt



      Name: skT45625 Date: 08/21/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      First, we understand that the Jdbc-Odbc bridge is not an officially supported
      product, but since it is distributed with the 1.3 SDK, we decided to log this
      as a bug.

      Second, the source code downloaded from the developer connection shows
      JdbcOdbcInputStream's read(byte[], int, int) method as throwing an IOException,
      but calls to this method does not throw the exception. So, we believe the
      source code distributed is out of sync with the 1.3 SDK.

      Third, the return value of the read(byte[], int, int) method is incorrect. The
      bug appears when you give the read method an offset other than zero. The
      sample program and output appear below. You will need to do some setup work as
      described in the header comment block before running the test.

      -- Sample Program:BEGIN --
      import java.io.InputStream;
      import java.io.IOException;

      import java.net.URL;

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.Statement;
      import java.sql.SQLException;

      /**
       * Compares the <tt>read(byte[], int, int)</tt> of a java.io.BufferedInputStream
       * and a sun.jdbc.odbc.JdbcOdbcInputStream. Both input streams are given
       * "test" as the data.<p>
       *
       * In order to run this test, you will need to create a file called "test.txt",
       * placed in your classpath, containing some text (we used the word "test").
       * Also, you will need to setup a database with an ODBC datasource
       * (Windows NT4.0) named jdbc_test with a table named jdbc_test. The table
       * jdbc_test has two columns:<br>
       * <tt>name varchar(10) NOT NULL</tt><br>
       * <tt>data varchar(50) NULL</tt><br>
       * Add a row where name="test0" and data="test".<p>
       *
       * @author W. Eric Trull (###@###.###)
       */
      public class JdbcOdbcInputStreamTest {
          
          public JdbcOdbcInputStreamTest() {
          }
          
          /**
           * Reads from <tt>inputStream</tt>, one byte at a time, into a byte array
           * buffer.
           *
           * @param inputStream Input stream to read from.
           */
          public void readInputStream(InputStream inputStream) {
              byte[] buffer = new byte[128];
              int bytesRead = 0;

              System.err.println("");
              System.err.println(inputStream.getClass().getName());

              try {
                  int cycle = 0;
                  
                  // read first byte
                  bytesRead = inputStream.read(buffer, cycle++, 1);
                  System.err.println("[" + cycle + "]: Asked for 1 byte...got " +
                      bytesRead);
                  while (bytesRead != -1) {
                      // read next byte. give offset of previously read bytes.
                      bytesRead = inputStream.read(buffer, cycle++, 1);
                      System.err.println("[" + cycle + "]: Asked for 1 byte...got " +
                          bytesRead);
                  }
                  
                  // print out what is in buffer.
                  System.err.println("-- Start --");
                  System.err.print(new String(buffer));
                  System.err.println("-- End --");
              }
              catch (IOException ioException) {
                  System.err.println("" + ioException);
              }
          }
          
          /**
           * Gets a URL to "test.txt", opens an input stream, and then calls
           * <tt>{@link #readInputStream(InputStream)}</tt> passing the input stream.
           *
           * @see #readInputStream(InputStream)
           */
          public void fileTest() {
              try {
                  URL url = this.getClass().getClassLoader().getResource("test.txt");
                  
                  if (url != null) {
                      InputStream inputStream = url.openStream();

                      readInputStream(inputStream);
                  }
                  else {
                      System.err.println("Failed to get test.txt file.");
                  }
              }
              catch (Exception exception) {
                  System.err.println("" + exception);
              }
              
          }
          
          /**
           * Connects to a database via JDBC, executes a select statment, gets an
           * input stream to the first column, and then calls
           * <tt>{@link #readInputStream(InputStream)}</tt> passing the input stream.
           *
           * @see #readInputStream(InputStream)
           */
          public void jdbcTest() {
              try {
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              }
              catch (ClassNotFoundException classNotFoundException) {
                  System.err.println("" + classNotFoundException);
                  return;
              }
              
              try {
                  String sql = "SELECT data FROM jdbc_test WHERE name='test0'";
                  Connection connection = null;
                  Statement statement = null;
                  ResultSet resultSet = null;

                  connection = DriverManager.getConnection("jdbc:odbc:jdbc_test");
                  statement = connection.createStatement();
                  statement.execute(sql);
                  resultSet = statement.getResultSet();
                  
                  boolean wasNull = resultSet.wasNull();
                  if (!wasNull) {
                      if (resultSet.next()) {
                          InputStream inputStream = resultSet.getAsciiStream(1);

                          readInputStream(inputStream);
                      }
                  }
                  else {
                      System.err.println("SELECT statement returns null.");
                  }

                  resultSet.close();
                  statement.close();
                  connection.close();
              }
              catch (SQLException sqlException) {
                  System.err.println("" + sqlException);
              }
          }


          /**
           * Main method that reads a BufferedInputStream and then a
           * JdbcOdbcInputStream.
           */
          public static void main(String[] args) {
              JdbcOdbcInputStreamTest test = new JdbcOdbcInputStreamTest();
              
              test.fileTest();
              test.jdbcTest();
          }
      }
      -- Sample Program:END --

      -- Sample Program Output:BEGIN --
      java.io.BufferedInputStream
      [1]: Asked for 1 byte...got 1
      [2]: Asked for 1 byte...got 1
      [3]: Asked for 1 byte...got 1
      [4]: Asked for 1 byte...got 1
      [5]: Asked for 1 byte...got 1
      [6]: Asked for 1 byte...got 1
      [7]: Asked for 1 byte...got -1
      -- Start --
      test
      <snipped garbage>-- End --

      sun.jdbc.odbc.JdbcOdbcInputStream
      [1]: Asked for 1 byte...got 1
      [2]: Asked for 1 byte...got -1
      -- Start --
      t<snipped garbage>-- End --
      -- Sample Program Output:END --
      (Review ID: 108719)
      ======================================================================

            jbrucesunw Jonathan Bruce (Inactive)
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: