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

DataInputStream.read{Int,Short} should consider using readFully

    XMLWordPrintable

Details

    • Fix Understood
    • sparc
    • solaris_10

    Description

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.6.0-rc-b95)

      A DESCRIPTION OF THE PROBLEM :
      readLong uses an internal byte array and reads all of its data for a long in one method call. In readFully it attempts to read all of the data at once. readShort and readInt on the other hand read each byte of their target primitive one at a time. Reading all of their data at once should be considered for to make them more performant.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run this test, see the times it takes to read the data. Run it with reimplemented readInt and readShort and see the timing differences.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      readInt and readShort not to do separate reads for each byte
      ACTUAL -
      readInt and readShort do separate reads

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.util.*;

      public class TestDataInputStream{

          public static void main(String ... args) throws Exception{
              int mult = 6;
              File f= new File("testInt.data");
              FileOutputStream fos = new FileOutputStream(f);
              DataOutputStream dos = new DataOutputStream(fos);
              Random r = new Random();
              int[] values = new int[Short.MAX_VALUE * mult];
              for(int i = 0; i < Short.MAX_VALUE * mult; i++){
                  values[i] = r.nextInt();
      dos.writeInt(values[i]);
              }
              dos.close();

              System.out.println("INT TEST: Starting to read...");
              FileInputStream fis = new FileInputStream(f);
              DataInputStream dis = new DataInputStream(fis);
              int[] read = new int[Short.MAX_VALUE * mult];
              long start = System.currentTimeMillis();
              for(int i = 0; i < Short.MAX_VALUE * mult; i++)
                  read[i] = dis.readInt();
              long end = System.currentTimeMillis();
              dis.close();
              System.out.println("TIME: " + (end - start));
              for(int i = 0; i < Short.MAX_VALUE * mult; i++)
                  assert(values[i] == read[i]): "not equal ";

              f= new File("testShort.data");
              fos = new FileOutputStream(f);
              dos = new DataOutputStream(fos);
              short[] svalues = new short[Short.MAX_VALUE * mult];
              for(int i = 0; i < Short.MAX_VALUE * mult; i++){
                  svalues[i] = (short)r.nextInt(Short.MAX_VALUE);
      dos.writeShort(svalues[i]);
              }
              dos.close();

              System.out.println("SHORT TEST: Starting to read...");
              fis = new FileInputStream(f);
              dis = new DataInputStream(fis);
              short[] sread = new short[Short.MAX_VALUE * mult];
              start = System.currentTimeMillis();
              for(int i = 0; i < Short.MAX_VALUE * mult; i++)
                  sread[i] = dis.readShort();
              end = System.currentTimeMillis();
              dis.close();
              System.out.println("TIME: " + (end - start));
              for(int i = 0; i < Short.MAX_VALUE * mult; i++)
                  assert(svalues[i] == sread[i]): "not equal ";

          }
      }
      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: