• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File conversion program...

 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Skool. Stay in. Smartness. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic