Hello all!

I have some troubles in saving a file by byte chunks, (will use in saving large file size.). My goal is to chunk the file bytes first into a certain size before writing.

I have this code:

FileConnection fc = (FileConnection) Connector.open("file.ext",
					Connector.READ);
InputStream fis = (InputStream) fc.openInputStream();
byte[] fileData = new byte[(int) fc.fileSize()];
fis.read(fileData);

int MAX = 1024;

while(saved < fSize)
{
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	if((fSize - saved) < MAX){
		MAX = fSize - saved;
	}
	for(int i = saved; i<MAX; i++){
		baos.write(fileData[i]);
	}
	fout.write(baos.toByteArray());
	saved += MAX;
}

A file is saved but not complete. I think only 1024 bytes are written. Any suggestions? Explanations?

Your help will be highly appreciated...

Thank you!

Dani AI

Generated

The symptom described by (file saved but truncated) almost always comes from two things: incorrect loop boundaries and assuming an InputStream fills a buffer completely. was right to flag a missing/undefined variable — small mistakes like that often hide the real loop bug. 's Huffman question is a red herring unless compression/transcoding is intended; for binary transfer you must preserve raw bytes.

Use a streaming read/write pattern and always honor the number of bytes actually read. The typical safe loop reads into a fixed buffer, checks the returned length, and writes exactly that many bytes. Do not read the whole file into memory if the file may be large; stream it instead. See the Java I/O docs for the reader/writer semantics: InputStream.read and OutputStream.write.

When uploading in server-sized chunks, send per-chunk metadata (file id, chunk index, total size) and a simple checksum per chunk; the server should append chunks in order (or store and reassemble when all parts arrive) and verify the final checksum. Test the flow with a small binary file first and compare a hash (SHA-256 or MD5) of source vs reassembled file to confirm integrity.

Troubleshooting checklist: confirm no character/text conversions (use binary modes), check the read() return value rather than buffer length, ensure streams are closed/flushed, log bytes-per-chunk during tests, and try a minimal working example before integrating into the full upload flow.

Recommended Answers

All 4 Replies

Are you doing a Huffman Coding project? If not, I don't see the purpose of what you're doing. (In other words, if you aren't doing Huffman Coding, then what do you mean by 'chunk the file bytes into a certain size before writing'? .... I don't see how you'd know what characters they represent once you wanted to open the file for reading again.)

hi,

Thanks for the reply, I have no idea about huffman coding. But, i'll try to explain again what I wana do.

For example, in a certain webserver, there is a limit of filesize that could be uploaded one at a time and you have a file that is greater than this limit.

This is how it looks like:

size of the file you wana send = 2050bytes
limit to webserver upload per transaction = 1024bytes

I wish to send the file with 2050bytes of size, but I cant do it in one time, since there is a limit of 1024bytes.
so,
I wish to send 1024bytes of data first. then,
1024 bytes again and,
2bytes remaining.

How will I do this without any data loss?

Thank you again...

hi,

Thanks for the reply, I have no idea about huffman coding. But, i'll try to explain again what I wana do.

For example, in a certain webserver, there is a limit of filesize that could be uploaded one at a time and you have a file that is greater than this limit.

This is how it looks like:

size of the file you wana send = 2050bytes
limit to webserver upload per transaction = 1024bytes

I wish to send the file with 2050bytes of size, but I cant do it in one time, since there is a limit of 1024bytes.
so,
I wish to send 1024bytes of data first. then,
1024 bytes again and,
2bytes remaining.

How will I do this without any data loss?

Thank you again...

We can see that you solved your problem. Your problem explanation was a solution of what you are trying to say.

fSize isn't defined (as far as I can see).
The problem's probably in the way the loops are constructed, but because you posted your code without tags and proper indentation it's too hard to see.[code=java] tags and proper indentation it's too hard to see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.