473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Handling Blocks of Bytes

72
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. :)
Nov 19 '07 #1
18 2898
JosAH
11,448 Expert 8TB
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
Nov 19 '07 #2
KWSW
72
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 :)
Nov 19 '07 #3
KWSW
72
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?
Nov 19 '07 #4
JosAH
11,448 Expert 8TB
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
Nov 19 '07 #5
KWSW
72
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.
Nov 19 '07 #6
JosAH
11,448 Expert 8TB
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
Nov 19 '07 #7
r035198x
13,262 8TB
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?
Nov 19 '07 #8
KWSW
72
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.
Nov 19 '07 #9
r035198x
13,262 8TB
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.
Nov 19 '07 #10
KWSW
72
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:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<byte[]> al = new ArrayList<byte[]>();
  2. byte[] b = new byte[bSize]; 
  3.  
  4. FileInputStream fIn = new FileInputStream(inFile);
  5. BufferedInputStream in = new BufferedInputStream(fIn);                                                                   
  6.  
  7.  
  8. while(in.read(b) != -1)
  9. {
  10.     al.add(b);
  11. }            
  12.  
  13. in.close();
  14.  
  15. FileOutputStream fOut = new FileOutputStream(outFile);
  16. BufferedOutputStream out = new BufferedOutputStream(fOut);                                                                   
  17.  
  18.  
  19. for(int i = 0; i < al.size(); i++)               
  20. {
  21.     out.write(al.get(i));
  22. }
  23.  
  24. out.close();
  25.  
Nov 19 '07 #11
JosAH
11,448 Expert 8TB
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
Nov 19 '07 #12
KWSW
72
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?
Nov 19 '07 #13
r035198x
13,262 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<byte[]> al = new ArrayList<byte[]>();
  2. byte[] b = new byte[bSize]; 
  3.  
  4. FileInputStream fIn = new FileInputStream(inFile);
  5. BufferedInputStream in = new BufferedInputStream(fIn);                                                                   
  6.  
  7.  
  8. while(in.read(b) != -1)
  9. {
  10.     al.add(b);
  11. }            
  12.  
  13. in.close();
  14.  
  15. FileOutputStream fOut = new FileOutputStream(outFile);
  16. BufferedOutputStream out = new BufferedOutputStream(fOut);                                                                   
  17.  
  18.  
  19. for(int i = 0; i < al.size(); i++)               
  20. {
  21.     out.write(al.get(i));
  22. }
  23.  
  24. out.close();
  25.  
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.
Nov 19 '07 #14
KWSW
72
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.
Nov 19 '07 #15
r035198x
13,262 8TB
but now i cant seem to write properly... lol... i check the output file and its the same size as the input file...

.....
[/code]
I thought you were saying that there's a problem because the files are the same.
So you don't have a problem right now?
Nov 19 '07 #16
KWSW
72
I thought you were saying that there's a problem because the files are the same.
So you don't have a problem right now?
i have a problem becos while the file size is the same, the file isnt...

I solved the part about the last byte[] being smaller than the block size so my files are the same size now. But they are not the same file. As in if its a picture file, the output will be corrupted. But it works fine with text
Nov 19 '07 #17
KWSW
72
ok one of those weird days... now its works... -_-'''

anyway thanks again for the help :)
Nov 19 '07 #18
r035198x
13,262 8TB
i have a problem becos while the file size is the same, the file isnt...

I solved the part about the last byte[] being smaller than the block size so my files are the same size now. But they are not the same file. As in if its a picture file, the output will be corrupted. But it works fine with text
See the specs of the read method here.
Nov 19 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
2
by: HackerSpiff | last post by:
From a server process (no display access) I would like to take an image (most likely GIF or JPG) as a blob from a DB and rummage around the pixel values. Are there classes in the .NET framework...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
9
by: Phil Jenson | last post by:
I am try to evaluate the most efficient method of handling thousands of simultaneous TCP connects each of which remain connected to the server for hours and pass a small amount of data usually once...
1
by: michaeltorus | last post by:
Hi I'm currently designing a new web application in .Net. I've pretty covered everything, apart from error handling. There seems to be a few different way to do this, but something I've read...
2
by: equinox1248 | last post by:
Hi, I thought this would be answered several time in this group, but I couldn't find anything relevant. What would be the most efficient way of calculating sum of absolute values of two...
5
by: m.topczewski | last post by:
Hello, I'm a beginner in c++, i have to write a program that will take couple of segments from one wave file, and store them in another wave file to be played back later. Here's the code i have...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.