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

java.io.DataOutputStream.writeXXX must control overflow of internal counter.

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.2beta4
    • CPU:
      sparc
    • OS:
      solaris_2.5
    • Verification:
      Verified

      Description



      Name: saC57035 Date: 12/03/96


      The DataOutputStream write[XXX] must control overflow of internal counter,
      since all DataOutputStream write[XXX] methods increase internal counter of
      bytes written ( int written ) without check of overflow situation.
      It is assumed that Integer.MAX_VALUE treats like the infinity.


      For example,it is the specification for DataOutputstream write(int):

      The Java Language Specification Version 1.0:

      22.21.3 public void write(int b) throws IOException
      The byte for this operation ... is written
       to the contained output stream. If no exception is thrown, the counter
       written is incremented by 1.

      Implements the write method of OutputStream (§22.15.1).


      _______________________Example ______________________________________
      import java.io.*;

      public class NewDataOutputStream extends DataOutputStream{

        public static void main( String argv[] ) {
         NewDataOutputStream d_out;
            
         d_out = new NewDataOutputStream(System.out); // Create DataOutputStream object


         System.out.println("Some Java-application wrote "+Integer.toString(d_out.size())+" bytes after hard-working.");


         try {
          d_out.writeByte(1); // Write "extra" byte
         System.out.println("One additional byte..");
         } catch (Exception e)
           { System.out.println("Ok. Some exception:"+e);
             return;}

          if (d_out.size()<0) // Check
           { System.out.println("Overflow , but no any exception thrown. Internal counter equals to "+Integer.toString(d_out.size()));
              return;
           }

          System.out.println("No exceptions.No overflows.");

        }

        
        public NewDataOutputStream(OutputStream out) {
          super(out);
          written=Integer.MAX_VALUE; //Initialize internal counter with
                                      // maximum value.
                                      // Emulate that Integer.MAX_VALUE bytes
                                      // been written.
           }
        
        public void write(int b) throws IOException {
         // It does nothing in order to avoid real writing to enywhere.
            }

        public void close() throws IOException {
          
         }
       

      }

      _____________________Output_________________________________________
      Some Java-application wrote 2147483647 bytes after hard-working.
      One additional byte..
      Overflow , but no any exception thrown. Internal counter equals to -2147483648
      ____________________________________________________________________
        
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            zlisunw Zhenghua Li (Inactive)
            Reporter:
            savzan Stanislav Avzan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: