This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes File conversion program... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "File conversion program..." Watch "File conversion program..." New topic
Author

File conversion program...

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Hi All, Hopefully one of you can help me, I think i've got to the stage where i've been staring at the code too long to miss an obvious problem..

I want to read in a binary file, look for all occurances of hex 0x00 and change those bytes to hex 0xFF.

My problem is, the resulting file is 'always' large than the original file.. It appears the last buffered read, is being written twice and I dont understand why.. my code is as follows..

Thanks in advance..



Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That int that in.readBuffer returns ... you test it for -1 to detect EOF. See if you don't need to keep it and use it in building the String.

And does char -> String -> char work out reliably for you? I rarely concern myself with the fact that String isn't just ASCII characters, but it isn't.
[ February 02, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Quang Pham
Ranch Hand

Joined: Nov 29, 2005
Posts: 47
Originally posted by Dave Brown:
Hi All, Hopefully one of you can help me, I think i've got to the stage where i've been staring at the code too long to miss an obvious problem..

I want to read in a binary file, look for all occurances of hex 0x00 and change those bytes to hex 0xFF.

My problem is, the resulting file is 'always' large than the original file.. It appears the last buffered read, is being written twice and I dont understand why.. my code is as follows..

Thanks in advance..


Hi
How about take in one byte at a time (byte[] buffer = new byte[1] , compare read character with the old character, perform the change if there is a match. After your program ran, you may be able to see what's going on.
good luck.
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
I discovered that the line..

s = new String(buffer); would always return a string the length of the buffer... So on the last and final read, even though the number of bytes read was less than the buffer size.. the string was 1024 bytes.

So a quick check if the lengths differ, and a substring() call sorted that out.

Thanks for the replies..
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look through the String constructors. There's one that will save you the test and substring bits. I'm deliberately not quite giving the answer cause it's so much fun to go discover it.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: File conversion program...
 
Similar Threads
replace() method in strings.......
Help! Problem with Encryption!
Java nio
how to encrypt data into an image
Confusion with Binary, Byte representations