• 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 Channels == Big Headache

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A note to those unfamiliar with NIO and thinking of using it: Stay away from these things! They've caused me so much headache over the last couple of days. Stick with RandomAccessFiles. I swear, I use the exact same code in some places and sometimes it works, sometimes it doesn't. It's unreliable. It's maddening. I'd be so much further along now if I hadn't tried to incorporate these things.
I've never had these problems using RandomAccessFiles. Sure, it's a bit slower, but who cares. Stick with what works. Save yourselves the headaches.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, I'm suprised to hear you say that. What are you having trouble with? maybe I can help?
M
[ August 15, 2003: Message edited by: Max Habibi ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, unless you want to learn about them for your own benefit. Which I do recommend. But it depends on your goals and time frame - NIO does have a bit of a learning curve; it's not as familiar as one might hope. Once you figure it out, the final code can be fairly simple - it's getting there that can be less than straightforward. Then again, it's not nearly as bad as, say, getting table columns to work correctly. :roll: IMO anyway.
[ August 15, 2003: Message edited by: Jim Yingst ]
 
Rob Zidsen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Max,
I'm just having trouble with the read/write operations sometimes. It seems to work in some places, but in other places it doesn't work. I've tried moving where I created the byte buffer, etc. to geti t to produce the results I want, and sometimes that works, too. Although to be honest that makes me even more concerned.
Here's a snippet of the code that works in most places but isn't working in create():

here's my Random Access File substitution:
 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you write to a file channel the position is moved on by the amount of bytes written. So I think you're moving your position twice(implicitlly and explicitly).
Tony
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Collins:
When you write to a file channel the position is moved on by the amount of bytes written. So you might be moving your position twice(implicitlly and explicitly). i.e you supply an offset to your FileChannel that has already been moved on.
Tony

 
Rob Zidsen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony, why should I care where the FileChannel is pointing? I'm not reading/writing relative to it's current position. I'm using an absolute file position to write the data.
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I can see that now.
Are you testing this with multiple threads, as I believe only writing ByteBuffer is atomic. So the move and write method is not atomic.
I moved to the position required and used write(ByteBuffer[]) to atomically write the whole record and that worked fine. The record is locked so it can't be updated by anything else and dirty reads are avoided as the wrte is atomic
[ August 18, 2003: Message edited by: Tony Collins ]
 
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
Rob, are you reusing that validFlagBuf? If so, you probably need to clear() it after each write().
Also, is your strRecord padded out with spaces or something to ensure it occupies the full record length? If not, then you may still have part of the old record immediately after what you just wrote - the record format wouldn't have any way of telling where the intended end of the record was, so the two would just run together. So make sure there are spaces, or at least one null, at the end of each record.
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah or after you write to the bytebuffer rewind to the start of it, before you use it.
 
Rob Zidsen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help guys. that's the problem I guess. I didn't reset the buffer position to 0.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic