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

Read binary resource stream differs when run from jar or run from unjarred files

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]


      A DESCRIPTION OF THE PROBLEM :
      Reading binary data from an input stream returned by Class.getResourceAsStream() using read(byte[]) method returns different data than when read byte by byte in a loop when run from a jar.
      With the binary resource I have the last three bytes are incorrect when read from jar.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Build the very simple app provided ( I don't see how to attach my binary data file however).
      Run the application from jar: "java -jar javabug.jar"


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This is the data read by is.read(byte[])
      CRC=2834285511, length=294
      30,82,01,22,30,0D,06,09,2A,86,48,86,F7,0D,01,01,01,05,00,03,82,01,0F,00
      30,82,01,0A,02,82,01,01,00,CE,DA,BA,18,9E,49,25,1D,AD,F8,B4,E9,9E,E3,B4
      7B,99,C8,50,74,C0,39,35,FD,C9,C6,14,4C,AA,88,F6,E5,22,6D,75,7C,DF,22,7E
      42,DD,52,75,9E,0A,53,E1,8F,CC,02,BD,3E,7A,AF,2B,67,DA,E7,43,7E,BA,3D,0E
      B4,6B,6F,8B,F2,F4,5A,E9,6B,DE,5D,6C,F4,3A,1D,95,5B,92,0D,8E,4A,63,5D,8D
      E4,37,CC,6C,92,7F,DB,B0,00,DB,A7,58,97,B1,E6,73,BE,CE,44,04,34,D4,26,3C
      01,EF,21,21,9D,9A,8C,D1,F2,42,FD,48,3C,B9,9E,33,0C,46,45,22,0D,D2,DF,B4
      33,8B,CC,FD,A1,5F,EF,3C,31,C9,7D,5F,1B,F9,9A,71,91,CE,4D,F9,11,C8,4E,CF
      65,74,3C,C4,F4,40,D2,6A,D7,59,CF,45,0B,61,AC,B2,CE,12,15,27,CC,F1,E6,6C
      9F,6A,7C,4C,8C,92,C2,53,01,1A,05,56,24,66,D9,D0,97,BF,EA,78,06,6F,26,4E
      D2,97,08,30,EF,A8,8E,D7,97,39,B3,FE,E0,A9,E5,C7,39,9E,46,06,47,15,2E,E0
      41,FB,5F,D7,76,2F,31,0F,0D,A6,37,C4,4D,F4,1D,AC,EA,EC,CC,6E,D8,49,66,E6
      05,02,03,01,00,01,

      This is the data read byte by byte in a loop
      CRC=2834285511, length=294
      30,82,01,22,30,0D,06,09,2A,86,48,86,F7,0D,01,01,01,05,00,03,82,01,0F,00
      30,82,01,0A,02,82,01,01,00,CE,DA,BA,18,9E,49,25,1D,AD,F8,B4,E9,9E,E3,B4
      7B,99,C8,50,74,C0,39,35,FD,C9,C6,14,4C,AA,88,F6,E5,22,6D,75,7C,DF,22,7E
      42,DD,52,75,9E,0A,53,E1,8F,CC,02,BD,3E,7A,AF,2B,67,DA,E7,43,7E,BA,3D,0E
      B4,6B,6F,8B,F2,F4,5A,E9,6B,DE,5D,6C,F4,3A,1D,95,5B,92,0D,8E,4A,63,5D,8D
      E4,37,CC,6C,92,7F,DB,B0,00,DB,A7,58,97,B1,E6,73,BE,CE,44,04,34,D4,26,3C
      01,EF,21,21,9D,9A,8C,D1,F2,42,FD,48,3C,B9,9E,33,0C,46,45,22,0D,D2,DF,B4
      33,8B,CC,FD,A1,5F,EF,3C,31,C9,7D,5F,1B,F9,9A,71,91,CE,4D,F9,11,C8,4E,CF
      65,74,3C,C4,F4,40,D2,6A,D7,59,CF,45,0B,61,AC,B2,CE,12,15,27,CC,F1,E6,6C
      9F,6A,7C,4C,8C,92,C2,53,01,1A,05,56,24,66,D9,D0,97,BF,EA,78,06,6F,26,4E
      D2,97,08,30,EF,A8,8E,D7,97,39,B3,FE,E0,A9,E5,C7,39,9E,46,06,47,15,2E,E0
      41,FB,5F,D7,76,2F,31,0F,0D,A6,37,C4,4D,F4,1D,AC,EA,EC,CC,6E,D8,49,66,E6
      05,02,03,01,00,01,
      OK - Data match
      Done

      ACTUAL -
      This is the data read by is.read(byte[])
      CRC=3727333734, length=294
      30,82,01,22,30,0D,06,09,2A,86,48,86,F7,0D,01,01,01,05,00,03,82,01,0F,00
      30,82,01,0A,02,82,01,01,00,CE,DA,BA,18,9E,49,25,1D,AD,F8,B4,E9,9E,E3,B4
      7B,99,C8,50,74,C0,39,35,FD,C9,C6,14,4C,AA,88,F6,E5,22,6D,75,7C,DF,22,7E
      42,DD,52,75,9E,0A,53,E1,8F,CC,02,BD,3E,7A,AF,2B,67,DA,E7,43,7E,BA,3D,0E
      B4,6B,6F,8B,F2,F4,5A,E9,6B,DE,5D,6C,F4,3A,1D,95,5B,92,0D,8E,4A,63,5D,8D
      E4,37,CC,6C,92,7F,DB,B0,00,DB,A7,58,97,B1,E6,73,BE,CE,44,04,34,D4,26,3C
      01,EF,21,21,9D,9A,8C,D1,F2,42,FD,48,3C,B9,9E,33,0C,46,45,22,0D,D2,DF,B4
      33,8B,CC,FD,A1,5F,EF,3C,31,C9,7D,5F,1B,F9,9A,71,91,CE,4D,F9,11,C8,4E,CF
      65,74,3C,C4,F4,40,D2,6A,D7,59,CF,45,0B,61,AC,B2,CE,12,15,27,CC,F1,E6,6C
      9F,6A,7C,4C,8C,92,C2,53,01,1A,05,56,24,66,D9,D0,97,BF,EA,78,06,6F,26,4E
      D2,97,08,30,EF,A8,8E,D7,97,39,B3,FE,E0,A9,E5,C7,39,9E,46,06,47,15,2E,E0
      41,FB,5F,D7,76,2F,31,0F,0D,A6,37,C4,4D,F4,1D,AC,EA,EC,CC,6E,D8,49,66,E6
      05,02,03,00,00,00,

      This is the data read byte by byte in a loop
      CRC=2834285511, length=294
      30,82,01,22,30,0D,06,09,2A,86,48,86,F7,0D,01,01,01,05,00,03,82,01,0F,00
      30,82,01,0A,02,82,01,01,00,CE,DA,BA,18,9E,49,25,1D,AD,F8,B4,E9,9E,E3,B4
      7B,99,C8,50,74,C0,39,35,FD,C9,C6,14,4C,AA,88,F6,E5,22,6D,75,7C,DF,22,7E
      42,DD,52,75,9E,0A,53,E1,8F,CC,02,BD,3E,7A,AF,2B,67,DA,E7,43,7E,BA,3D,0E
      B4,6B,6F,8B,F2,F4,5A,E9,6B,DE,5D,6C,F4,3A,1D,95,5B,92,0D,8E,4A,63,5D,8D
      E4,37,CC,6C,92,7F,DB,B0,00,DB,A7,58,97,B1,E6,73,BE,CE,44,04,34,D4,26,3C
      01,EF,21,21,9D,9A,8C,D1,F2,42,FD,48,3C,B9,9E,33,0C,46,45,22,0D,D2,DF,B4
      33,8B,CC,FD,A1,5F,EF,3C,31,C9,7D,5F,1B,F9,9A,71,91,CE,4D,F9,11,C8,4E,CF
      65,74,3C,C4,F4,40,D2,6A,D7,59,CF,45,0B,61,AC,B2,CE,12,15,27,CC,F1,E6,6C
      9F,6A,7C,4C,8C,92,C2,53,01,1A,05,56,24,66,D9,D0,97,BF,EA,78,06,6F,26,4E
      D2,97,08,30,EF,A8,8E,D7,97,39,B3,FE,E0,A9,E5,C7,39,9E,46,06,47,15,2E,E0
      41,FB,5F,D7,76,2F,31,0F,0D,A6,37,C4,4D,F4,1D,AC,EA,EC,CC,6E,D8,49,66,E6
      05,02,03,01,00,01,
      FAIL - Data mismatch
      Done


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class JavaBugMain {

         public static void main( String[] args ) {
            try {
               // read data from resource using InputStream.read(byte [])
               InputStream is = JavaBugMain.class.getResourceAsStream("data.bin");
               byte[] data0 = new byte[is.available()];
               is.read(data0);
               is.close();
               long crc0 = dumpData( "This is the data read by is.read(byte[])", data0 );

               // now repeat, reading data from resource byte by byte in a loop
               is = JavaBugMain.class.getResourceAsStream("data.bin");
               byte[] data1 = new byte[is.available()];
               for( int i = 0; i < data1.length; i++ ) {
                  data1[i] = (byte)is.read();
               }
               is.close();
               System.out.println();
               long crc1 = dumpData( "This is the data read byte by byte in a loop", data1 );

               System.out.println(crc0 == crc1 ? "OK - Data match" : "FAIL - Data mismatch");
               System.out.println("Done");
            }
            catch( Exception e) {
               System.out.println("Something went wrong: " + e.getMessage());
            }
         }

         static long dumpData( String header, byte[] data ) {
            System.out.println(header);
            CRC32 crc = new CRC32();
            // print length and crc
            crc.update(data);
            System.out.println("CRC=" + crc.getValue() + ", length=" + data.length);

            // dump data
            for( int i = 0; i < data.length; i++ ) {
               System.out.print( String.format("%02X%s",Byte.toUnsignedInt(data[i]),(i + 1) % 24 == 0 ? System.lineSeparator() : ","));
            }
            System.out.println();
            return crc.getValue();
         }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Unzip the jar and run the main class directly: "java JavaBugMain"

            aroy Abhijit Roy (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: