• 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

Searching a FileInputStream

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading in a binary file and searching for key markers to determine what action to take with the data that follows the marker. I'm using a FileInputStream with a buffer to read the file.

My search algorithm was to read a buffer sized chunk (from 64K to 1Mb), search that buffer for my pattern and save how many of the patterns occured in that buffer and repeat the read accumulating the total pattern match count for the file. Changing the buffer size gave me different numbers of pattern matches so I knew I was doing something wrong. My guess was that my pattern (which is 32 bits) was being split across buffer reads so changing buffer size would mean different numbers of matches. I wrote a quick fix that would append the last (pattern_length - 1) bytes from the previous buffer read to the beginning of the current buffer prior to searching for my pattern. This just made things much worse...

I finally just started looking at where the different buffer sizes were finding my pattern and it turns out that all the differences were at the very end of the file where, it appears, the last buffer reads past the end of the file getting garbage data. Disguarding the bad matched at the end gives the correct number of patter matches. I don't understand that...

Don't I have to worry about searching for a pattern when reading a file in buffered chunks like this? Isn't having the pattern split across reads a possibility?

I've attached the code. It's just something I threw together for testing so please don't be too critical....

 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic