Has anyone come up with a good way to use Xerces with java.nio file
locking? In particular I'm trying to get it to work with SAX parsing.

So okay, the DOM and SAX APIs annoyingly don't support nio, but nio and io
aren't incompatible. Here's what I tried:

I created a RandomAccessFile, from that I got a FileDescriptor, and with
that I created a FileInputStream. Also from RandomAccessFile I got a
Channel, and that I was able to lock it for exclusive access. I then took
my FileInputStream and handed it off to the XMLReader for parsing. My
intent was to keep the file exclusively locked beyond the parsing phase
during other operation of application. However XMLReader (or SAXParser)
takes it upon itself to close the stream as when they are done reading it.
This effectively terminates the lock.

(As a side note, why does it do this? Is this required by the spec or is
this specific to Xerces' implementation of it? Isn't it generally
considered bad design for an object or method to close resources it did not
open? I vaguely recall having seen complaints about this before on this
mailing list but don't recall the resolution and can't get to the archive.
Yes, I could subclass FileInputStream to capture the close, but that seems
like such a hack.)

I also realize I can read the file into a buffer without closing the file
stream, and pass the parser a new stream created from the buffer. But
loading the whole document into memory negates some of the benefits of SAX
parsing.

I'll resort to one of these workarounds if I have to, but am hoping there
is some better solution. Like is there a way to get the SAX parser to NOT
close the stream? Or is there a better way than what I describe above to
mix nio and stream functions (I'm a new to nio, so if I'm doing something
something naive here, please tell me).

Thanks,

Eric



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to