nio

StAX parsing from Java NIO channel

I am attempting to receive a stream of XML events over a Java NIO channel. I am new to both NIO and StAX parsing, so I could very easily be overlooking something :) My search has led me to several SAX and StAX implementations, but they all seem to operate on InputStreams and InputSources--not NIO channels. The two closest attempts I hav...

BufferedReader for large ByteBuffer?

Is there a way to read a ByteBuffer with a BufferedReader without having to turn it into a String first? I want to read through a fairly large ByteBuffer as lines of text and for performance reasons I want to avoid writing it to the disk. Calling toString on the ByteBuffer doesn't work because the resulting String is too large (it throws...

JAVA NIO ByteBuffer allocatation to fit largest dataset?

I'm working on an online game and I've hit a little snag while working on the server side of things. When using nonblocking sockets in Java, what is the best course of action to handle complete packet data sets that cannot be processed until all the data is available? For example, sending a large 2D tiled map over a socket. I can think...

thread is stuck while registering channel with selector in Java NIO server.

I have a basic question. Why and how SelectableChannel's register method can be in blocking call. Let me provide a scenario. I have created a Selector object in class Register as follows. private static Selector selector = Selector.open(); I also have a method in same class(Register) to register the channel with the selector. ...

static selector between threads on nio server

I need to implement a server in which has only one selector(static); multiple threads try to register channel to the same static selector. I tried to implement the server, but the problem is that the static selector works for first time i.e. registers the channel; but on next call to register different channel the thread gets hanged. I...

java.net.ConnectException: Connection refused when SocketChannel.open is invoked

Hi, I am trying to write a simple program to open a socket channel to a local address. I get a connection refused exception whenever I run this program import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; public class testSocket { public static void main(String [] args) { ...

In Java, what is the best/safest pattern for monitoring a file being appended to?

Someone else's process is creating a CSV file by appending a line at a time to it, as events occur. I have no control over the file format or the other process, but I know it will only append. In a Java program, I would like to monitor this file, and when a line is appended read the new line and react according to the contents. Ignore ...

help needed in selector issue nio, threading

I need to implement a server in which has only one selector(static); multiple threads try to register channel to the same static selector. I tried to implement the server, but the problem is that the static selector works for first time i.e. registers the channel; but on next call to register different channel the thread gets hanged. I...

SocketChannel in Java sends data, but it doesn't get to destination application

Hi Everybody, I'm suffering a lot to create a simple ChatServer in Java, using the NIO libraries. Wonder if someone could help me. I am doing that by using SocketChannel and Selector to handle multiple clients in a single thread. The problem is: I am able to accept new connections and get it's data, but when I try to send data back, the...

Java - Multiple selectors in multiple threads for nonblocking sockets

Hi there, I'm writing a Java application that will instantiate objects of a class to represent clients that have connected and registered with an external system on the other side of my application. Each client object has two nested classes within it, representing front-end and back-end. the front-end class will continuously receive da...

Java NIO framework for filesystems instead of networks?

There are several high quality frameworks that hide the complexity of NIO based network programming (mina, netty, grizzly, etc.). Are there similar frameworks that simplify NIO based file-system programming? For example, as a learning exercise, I would like to implement a disk backed Map based on this (awesome!) article: http://www.jav...

Use Grizzly transparently under the hood?

Is there a way to bind grizzly dynamic/transparent to my Java Applications (maybe by using spring)? Or is the IO handling part of the Web Container (eg. Glassfish)? ...

Java NIO Threading issue with SocketChannel.write()

Sometimes, while sending a large amount of data via SocketChannel.write(), the underlying TCP buffer gets filled up, and I have to continually re-try the write() until the data is all sent. So, I might have something like this: public void send(ByteBuffer bb, SocketChannel sc){ sc.write(bb); while (bb.remaining()>0){ Thread...

Why are we getting ClosedByInterruptException from FileChannel.map in Java 1.6?

A customer of ours complains that, sporadically, calls of ours to FileChannel.map fail with a ClosedByInterruptException. The Javadoc does not list this as a legitimate possibility. Does anyone know what might be going on here? Cause0: java.nio.channels.ClosedByInterruptException Cause0-StackTrace: at java.nio.channels.spi.AbstractInte...

How to make repeatable unit tests of code that works with Thread.interrupt?

Consider some code that calls, say, file = new RandomAccessFile(x, "r"); file.getChannel().map(....) and wants to handle a ClosedByInterruptException circumstance. I'm stumped by how to make a realistic, repeatable unit test. To make a test, I need to somehow synchronize this thread with some other thread, and cause the other thre...

Java NIO: What does IOException: Broken pipe mean?

For some of my Java NIO connections, when I have a SocketChannel.write(ByteBuffer) call, it throws an IOException: "Broken pipe". What causes a "broken pipe", and more importantly, is it possible to recover from that state? If it cannot be recovered, it seems this would be a good sign that an irreversible problem has occurred and that ...

Java NIO: Relationship between OP_ACCEPT and OP_READ ?

I am re-writing the core NIO server networking code for my project, and I'm trying to figure out when I should "store" connection information for future use. For example, once a client connects in the usual manner, I store and associate the SocketChannel object for that connected client so that I can write data to that client at any tim...

Testing/troubleshooting tool for network connections?

I am creating a non-blocking IO system for the server side of my project. The actual implementation isn't important (it is Java NIO if you care), but I feel very intimidated by the subtle issues you have to pay close attention to when programming socket communications. My knowledge is lacking in a lot of areas, so I can't even conceive...

Java: Complete code examples of thread-per-connection blocking IO versus NIO?

Ok, I'm going crazy here. I have been rewriting the NIO code for my server, and running into some real headaches. The bottom line is that getting NIO "right" is very hard. Some people pointed me to the Rox tutorial at http://rox-xmlrpc.sourceforge.net/niotut/, which seems to be on a good path, but is not as complete as I would like. ...

Difference between ByteBuffer.allocateDirect() and MappedByteBuffer.load()

Hi , I was trying to implement a sort of shared cache between two or more JVMs by memory mapping a particular file using MappedByteBuffer. From the specifications I see that when we use MappedByteBuffer.load() it should load the data into a direct buffer. I have a couple of questions on this. My code snippet:: RandomAccessFile file =...