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

(cs) UnicodeEncoder emits BOM when there are no bytes to encode

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 5.0, 6u11
    • core-libs
    • b123
    • x86, sparc
    • solaris, solaris_2.5.1
    • Verified

      FileWriter object failed to close when UTF-16 encoding used and FileWriter object was initialized with FileDescriptor of closed FileOutputStream object. With UTF-8 encoding test below passed.

      import java.io.*;

      public class TestFW {

          static byte[] bytes = {0, 2, 4, 6, 8};


          /**
           * This method constructs FileWriter with FileDescriptor object of a
           * file which is instantiated, exists or not exists.
           * if exists, it is readable, open, non-empty)
           * Returns the constructed object.
           * @param fd FileDescriptor of the file
           * @return Status Status of the test
           */
          
          public static void main (String[] args) {
              

              try {
                  File tmp = File.createTempFile("test", "fw");
                  FileOutputStream fos = new FileOutputStream(tmp);
                  fos.write(bytes);
                  FileDescriptor fd = fos.getFD();
                  fos.close(); //To pass with UTF-16 comment this line and uncomment fos.close() below
                  System.out.println("fd.valid()=" + fd.valid());
                  System.out.println("Constructing FileWriter( FileDescriptor )");
                  FileWriter fw = new FileWriter(fd);
                  System.out.println("Encoding: " + fw.getEncoding());
                  System.out.println("Constructor successful");
                  fw.close();
                  System.out.println("FileWriter closed");
                  fos.close();//uncomment this to pass with UTF-16
              }
              catch (SecurityException e) {
                  System.out.println("Test bypassed. File creation disallowed due to "
                              +"exception "+e);
                  System.out.println("OKAY");
              }
              catch (IOException e) {
                  e.printStackTrace(System.out);
                  System.out.println("Unexpected exception during flush/close: "+e);
              }

          }
      }

      Output of java -cp. -Dfile.encoding=UTF-16 TestFW with closed descriptor:

         f d . v a l i d ( ) = f a l s e
       C o n s t r u c t i n g F i l e W r i t e r ( F i l e D e s c r i p t o r )
       E n c o d i n g : U T F - 1 6
       C o n s t r u c t o r s u c c e s s f u l
       j a v a . i o . I O E x c e p t i o n : N o s u c h f i l e o r d i r e c t o r y
               a t j a v a . i o . F i l e O u t p u t S t r e a m . w r i t e B y t e s ( N a t i v e M e t h o d )
               a t j a v a . i o . F i l e O u t p u t S t r e a m . w r i t e ( U n k n o w n S o u r c e )
               a t s u n . n i o . c s . S t r e a m E n c o d e r $ C h a r s e t S E . w r i t e B y t e s ( U n k n o w n
         S o u r c e )
               a t s u n . n i o . c s . S t r e a m E n c o d e r $ C h a r s e t S E . i m p l C l o s e ( U n k n o w n
       S o u r c e )
               a t s u n . n i o . c s . S t r e a m E n c o d e r . c l o s e ( U n k n o w n S o u r c e )
               a t j a v a . i o . O u t p u t S t r e a m W r i t e r . c l o s e ( U n k n o w n S o u r c e )
               a t T e s t F W . m a i n ( T e s t F W . j a v a : 3 1 )
       U n e x p e c t e d e x c e p t i o n d u r i n g f l u s h / c l o s e : j a v a . i o . I O E x c e p t i o n
       : N o s u c h f i l e o r d i r e c t o r y

      Output of java -cp. -Dfile.encoding=UTF-8 TestFW with closed descriptor:

      fd.valid()=false
      Constructing FileWriter( FileDescriptor )
      Encoding: UTF8
      Constructor successful
      FileWriter closed

      Output of java -cp. -Dfile.encoding=UTF-16 TestFW with not closed descriptor:

         f d . v a l i d ( ) = t r u e
       C o n s t r u c t i n g F i l e W r i t e r ( F i l e D e s c r i p t o r )
       E n c o d i n g : U T F - 1 6
       C o n s t r u c t o r s u c c e s s f u l
       F i l e W r i t e r c l o s e d

            sherman Xueming Shen
            mismirno Mikhail Smirnov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: