nio « nio « Java I/O Q&A

Java I/O Q&A
2.batch File
3.binary File
4.class file
5.CSV file
10.Excel File
11.File Attribute
14.Media File
17.PDF file
20.text file
22.XML file
Java I/O Q&A » nio » nio 

1. how to send data to client using nio

Is there any simple example for reading data from server and sending the response to client using java NIO socket channel Thanks, Deepak

2. 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 ...

3. Java's ReadableByteChannelImpl inconsistent behaviour

When I create a channel out of the InputStream with Channels.newChannel(is) java standard library returns a ReadableByteChannelImpl, which is:

   private static class ReadableByteChannelImpl

4. Java: use NIO with

Is it possible to use NIO with I would like to somehow treat 'stdin' as a selectable channel. Has anyone found a way to do this?

5. NIO: Send message and then disconnect immediately

In some circumstances I wish to send an error message from a server to client using non-blocking I/O (SocketChannel.write(ByteBuffer)) and then disconnect the client. Assuming I write the full contents ...

6. Strange exception of Httpcore nio in java

    Exception in thread "Thread-0" java.lang.NullPointerException
 at org.apache.http.impl.nio.reactor.AbstractIOReactor.closeActiveChannels(
 at org.apache.http.impl.nio.reactor.AbstractIOReactor.hardShutdown(
 at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.doShutdown(
 at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(
 at com.***.clawer.Clawer$
 at Source)
    Exception in thread "Thread-1" java.lang.IllegalStateException: I/O ...

7. Java programs using NIO framework

I am doing some research related to Java NIO. I need to find some representative applications that are based on this framework. Please feel free to suggest! The more, the merrier! ...

8. Apache MINA NIO connector help

I'm new to using MINA. I've a program which uses MINA NIOconnector to connect to host. I'm able to send data and also receive. This is clear from log4j log which i'm attaching ...

9. Why use Java's AsynchronousFileChannel?

I can understand why network apps would use multiplexing (to not create too many threads), and why programs would use async calls for pipelining (more efficient). But I don't understand the ...

10. Java: GatheringByteChannel advantages?

I'm wondering when the GatheringByteChannel's write methods (taking in an array of ByteBuffers) have advantages over the "regular" WritableByteChannel write methods. I tried a test where I could use ...

11. Is this the right way to write a ProtocolDecoder in MINA?

public class CustomProtocolDecoder extends CumulativeProtocolDecoder{
byte currentCmd = -1;
int currentSize = -1;
boolean isFirst = false;
protected boolean doDecode(IoSession is, ByteBuffer bb, ProtocolDecoderOutput pdo) throws Exception {

12. Java RTP/RTCP library using NIO

Is there a Java RTP/RTCP library based on Java NIO or some Java NIO framework (Netty, MINA, ...)?

13. understanding Java NIO concepts

Can someone suggest a good book/tutorial to understand Java NIO from the operating system perspective (in comparison with

14. How do I declare a variable that contains a subclass of a class which implements an interface?

I want to declare a variable which holds a class which implements a specific interface. Specifically, I'm trying to store a SocketChannel and a DatagramChannel in the same property so ...

15. Can I use Java NIO2?

I am working on a project where I've to implement Distributed File System, so for I/O operations, I was thinking of using NIO2 (JDK7) JDK7 will be

16. Need help making this code more efficient

I always use this method to easily read the content of a file. Is it efficient enough? Is 1024 good for the buffer size?

public static String read(File file) {

17. Java NIO, to use or not to use a framework?

I'm developing a Java Based server, with NIO multiplex and I started to see a lot of frameworks... I don't understand if these frameworks makes the life easier only or has ...

18. Use String or Object for data in Java NIO-based distributed system?

I am trying to send data different data (Security keys, encrypted message )in distributed system that use Java NIO. my question is what is the best way to send data? . ...

19. Access running java program from shell command

I am looking for a way to access running java program from command line. The best woud be something that does the following: Starting java app:

bash$java -jar MyBundle.jar App
Accessing app:
bash$ param1 param2
So, ...

20. java NIO.2 glob problem

I have:

Path path = Paths.get("Foo.class");
Path path2 = Paths.get("");

FileSystem fs = FileSystems.getDefault();
PathMatcher matcher = fs.getPathMatcher("glob:*.{class, java}"); 

matcher.matches(path); // TRUE
matcher.matches(path_2); // FALSE
Why path2 is false if the glob syntax {} means that it ...

21. Get file/directory size using Java 7 new IO

How can I get the size of a file or directory using the new NIO in java 7?

22. Apache Mina NioAcceptor

I am experimenting with Apache Mina and currently have unclear point: In NioSocketAcceptor acceptor = new NioSocketAcceptor(2); I specify the number of acceptors. As fa as I ...

23. How to refactor this IO code?

I need to read records from a flat file, where each 128 bytes constitutes a logical record. The calling module of this below reader does just the following.


24. NPE at

We are using a third party application which uses sun.nio package. We are facing following error very frequently,


25. recommend a book about java io and java nio

I want to dive into java io and java nio, I am looking for some good materials to study. Do you have some good advice to me , good io blogs ...

26. With Java NIO, how do you distinguish between clients?

This may be a terribly naive question but I was just wondering if the client would have to send a "client id" of some sort along with every packet to be ...

27. How can I generate 'un-mappable' input for a Java CharsetDecoder?

I'm writing a set of unit tests for a text decoding class. I'd like to write a test that correctly exercises the handling of un-mappable input to a CharsetDecoder. However, I've ...

28. NIO non-blocking mode

One of the most important aspects of NIO is the ability to operate in non-blocking mode, denied to the traditional java I/O library. But what is non-blocking mode?

29. Why java.nio

31. Java NIO error (anyone?)

I get the following error whenever I try to use NIO to copy certain files. Has anyone seen this.. Bad address at Method) at at at at at at Code -------- try { // Create channel on the source FileChannel srcChannel = new FileInputStream("srcFilename").getChannel(); // Create channel on the destination FileChannel dstChannel = ...

32. Question about NIO

33. NIO: Impact on original I/O libraries

I have just read the following in Manning's JDK1.4 Tutorial (Greg M. Travis) : " a substantial degree, the original I/O libraries have been rewritten to take advantage of the New I/O API" I know, for instance, I can obtain a FileChannel from a RandomAccessFile, but does the above mean that the existing methods of RandomAccessFile have been reimplemented to internally ...

34. NIO and Internationalization

35. NIO and backward compatability with SDK 1.3

No. Because there is no backward compatability. NIO is an integral part of the base 1.4 Java platform. It cannot be used independently because much of it is implemented in the low-level plumbing of the JVM itself. If you make the decision to use NIO, you're committed to 1.4 - there's no going back. I think this is one reason many ...

36. my post to win a free Java NIO book...

37. Ron Hitchens' Java NIO book

I looked at the description on Amazon and it sounds good. Does it have lots of code examples too? Most O'Reilly books are really great with that (with a few exceptions) and it's always very helpful for me. Is there a way to use regular expressions to create a filter (wrapper) for the stream in 1.4? Or do you have to ...

40. Java NIO vs. traditional IO.

41. NIO

No, at least not any time soon. The name "New I/O" is rather unfortunate. NIO is not a replacement for the old I/O classes. Rather, it's an alternate way of performing I/O. The older classes in are very powerful and flexible. The compositional patterns they enable work for a broad range of applications. Unfortunately, the stream metaphor used throughout ...

42. Portability implications for NIO

43. Writing objects over nio

ByteBuffers and channels deal only with arrays of bytes, they have no conception of things like ObjectStreams. Sending a serialized object graph over an NIO channel is no different than sending any other chunk of bytes. You just need to obtain the bytes representing your serialized object and write it to the channel. Here are two ways to do so: ByteArrayOutputStream ...

44. NIO:Selling point of the Java NIO

45. NIO DatagramChannels

46. NIO Book Required

Hi all java Gurus , I have worked on I/O jdk1.3, Now after a long time again i am starting working on NIO. Please suggest me the best Book on "NIO" to the point with healhy stuff. Currently best book for over all java (2SE v1.4), covering new features or enhancements provided in jdk1.4 by SUN. In time reply will be ...

47. Help about nio...

Hello friends , I am back to you all experts to get some more things clarified. Well cud anyone tell me the list of differences between package and the java.nio packages ?? One thing which is very much clear to me is that java.nio is more faster that ?? but i wanna know more of it .. i hope ...

48. Some doubt abt NIO usage ?

If you can do useful processing while waiting for more data to arrive over the network, non-blocking is a hnday way to just grab whatever data is available and continue processing. A more common use is in high-load servers. With blocking IO you need to spawn a new thread for each request which limits the number of simultanious request the server ...

49. Bunch of NIO questions

Hello, I'm fairly new to NIO, but I do see its benefits. Yet, I still have some questions regarding real world use. For starters, if you drop all advertizements and sale points and theory - how much better is it? has anyone rewritten multiclient socket apps and seen dramatic performance improvements? Also, I have this server that sends out streaming data ...

50. NIO problem.

Hi, Just playing with NIO for the first time and I am reading from text files from a collection of file objects and the reads always result in: ??? Where as if I read the same files with the old Java IO I get the actual text. My code: FileInputStream inFile = null; try { inFile = new FileInputStream(files[i]); } catch(FileNotFoundException ...

51. NIO Stability

Hi, I am at a crossroads in deciding the I/O package I have to use in my current project. We are using Sun JVM 1.4.2 and I wanted to know how stable NIO is at present. I know its been around for a while (> 1 year), but I wanted to know if someone has this in production handling huge amount ...

52. convert java.nio to equivalent in 1.3

Hello, My problem is as follows. I have an application developed under java 1.4 that uses java.nio (available in 1.4 only). I have to make this application compatible for java 1.3 (due to the device it has to run on: PDA). I would really like to avoid rewriteing the application without using java.nio. So I am thinking at either an alternative ...

53. Is NIO really faster than*

Hi, I have always used the classes for java IO. I tried to compare the performance of the io and nio classes FileChannel and ByteBuffers with the normal FileInput/OutputStreams and found that the normal java io was faster than nio. I wrote a simple program that reads a file into a ByteBuffer/Byte Array and writes the output to a FileOutputStream/ByteBuffer. ...

54. nio download problem

I am using nio to download a file given from a web address. I have not conducted extensive testing but my code works for text files and zip files, but NOT exe or bin files. I assume it works for all text and no binary (although zip is binary right?). The weird thing is, I set up tomcat, and I can ...

55. nio maxing CPU usage

This post is related to my other post,, but now I have a new problem. The code below works, pulls down a file, rips out the http response, and closes the channels properly. HOWEVER, my cpu usage is 100% max while the code runs. This code is also in a thread spawned by a download manager. I have an AMD64 ...

56. java nio usefull for...

57. io vs nio debate continues

HI there, I once created a client/server application and used ObjectInputStream and ObjectOutputStream to move the object data back and forth. This worked well. I'm now considering that since the client/server application will soon include VoiP and video streaming, that a faster server using NIO may help increase performance and help get rid of those herky-jerky video images. My understanding of ...

58. nio and getRuntime()?

Well, Runtime (or rather, Process) doesn't have any special hook for NIO, so you'd have to use the InputStreams and put wrappers around them: Process proc = Runtime.getRuntime().exec(command); ReadableByteChannel standardChannel = Channels.newChannel(proc.getInputStream()); ReadableByteChannel errorChannel = Channels.newChannel(proc.getErrorStream()); What you do with them after that is up to you - they work like other NIO classes, if you have examples of those. As ...

60. NIO in Java SE 6

61. Text search and replacement using nio

Hi All, I want to search a particular text ina file and replace it with a different string (much more in length). I am tryinh it with regular expression and new io. here is my code snippet ..................................... RandomAccessFile raf=new RandomAccessFile(f,"rw"); FileChannel fc = raf.getChannel(); int len=(int)fc.size(); MappedByteBuffer bb =, 0, len); Charset charset = Charset.forName("US-ASCII"); CharsetDecoder decoder = charset.newDecoder(); ...

62. Java nio

Java nio (I/O and Streams forum at JavaRanch) A friendly place for programming greenhorns! Register / Login Java Forums Java I/O and Streams Java nio Post by: jubin kart, Greenhorn on Jul 19, 2008 13:25:00 Hi, I was trying out java nio and I wanted to see the difference between nio and the normal file stream. I tried copying ...

64. NIO question

I have write a new NIO server thread, which accept messages from client, but there is some problem. which make it loop too much quickly which make cpu usage to 100%, my run method is follow, any problem? public void run() { try { // This map is used to store clientChannel - Connection pair set, // store it while accepting ...

65. NIO MalformedInputException & German Umlauts

Hello, I have an NIO server and wish to send messages containing german umlauts such as , , . This works fine on the local development machine but on the internet server it throws an Excpetion: java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(Unknown Source) at java.nio.charset.CharsetDecoder.decode(Unknown Source) I am using 1.5.0_06 on the server and 1.5.0_07 locally. The server is also ...

66. java nio usefull for...

67. java.nio and Selctor

Hi all I am trying to understand how java.nio with Async non blocking chanels. Basicly I am going to have to write a server that opens up a port and allows many different clients stream data to it to be processed. I have seen some examples, and I have gotton them to work some, but I want to understand what they ...

68. nio API

Does the new IO API have a better way of dealing with directories? For example, how do you get the size of a directory? In 1.3, you have to get the size of every file in it. Is there a better way now? Another example: In 1.3. when getting a list of all files in a directory(including all subdirectories) you have ...

69. J2SE 5.0 and NIO

I'm reading Ivan Horton's Java 2 JDK 5 edition. I just went through the chapters on reading and writing files. He ony covers the new nio classes (FileChannels, ByteBuffers and the various view buffers) and states that he won't cover using streams directly because the new nio methodolgy will supercede using streams directly. After learning the new nio stuff, I can ...

70. Java NIO


Hey, I'm doing several networking tests with NIO and I have the following issue: I'm trying to send bytes with a gathering write ( write of bytebuffer array) and I see that my client sends alot of bytes but my client reads much less. Its like the Selector is not fast enough, and it misses some of the selecting. Anyone had ...

72. java nio

73. NIO vs IO

Hi All, I have read one tutorial on New IO package of Java. It says that Java NIO is faster than the old IO API. I have tried to find the difference between them using below programs package com.nio.chapter; import; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class NIOReader { public void readFromFile() { try { FileInputStream fis = new FileInputStream( "file.txt" ...

74. NIO 2

You should probably try googling for such resources. There probably won't be that many yet, since NIO 2 is still pretty new. For detail, I suggest you click my second link and read the documentation of the java.nio.file.* package. In my own code, I haven't changed much since NIO 2 came, except that I'm dropping the File class where I can, ...

75. Helo need for Java NIO

String ss = "56"; String mm = "07"; String HH = "15"; String DD = "20" ; String MM = "01"; String yy1 = "09"; String yy2 = "20"; String[] dateByteFormat = new String[7]; dateByteFormat[0] = ss; dateByteFormat[1] = mm; dateByteFormat[2] = HH; dateByteFormat[3] = DD; dateByteFormat[4] = MM; dateByteFormat[5] = yy1; dateByteFormat[6] = yy2; for (int i = 0; i

76. IO versus NIO

Hi all, Wow a new look to this forum!!!! I was goin g through the IO and NIO packages in java.I understand that the new nio package as can do all the thing likethe old IO package but its a bit complex as compare to the old one.I am wondering mostly I see people using (even on this forum) new IO ...

78. Why does throw BindException?

You are running out of local ports. That can happen by opening large numbers of outbound connections within a two minute time period, which brings TIME_WAIT into play. The solution is simple: don't do that. It should only arise in testing, and all that that means is that you're testing an unrealistic situation, so don't test it. If it arises in ...

79. NIO libs

This might be a stretchy question, but what are the names of the native library needed for NIO to work? I want to know because the first time you create any NIO channel it takes around 500ms which I've been told is the native libs loading, and I'd like to do this at startup instead of the first NIO call. Thanks. ...

80. Java NIO

81. Need to Save String value using Java nio

The 7 byte is saving but all are in HEX format. It want to save these 7 byte exactly same value not in HEX in the file. can u please help me? need to save in file exactly in selected position as per attachment. 56 07 15 20 01 09 20 Please help me.

82. java.nio.charset enconding special characters

83. NIO question

A quick question here. In my program I use NIO filechannel to open big, binary file. Since I don't need to open whole file at once, I just move the position of this filechannel to the point, then read specified size. It works fine, now I was asked if it still works for really big file, >2g. Since I don't have ...

84. Is this bug in java.nio?

But it is not working for me(means it is acting as a shared lock). Really? Can you really acquire two of these exclusive locks on the same file from separate JVMs at the same time? If so that would be a pretty major bug. Strange it hasn't been reported really. Possibly you mean you can still open the file, or you ...

85. Java Nio Problem

86. Writing Objects using java.nio

87. Using NIO or IO?

I have made a Live Online Chat program and have been having a few strange issues that have caused the connections to reset and disconnect the chat session. (e.g. Software caused connection abort: socket write error) I am wondering if this is because I'm using IO over NIO. Currently, my program sends many Objects back and forth using ObjectInputStream and ...

88. NIO verses IO

Before starting to write down anything go and look. I've been working in a java project using sockts, the project took 8 months and during that time we did test in order to decide what to use... NIO offers more functionaty out of the box, and that off course leads to a bigger code complexety, not in the end code of ...

89. java.nio read/write question

A number of the regular posters here are striking in protest at the poor management of these forums. Although it is our unpaid efforts which make the forums function, the Sun employees responsible for them seem to regard us as contemptible. We hope that this strike will enable them to see the value which we provide to Sun. Apologies to unsuspecting ...

90. java.nio.charset.spi.CharsetProvider problem

sun.misc.ServiceConfigurationError: java.nio.charset.spi.CharsetProvider: jar:file:/C:/Program Files/Java/j2re1.4.2_12/lib/charsets.jar!/META-INF/services/java.nio.charset.spi.CharsetProvider:1: Illegal configuration-file syntax at Source) at Source) at sun.misc.Service.parseLine(Unknown Source) at sun.misc.Service.parse(Unknown Source) at sun.misc.Service.access$100(Unknown Source) at sun.misc.Service$LazyIterator.hasNext(Unknown Source) at java.nio.charset.Charset$1.getNext(Unknown Source) at java.nio.charset.Charset$1.hasNext(Unknown Source) at java.nio.charset.Charset$ Source) at Method) at java.nio.charset.Charset.lookupViaProviders(Unknown Source) at java.nio.charset.Charset.lookup2(Unknown Source) at java.nio.charset.Charset.lookup(Unknown Source) at java.nio.charset.Charset.isSupported(Unknown Source) at sun.nio.cs.StreamDecoder.forInputStreamReader(Unknown Source) at Source) Does anyone know a solution? Thanks ...

91. Java NIO Question

Hello, I am developing a java nio based chat application with encoded secure communication. Logging in to the server and initiating the whole cipher things works fine. So the communication between client and server is encoded. The whole architecture works fine, when I have the following approach: Client writes to -> Server response to -> Client But I want to develop ...

92. java nio

93. NIO Simultaneous read and write operation

No, it is that you have to get your buffer positions and limits and flips and rewinds organized. mb.put(bb) just puts the entire contents of bb into mb starting from the current position, and if bb won't fit between position and limit you'll get an overflow. You probably need to reset or rewind the buffer so position = zero and limit ...

94. java.nio: scatter-gather question

Hi, i have a question on the following scatter-gather java.nio code. What happens if a message is smaller in size than the (maximum) "messageLength" -if the messages are not of fixed size? The check "(bytesRead < messageLength) " won't work and we will start storing bytes from the next message in the buffer

95. Where I can find good Java NIO tutorial?

96. New to NIO and ByteOrder. How do I read in Little Endian?

Hi. About a year ago I wrote a class for reading little-endian files with the IO library where I had to convert the bytes from little-endian with bit shifting. I see that NIO has a ByteOrder class for specifying endianness. Ive also heard that its "faster" and "better". I have 30 megs files so that would be nice. Im having no ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.