nio

When and how are NIO direct buffers freed?

I have a C library that wants a temporary buffer for scratch space. I'm considering passing the address of a direct byte buffer to it. Is the VM ever allowed to relocate the buffer before it is ultimately freed? The native library will be holding on to the pointer after the JNI frame goes away. My understanding is that JNI local objec...

Java: Converting String to and from ByteBuffer and associated problems

I am using Java NIO for my socket connections, and my protocol is text based, so I need to be able to convert Strings to ByteBuffers before writing them to the SocketChannel, and convert the incoming ByteBuffers back to Strings. Currently, I am using this code: public static Charset charset = Charset.forName("UTF-8"); public static Cha...

Java: Is SelectionKey.attach() broken?

In my implementation of Java NIO I have not been able to get SelectionKey.attach() to work. Basically, once clients connect (OP_ACCEPT interest ops) I add them to a map where their IP address maps to an object that maintains state for the client. Then, when an OP_READ occurs, I again retrieve the client's IP address, and this time get ...

Java NIO: reading variable-sized blocks

I would like to read a string from a TCP stream that is given with a byte length followed by the actual data. In Python, I would do length = ord(stream.read(1)) data = stream.read(length) How do I do the same in Java NIO? What I have is a buffer (of capacity 257) stream.read(buffer); // cannot specify a size here int length = buffer....

Selector.close throwing a java.util.ConcurrentModificationException on AIX platforms

Hi, I am using java nio selector, and seem to hit the following issue randomly but consistantly in my application while calling the selector.close. The selector object is being accessed by a single thread in my application. The same application works fine on Solaris, Linux and Windows. I feel that this is an issue is with the AIX...

Multi-file gather using selectable file channels

I implemented a multi-file gatherer input stream which is able to read and combine data from previously scattered file segments. However, the current version uses RandomAccessFile along with fixed size ExecutorService, and with lots of CPU overhead. I'd like to avoid these I/O threads and perform a single threaded Selector based approach...

Java nio, checking if a channel is ready for read, write

Instead of using a selector in the usual way, where for each ready channel a message can be determined and sent, I'd like to choose whichever connection is currently ready and send a message there. Presumably this can be done by throwing all the channels into a selector for reading and marking them ready for reading if they come out, t...

How to send and receive serialized object in socket channel

I want to transmit a serialized object over a socket channel. I want make "Hi friend" string as serialized object and then write this object in socket channel while in the other end i want to read the same object and retrieve the data. All these things I want to do using Java SocketChannel. How to do this? I have tried like below, but d...

Best model for an NIO implementation?

I'm in the process of converting our java code to use NIO, but I'm not sure of the best way to design it. My initial approach was to create a pool of selector threads. The threads are started/killed as needed, and channels are registered to a selector thread when they are connected/accepted in a round-robin fashion. From there, each thr...

NIO Server Reading Once

I have a problem where I am able to read from any connection only once and then never again. When I select I do basically the following in a while(true) loop: if(selector.select( 500 ) == 0) continue; for(SelectionKey sk : selector.keys()) { // if (sk != anyOtherSk()) { obj = createAnObject(sk); } // obj.readChannel()...

Java Selector NIO Reading problem

Relevant Code -- Note Instructions is merely a class with several methods which will operate on the data. A new thread is created operate on the data read. READ THREAD: while(true) { System.out.println("."); if(selector.select(500) == 0) continue; System.out.println("processing read"); for(SelectionKey s...

Java Serializable, ObjectInputstream, Non-blocking I/O

I'm just starting out with Java serialization, and I'm not clear on how you are supposed to get objects from a source in a scenario with non-blocking I/O . All the documentation I can find suggests using ObjectInputStream is the proper way to read in serialized objects. However, as I mentioned I'm using java.nio and performing non-block...

NIO - Detecting a closed connection

I have written a server that performs non-blocking IO using the Java NIO API. I'm seeing a situation whereby occassionally a client application shuts down abruptly (e.g. due to power loss) and the connection is left open on the server-side. A colleague has encountered the same problem and said he uses heartbeats over the line to detect...

Is there any Non-blocking IO open source implementation for Scala's actors?

I have quite large files that I need to deal with (500Meg+ zip files). Are there any non-blocking IO open source implementations for Scala's actors? ...

Should thread blocked by java NIO Selector.select() be considered waiting or running

The documentation of selectorObj.select() method states This method performs a blocking selection operation. It returns only after at least one channel is selected, this selector's wakeup method is invoked, or the current thread is interrupted, whichever comes first. I understand the documentation The thread that is b...

How to redirect all console output to a Swing JTextArea/JTextPane with the right encoding?

Hi, I've been trying to redirect System.out PrintStream to a JTextPane. This works fine, except for the encoding of special locale characters. I found a lot of documentation about it (see for ex. mindprod encoding page), but I'm still fighting with it. Similar questions were posted in StackOverFlow, but the encoding wasn't addressed as...

Java: Generalization possible between: streams, readers, char buffers, stringbuilder, ...?

Hello everyone! Background story: There are these Source and Result interfaces for XML. These are adapters between different XML technologies in Java. Instances of these classes represent DOM, SAX, JAXB, XML streams, XML events (and even more?). The question: So, is there something comparable for plain old strings? Some generalizatio...

Java NIO UDP - Selector Hangs when trying to register

final DatagramChannel dc = DatagramChannel.open(); dc.configureBlocking(false); dc.socket().bind(localAddress); final SelDatagramChannel c = new SelDatagramChannel(dc, datagramListener); --->>>> final SelectionKey sk = dc.register(selector, SelectionKey.OP_READ); The market line hangs forever when you try...

Java NIO FileChannel versus FileOutputstream performance / usefulness

Hello, I am trying to figure out if there is any difference in performance (or advantages) when we use nio FileChannel versus normal FileInputStream/FileOuputStream to read and write files to filesystem. I observed that on my machine both perform at the same level, also many times the FileChannel way is slower. Can I please know more de...

java.nio.BufferUnderflowException when processing files in Scala

I got a similar problem to this guy while processing 4MB log file. Actually I'm processing multiple files simultaneously but since I keep getting this exception, I decide to just test it for a single file: val temp = Source.fromFile("./datasource/input.txt") val dummy = new PrintWriter("test.txt") var itr = 0 println("Default Buffer siz...