java.nio.*

A

ak

am I the only one to say that the java.nio. is bullshit?

i have been trying to write some code using it to transfer file over the
net.

small file it is ok, big file -- problematic.

java.lang.OutOfMemoryError
at java.nio.Bits.reserveMemory(Unknown Source)
at java.nio.DirectByteBuffer.<init>(Unknown Source)
at java.nio.ByteBuffer.allocateDirect(Unknown Source)
at sun.nio.ch.Util.getTemporaryDirectBuffer(Unknown Source)
 
S

S. Balk

am I the only one to say that the java.nio. is bullshit?

am I the only one to say that the way you use the java.nio. is bullshit?

ok... you are trying to put a large file in memory, and the memory turns out
it isn't big enough. that's no problematic, that's what you should expect.
 
T

Tor Iver Wilhelmsen

ak said:
java.lang.OutOfMemoryError
at java.nio.Bits.reserveMemory(Unknown Source)
at java.nio.DirectByteBuffer.<init>(Unknown Source)
at java.nio.ByteBuffer.allocateDirect(Unknown Source)
at sun.nio.ch.Util.getTemporaryDirectBuffer(Unknown Source)

What is your heap setting? You did remember to pass a large enough
value to the -Xmx option when running you app, yes?
 
J

Jordan Zimmerman

There are bugs with nio in the 1.4.2 release. You need to go back to 1.4.1
or wait for a fix. Check the Bug Parade.
 
I

iksrazal

ak said:
am I the only one to say that the java.nio. is bullshit?

i have been trying to write some code using it to transfer file over the
net.

small file it is ok, big file -- problematic.

java.lang.OutOfMemoryError
at java.nio.Bits.reserveMemory(Unknown Source)
at java.nio.DirectByteBuffer.<init>(Unknown Source)
at java.nio.ByteBuffer.allocateDirect(Unknown Source)
at sun.nio.ch.Util.getTemporaryDirectBuffer(Unknown Source)

Two things:

1) This most likely is a heap problem. Try something like:

java -Xmx128m

Try both sides if neccesary.

2) The real advantage of nio is for sockets - previously you needed
one thread per connection.

iksrazal
 
B

Brian S O'Neill

I've played around with both the nio file APIs and socket APIs. Though
functional, they are buggy and much harder to use than they really need to
be. For memory mapped file I/O, I wrote my own simple native interface that
uses good ol' byte arrays. It's much simpler to use and I found it to be
faster too. For sockets I took the same approach, again with the same
approach. Easier and faster.

The problem with nio is that it is way overdesigned and imposes special
restrictions on how it is to be used. ByteBuffers, while convient when
writing certain kinds of applications, is a total PITA for anything else.
Try to implement a database or persistent hashtable using nio memory mapped
files. You'll find that sometimes the auto-moving cursor in the ByteBuffers
isn't the desired behavior. Try profiling the app and you'll see tons of
temporary objects being created as well as other useless work being
performed. ByteBuffers should have been designed as a higher level API. Byte
arrays offer much more flexibility.

In my opinion, nio is only good at writing only one application: a web
server. Open FileChannel, read chunks into direct ByteBuffer, flip, spew
contents out a SocketChannel. Even then, it won't scale that well on
platforms that support asyc socket I/O that has advanced beyond the
select/poll model.

It's also a pity that they rewrote the old socket API to use nio internally.
My applications that rely on the old sockets crash under load when running
all versions of jdk1.4. Jdk1.3.x is stable.

Whille nio is better than nothing, don't expect anything better to ever come
bundled with the jdk. We're stuck with it for the remaining lifetime of the
Java platform, and it sucks.
 
T

Thomas G. Marshall

iksrazal <[email protected]> horrified us with:

....[snippity doo dah]...
2) The real advantage of nio is for sockets - previously you needed
one thread per connection.

No, the real advantage of nio is in not having to use the ridiculous
decorator pattern x levels deep. It was both slow /and/ confusing. nio
fixes all that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top