How to convert Input Stream to bytes array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Unni Nair
    New Member
    • Jan 2011
    • 5

    How to convert Input Stream to bytes array?

    Hi ,
    I was trying to convert a Input Stream data to a byte array but was not sure , how to proceed. Please help me if you know the best way to get this done
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you can use InputStream to read an array of bytes, e.g.


    if you are having problems post your code

    Comment

    • Unni Nair
      New Member
      • Jan 2011
      • 5

      #3
      this is the code I am using
      java.io.File file = new java.io.File("M yFile.txt");//add a file
      int fileLength = (int) file.length();
      System.out.prin tln("fileLength of asciifile.txt is "+fileLengt h);// create an input stream


      try {
      fin = new java.io.FileInp utStream(file);
      } catch (FileNotFoundEx ception e1) {
      System.out.prin tln("error !!File Not Found");
      throw (new SQLException("e rror !!File Not Found"));
      }


      I want to convert this 'fin' object to a byte array
      the link that you gave me is not opening

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        strange the link works OK for me
        you use it so
        Code:
        java.io.File file = new java.io.File("test.txt");//add a file
        
        try {
            java.io.FileInputStream fin = new java.io.FileInputStream(file);
            byte data[]=new byte[100];
            int size=fin.read(data,0,100);
            for (int i=0; i<size; i++)
             System.out.print(data[i] + " ");
            } 
            catch (FileNotFoundException e1) 
               { System.out.println ("error !!File Not Found");}
            catch (IOException e1) {System.out.println("error !!" + e1);}
        if test.txt is
        Code:
        abcdef
        xyz
        the program gives
        Code:
        97 98 99 100 101 102 10 120 121 122 10
        which are the ASCII codes for the above characters (10 being the RETURN character)

        Comment

        • Unni Nair
          New Member
          • Jan 2011
          • 5

          #5
          the problem here is that my file size is 12994 bytes
          and when I use this code
          try {
          fin = new java.io.FileInp utStream(file);

          byte data[]=new byte[12994];
          int size=fin.read(d ata);
          for (int i=0; i<size; i++)
          System.out.prin t(data[i] + " ");

          } catch (FileNotFoundEx ception e1) {
          System.out.prin tln("error !!File Not Found");
          throw (new SQLException("e rror !!File Not Found"));
          }



          I see the output as:
          just a blank

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            I tried the following with a .exe file
            Code:
            java.io.File file = new java.io.File("t.exe");//add a file
            int fileLength = (int) file.length();
            System.out.println("fileLength of asciifile.txt is "+fileLength);// create an input stream
            try {
                java.io.FileInputStream fin = new java.io.FileInputStream(file);
                byte data[]=new byte[fileLength];
                int size=fin.read(data,0,fileLength);
                System.out.println("size is " + size);
                for (int i=0; i<size; i++)
                 System.out.print(data[i] + " ");
                } 
                catch (FileNotFoundException e1) 
                   { System.out.println ("error !!File Not Found");}
                catch (IOException e1) {System.out.println("error !!" + e1);}
            }
            and the output was
            Code:
            fileLength of asciifile.txt is 25264
            size is 25264
            77 90 -112 0 3 0 0 0 4 0 0 0 -1 -1 0 0 -72 0 0 0 0 0 0 0 64 0 0 0 0 0   .......
            I cut short the line of bytes as it printed 25264 values

            Comment

            • Unni Nair
              New Member
              • Jan 2011
              • 5

              #7
              Thank you that worked for me , for a file of smaller size !!

              Comment

              • Unni Nair
                New Member
                • Jan 2011
                • 5

                #8
                do you know how I can generate this byte array with the ccsid of 37?

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  have a look at the EBCDIC 37 page

                  Comment

                  Working...