• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

RandomAccessFile & FileInputStream read

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are RandomAccessFile.read() and FileInputStream.read() related, when the input stream is created using the file descriptor from the random access file?

(I didn't find any clues in the source code.)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene! From testing it seems that the two are indeed related. Reading from the RAF seems to advance the file positoin read by the input stream, and vice versa. However I haven't seen any documentation about this. I would guess that this is undocumented behavior, and so it's probably a good idea to avoid code that uses multiple streams and RAFs referring to the same file simultaneously, unless you find explicit documentation somewhere.
In contrast, if you use a FileChannel obtained by the getChannel() method of RAF or a file input or output stream, you do get some explicit guarantees about its behavior relative to the RAF/stream, as well as relative to other FileChannels. There are still some annoying loopholes IMO in terms of shat's guaranteed here and what's not (see this lengthy discussion if you're curious) - but the situation is certainly better than that you originally described, for which there seem to be no guarantees. Unless I missed someting which is entirely possible. Hope that helps...
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Jim (9990 posts, 10 more to go!).
Uh oh. Wrong forum again. Next time, IO and Streams.
>>avoid code that uses multiple streams and RAFs referring to the same file simultaneously
Yes, that was the reason for the question.
Thank you for the link. I will try to understand the whole thing.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh oh. Wrong forum again. Next time, IO and Streams.
And hey, I didn't even have to say anything. Cool.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic