nio

How to avoid OutOfMemoryError when using Bytebuffers and NIO?

I'm using ByteBuffers and FileChannels to write binary data to a file. When doing that for big files or successively for multiple files, I get a OutOfMemoryError exception. I've read elsewhere that using Bytebuffers with NIO is broken and should be avoided. Does any of you already faced this kind of problem and found a solution to effici...

Why do SocketChannel writes always complete for the full amount even on non-blocking sockets?

Using the Sun Java VM 1.5 or 1.6 on Windows, I connect a non-blocking socket. I then fill a ByteBuffer with a message to output, and attempt to write() to the SocketChannel. I expect the write to complete only partially if the amount to be written is greater than the amount of space in the socket's TCP output buffer (this is what I exp...

Java: Are concurrent reads and writes possible on a blocking SocketChannel via Object(In|Out)putStreams?

I created an ObjectInputSteam and ObjectOutputStream on a blocking SocketChannel and am trying to read and write concurrently. My code is something like this: socketChannel = SocketChannel.open(destNode); objectOutputStream = new ObjectOutputStream(Channels.newOutputStream(socketChannel)); objectInputStream = new ObjectInputStream(Chan...

Java NIO select() returns without selected keys - why?

In writing some test code I have found that Selector.select() can return without Selector.selectedKeys() containing any keys to process. This is happening in a tight loop when I register an accept()ed channel with SelectionKey.OP_READ | SelectionKey.OP_CONNECT as the operations of interest. According to the docs, select() should retur...

java.net versus java.nio

At what point is it better to switch from java.net to java.nio? .net (not the Microsoft entity) is easier to understand and more familiar, while nio is scalable, and comes with some extra nifty features. Specifically, I need to make a choice for this situation: We have one control center managing hardware at several remote sites (each s...

when I use nio, serverSocket.accept() throws IllegalBlockingModeException

When I code like this: ServerSocketChannel ssc = ServerSocketChannel.open(); InetSocketAddress sa = new InetSocketAddress("localhost",8888); ssc.socket().bind(sa); ssc.configureBlocking(false); ssc.socket().accept(); the ServerSocket.accept() method throws java.nio.channels.IllegalBlockingModeException. Why can't I call accept(), e...

How to detect a Selector.wakeup call

If I would write: int selectedChannels = selector.select(); Set selectedKeys = selector.selectedKeys(); if ( selectedChannels != selectedKeys.size() ) { // Selector.select() returned because of a call to Selector.wakeup() // so do synchronization. } // Continue with handling selected channels. would it correctly detect the wa...

Advantage of using NIO vs BIO in jetty ?

Any experience on the tradeoffs in terms of nio vs bio when having heap and computationally intensive process executed for every query with execution times in (100ms-900ms) range ? ...

When using Java's FileLock, is it ok to let close() to automatically do a lock.release()?

As most should know close() also closes any streams uses. This allows the follow code: BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(...))); ... br.close(); This is nice, since we don't need a reference to FileInputStream and remember to close it. But does it also work for FileLocks? final FileInp...

Java NIO: Sending large messages quickly leads to truncated packets and data loss

I've got this nasty problem where sending multiple, large messages in quick succession from a Java (NIO) server (running Linux) to a client will lead to truncated packets. The messages have to be large and sent very rapidly for the problem to occur. Here's basically what my code is doing (not actual code, but more-or-less what's happen...

FileChannel & RandomAccessFile don't seem to work

To put it simple: a swing app that uses sqlitejdbc as backend. Currently, there's no problem launching multiple instances that work with the same database file. And there should be. The file is locked (can't delete it while the app is running) so the check should be trivial. Turns out not. File f = new File("/path/to/file/db.sqlite"...

How to get Maximum bytes from a port using a serversocketchannel

hi guys, I'm developing a multiple port reading application using nio package.Actually these ports recieve packets continously.Using nio package we have to reAD packets using channels.There should not be a packet loss.Here is my code.I've confusion in else if ((key.readyOps() & SelectionKey.OP_READ)== SelectionKey.OP_RE...

Non-blocking UDP I/O vs blocking UDP I/O in Java

Non-blocking TCP/IP SocketChannels and Selector in NIO help me to handle many TCP/IP connections with small number of threads. But how about UDP DatagramChannels? (I must admit that I'm not very familiar with UDP.) UDP send operations don't seem to block even if the DatagramChannel is not operating in blocking mode. Is there really a...

transferring bytes from one ByteBuffer to another

What's the most efficient way to put as many bytes as possible from a ByteBuffer bbuf_src into another ByteBuffer bbuf_dest (as well as know how many bytes were transferred)? I'm trying bbuf_dest.put(bbuf_src) but it seems to want to throw a BufferOverflowException and I can't get the javadocs from Sun right now (network problems) when I...

What are some tips for processing large files in Java

I need to perform a simple grep and other manipulations on large files in Java. I am not that familiar with the Java NIO utilities, but I am assuming that is what I need to use. What resources or helpful tips do you have for reading/writing large files. Also, I am working on a SWT application and need to display parts of that data wit...

Extending ByteBuffer class

Is there any way to create class that extends ByteBuffer class? Some abstract methods from ByteBuffer are package private, and if I create package java.nio, security exception is thrown. I would want to do that for performance reasons - getInt for example has about 10 method invocations, as well as quite a few if's. Even if all checks ...

Any NIO frameworks for .NET?

Are there any non-blocking IO frameworks for .NET? I am looking for something similar to what Apache Mina and JBoss Netty provides for Java: a framework for implementing highly scalable servers - not just the low-level support that the .NET framework provides. EDIT: To better explain what I would like to see, here is a basic example o...

Binary search in a sorted (memory-mapped ?) file in Java

I am struggling to port a Perl program to Java, and learning Java as I go. A central component of the original program is a Perl module that does string prefix lookups in a +500 GB sorted text file using binary search (essentially, "seek" to a byte offset in the middle of the file, backtrack to nearest newline, compare line prefix with ...

How to determine if a file will be logically moved or physically moved.

The facts: When a file is moved, there's two possibilities: The source and destination file are on the same partition and only the file system index is updated The source and destination are on two different file system and the file need to be moved byte per byte. (aka copy on move) The question: How can I determine if a file will ...

How do I open 20000 clients in Java without increasing file limit?

Whenever I open a socket channel. If the client accepts then 1 file descriptor is created internally so I can create a maximum of 1024 clients in Linux. But I want to create more clients without increasing file descriptor limit in Linux (ulimit -n 20000) So how can I create more sockets in Java? ...