Handling Blocks of Bytes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KWSW
    New Member
    • May 2007
    • 72

    Handling Blocks of Bytes

    Just a question on handling "Byte[]" in java.

    I have to read in a file of any type and chop it up into blocks for processing later on.

    Was wondering if I got do a Byte[][] to keep the Byte[] in an array or would something else be a better way of doing it as I do now know how many elements the array of Byte[] can hold as the file might be huge and the blocks might be small.

    Thanks. :)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by KWSW
    Just a question on handling "Byte[]" in java.

    I have to read in a file of any type and chop it up into blocks for processing later on.

    Was wondering if I got do a Byte[][] to keep the Byte[] in an array or would something else be a better way of doing it as I do now know how many elements the array of Byte[] can hold as the file might be huge and the blocks might be small.

    Thanks. :)
    Why don't you use an ArrayList<byte[]> for that? Simply stack up all the byte[]s
    you have read in the ArrayList.

    kind regards,

    Jos

    Comment

    • KWSW
      New Member
      • May 2007
      • 72

      #3
      Originally posted by JosAH
      Why don't you use an ArrayList<byte[]> for that? Simply stack up all the byte[]s
      you have read in the ArrayList.

      kind regards,

      Jos
      ok thanks will give it a try later :)

      Comment

      • KWSW
        New Member
        • May 2007
        • 72

        #4
        oh btw... just to confirm, when i read in the whole file at the first go before chopping them up into blocks, Byte[19] would refer to the 20th byte in the file right?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by KWSW
          oh btw... just to confirm, when i read in the whole file at the first go before chopping them up into blocks, Byte[19] would refer to the 20th byte in the file right?
          Yup, Java (just like C and C++) starts counting from 0, so byte[0] is the first
          byte in the array, byte[1] is the second etc. etc. ArrayLists count the same.

          kind regards,

          Jos

          Comment

          • KWSW
            New Member
            • May 2007
            • 72

            #6
            got another qn though... i would need an integer to define the size of the byte[] array and am worried that if the file is too big, the integer data type cant fit it.

            Wanted to use long but the byte[] can only take in integer. Is there a way around it or the integer data type should be big enough.

            I would assume a 200~300 mb file would be the biggest I ever have to handle.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by KWSW
              got another qn though... i would need an integer to define the size of the byte[] array and am worried that if the file is too big, the integer data type cant fit it.

              Wanted to use long but the byte[] can only take in integer. Is there a way around it or the integer data type should be big enough.

              I would assume a 200~300 mb file would be the biggest I ever have to handle.
              I thought you were going to read that file in chunks? Each chunk in a byte[].

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by KWSW
                got another qn though... i would need an integer to define the size of the byte[] array and am worried that if the file is too big, the integer data type cant fit it.

                Wanted to use long but the byte[] can only take in integer. Is there a way around it or the integer data type should be big enough.

                I would assume a 200~300 mb file would be the biggest I ever have to handle.
                Any reason why you are using an array instead of an ArrayList?

                Comment

                • KWSW
                  New Member
                  • May 2007
                  • 72

                  #9
                  Originally posted by r035198x
                  Any reason why you are using an array instead of an ArrayList?
                  oh ok... haha... got a bit confused just now...

                  ok so lets say i have a file size of 10000 bytes and a block size of 100 bytes.

                  so i would have 100 byte[100] in the arraylist.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by KWSW
                    oh ok... haha... got a bit confused just now...

                    ok so lets say i have a file size of 10000 bytes and a block size of 100 bytes.

                    so i would have 100 byte[100] in the arraylist.
                    Now you've got it.
                    Refer to Jos' first reply.

                    Comment

                    • KWSW
                      New Member
                      • May 2007
                      • 72

                      #11
                      Originally posted by r035198x
                      Now you've got it.
                      Refer to Jos' first reply.
                      but now i cant seem to write properly... lol... i check the output file and its the same size as the input file...

                      my codes:

                      Code:
                      ArrayList<byte[]> al = new ArrayList<byte[]>();
                      byte[] b = new byte[bSize]; 
                      
                      FileInputStream fIn = new FileInputStream(inFile);
                      BufferedInputStream in = new BufferedInputStream(fIn);                                                                   
                      
                      
                      while(in.read(b) != -1)
                      {
                          al.add(b);
                      }            
                      
                      in.close();
                      
                      FileOutputStream fOut = new FileOutputStream(outFile);
                      BufferedOutputStream out = new BufferedOutputStream(fOut);                                                                   
                      
                      
                      for(int i = 0; i < al.size(); i++)               
                      {
                          out.write(al.get(i));
                      }
                      
                      out.close();

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        You're appending the same byte[] b to the array list over and over again. Of course
                        the content of that array changes after every read.

                        kind regards,

                        Jos

                        Comment

                        • KWSW
                          New Member
                          • May 2007
                          • 72

                          #13
                          Originally posted by JosAH
                          You're appending the same byte[] b to the array list over and over again. Of course
                          the content of that array changes after every read.

                          kind regards,

                          Jos
                          it doesn't read the next set of bytes automatically?

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by KWSW
                            but now i cant seem to write properly... lol... i check the output file and its the same size as the input file...

                            my codes:

                            Code:
                            ArrayList<byte[]> al = new ArrayList<byte[]>();
                            byte[] b = new byte[bSize]; 
                            
                            FileInputStream fIn = new FileInputStream(inFile);
                            BufferedInputStream in = new BufferedInputStream(fIn);                                                                   
                            
                            
                            while(in.read(b) != -1)
                            {
                                al.add(b);
                            }            
                            
                            in.close();
                            
                            FileOutputStream fOut = new FileOutputStream(outFile);
                            BufferedOutputStream out = new BufferedOutputStream(fOut);                                                                   
                            
                            
                            for(int i = 0; i < al.size(); i++)               
                            {
                                out.write(al.get(i));
                            }
                            
                            out.close();
                            Why do you expect the input file to have a different size from the source file? You seem to be reading one file into an arraylist and writing the same list's contents to the output file.

                            Comment

                            • KWSW
                              New Member
                              • May 2007
                              • 72

                              #15
                              Originally posted by r035198x
                              Why do you expect the input file to have a different size from the source file? You seem to be reading one file into an arraylist and writing the same list's contents to the output file.
                              what i need to do is to read in a file, chop it up into blocks of a certain size, do some checking to the blocks and write them out if they pass the checks.

                              What I am trying to do now is to make sure that what i read in as blocks are written out properly so the file is still the same.

                              Comment

                              Working...