FAQ Database Discussion Community


Appending text to existing file without overwriting it

java,nio
I'm adding some content into an existing file NameList.txt, which already contains some text there. But when I run the program, it removes all the existing content before starting to write new ones. Note: I also try to find where is the file ending line. E.g. by using while ((lines...

Is this ChannelFuture listener registration robust?

java,networking,netty,nio
Below is some code snippet from the book < Netty in Action >. I am wondering if the connect action finished after 1 and before 2, will the ChannelFutureListener ever get a chance to be notified?...

How does netty determine when a read is complete?

java,networking,netty,nio
Below is a ChannelHandler for a echo server. Netty framework will call channelReadComplete() method to notify the handler that the last call made to channelRead() was the last message in the current batch. My question is, since data are transferred on wire in trunks, how could Netty know when a...

How to get FileInputStream to File in assets folder

java,android,nio,android-resources
I know how to use the AssetManager to read a File from the res/raw directory with an InputStream, but for my special use case I need a FileInputStream. The reason I need a FileInputStream specifically is because I need to get the FileChannel object from it by calling getChannel(). This...

Java NIO object to date conversion

java,nio
I am trying to get last access, and created date of a file in java, so I am using nio, but output is coming in this format: Last access time :: 2015-02-26T15:22:02.988862Z Creation time :: 2015-02-26T15:22:02.988862Z last modified time :: 2015-02-26T16:06:52.022666Z Is there anyway to convert them to Date or...

Reference Count Object

java,netty,nio
In netty, Objects processed by ChannelHandler(s) should be released by calling ReferenceCountUtil.release(). I am using customized objects in my application and documents says any object needed to be released should implement ReferenceCounted so it could be managed by netty framework. So should we put a clean up handler at the...

Writing zipped byte[] into file

java,nio,bytearrayoutputstream,zipoutputstream
I am trying to write a zipfile to a byte[] in memory, and then write that out to disk. The resulting zipfile is corrupt. This works: try (FileOutputStream fos = new FileOutputStream(Files.createTempFile("works", ".zip").toFile()); ZipOutputStream zos = new ZipOutputStream(fos)) { zos.putNextEntry(new ZipEntry("test.txt")); zos.write("hello world".getBytes()); zos.closeEntry(); } This is broken and creates...

How java nio ServerSocketChannel accept works?

java,sockets,network-programming,nio,nonblocking
I can't get how NIO works under the hood. Here is a sample code: // Create the server socket channel ServerSocketChannel server = ServerSocketChannel.open(); // nonblocking I/O server.configureBlocking(false); // host-port 8000 server.socket().bind(new java.net.InetSocketAddress(host,8000)); // Create the selector Selector selector = Selector.open(); // Recording server to selector (type OP_ACCEPT) server.register(selector,SelectionKey.OP_ACCEPT); while...

FileChannel.write incomplete

java,nio,filechannel
I'm using FileChannel to wrtie 2MB data to a file. private void write(int numEntries, int entrySize) throws Exception { File dir = new File(iotestDir); dir.mkdirs(); File file = new File(dir, "data00001.dat"); file.delete(); file.createNewFile(); try { FileChannel channel = new FileOutputStream(file).getChannel(); ByteBuffer[] bb = new ByteBuffer[numEntries]; for (int i = 0;...

Android ways to speed up few readings from files

android,multithreading,performance,nio
In my application I need to load some files, when the app starts. I have 6 different files, which content int arrays and I need to read them as fast as possible. 3 of them content 80K ints, other 200K ints Here is method that I'm using to read int...

How to create a file Path from a folder Path

java,nio
I am probably overlooking something but what's the right way to create a file Path from a folder Path? This is what I'm doing but it seems wrong to convert the folder path to a string just to reconstruct it. Is there a better way? Path testFolder = Files.createTempDirectory("fileFinder"); Path...

SocketChannel sockets not reading data

java,sockets,nio
Here is my code. From the TestServer, I'm trying to send data through an outputstream and have it received from the test client. I'm using a SocketChannel because I need the client to listen on 3 ports at the same time. At the moment, i'm only trying to read from...

Server running at 30% CPU after sudden client disconnect with Java NIO, will my fix work?

java,nio,nonblocking
I have an non-blocking NIO server I wrote, for the most part it functions as expected with normal use; however I noticed that on occasion it sticks at 30% CPU usage for some reason. After a lot of time trying to force the bug I eventually triggered it when I...

How can I convert an ArrayList to a ByteBuffer?

java,arraylist,nio,bytebuffer
I'm trying to create a TriangleIndexVertexArray with JBullet, but to do this I need to parse a ByteBuffer of all the vertices in the model. I have got an ArrayList<Vector3f> of all the vertices in the model. How can I parse this list of Vector3f's to a ByteBuffer?...

Why does the java DirectoryStream perform so slow?

java,performance,stream,nio
I've done some testing with Streams in special with DirectoryStreams of the nio-package. I simply try to get a list of all files in a directory sorted by last modified date and size. The JavaDoc of old File.listFiles() stated a Note to the method in Files: Note that the Files...

Java NIO SSL - SSLEngine Session reuse

java,ssl,java-8,nio,jsse
I am implementing client and server part using Java 8 NIO and SSL. I have some doubts on creating SSLEngine. There is one method in SSLContext public final SSLEngine createSSLEngine(String peerHost, int peerPort) Applications using this factory method are providing hints for an internal session reuse strategy. While creating the...

Java Scanner input in separate thread

java,multithreading,java.util.scanner,nio
I have a multi-threaded command line app. It is a web service client with a pool of 10 threads that churns away, sending requests, batch-style, to a server. But it runs for a few days, and sometimes further down the pipeline, the queues start getting backed up. So I want...

TFTP client based on java NIO

java,nio,tftp
I am trying to implement TFTP client using Java NIO. But it leads to error: network error: Address already in use: bind Code snippet is shared here. Selector selector = Selector.open(); DatagramChannel channel = DatagramChannel.open(); InetSocketAddress isa = new InetSocketAddress("10.86.4.250",69); channel.socket().bind(isa); channel.configureBlocking(false); As I am new to this networking concept,...

NIO SocketChannel saying there is no data when there is (or selector is not informing me)

java,sockets,tcp,nio,socketchannel
I have a functioning client-server apparatus which can successfully connect and send messages to each other using NIO. Right now my only confusion is how I'm supposed to continue reading when socketChannel.read() returns zero. I have a protocol that sends the first 4 bytes as the number of incoming bytes...

Java NIO multiple selectors per selectable channel

java,nio
For Java NIO, can we register multiple selectors for a single selectable channel such that the registered selectors can run select query in parallel with each other on the same channel.

java.nio.file.Path vs sun.nio.fs.UnixPath

java,nio
Almost all file I/O operations in my Java code uses java.nio.*. While debugging a piece of code today, however, I noticed that the debugger (Intellij IDEA 14) was showing the following piece of information for a java.nio.file.Path instance: Why is java.nio.file.Path object being shown as sun.nio.fs.UnixPath object in the debugger?...

Alternative to File.deleteOnExit() in Java NIO?

java,nio
Java IO has File.deleteOnExit(), which is a method that deletes the file it is called on during normal termination of the JVM. I've found this to be very useful for cleaning up temporary files, especially during unit tests. However, I don't see a method by the same name in Java...

NIO Selector OP_READ and OP_WRITE, some questions about handling them

java,bit-manipulation,nio
I've been following this tutorial, it's quite helpful and most things are making sense. If you look on page 34 you'll see this snippet of code: } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { // Read the data SocketChannel sc = (SocketChannel)key.channel(); // ... } What exactly is going...

Netty: Bound Mismatch

java,abstract-class,netty,nio
I am new to java and was trying to use Netty to build a sample tcp server. Here is what i have currently package http_server; import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; import netty_tutorial.EchoServerHandler; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.ServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;...

Java NIO with sql database

java,sql-server,nio
I am writing a Server using Java NIO that will receive data from client (for eg. location) and will store the data into a database using Microsoft SQL Server 2012. I know the call to database is blocking and thus writing codes in the main NIO thread will be to...

Event Driven server using Java NIO

java,multithreading,nio,httpserver,event-driven
I'm trying to wrap my head around building an asynchronous (non blocking) HTTP server using java NIO. I presently have a threadpool implementation and would like to make it into Event Driven with single thread. How exactly does an Event Driven server work? Do we still need threads? I've been...

java.net.ConnectException: Connection timed out: no further information

java,nio
Now today I was testing Server and Client code on different machine. Both were on same Wi-fi network. I created clients using below code and got this exception for many threads : java.net.ConnectException: Connection timed out: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source) at SocketTest.connect(Client.java:188) at SocketTest.run(Client.java:73) at...

CastException with Java NIO sockets

java,class,sockets,nio
I've been studying the book Pro Java 7 NIO.2 to get a better understanding of the NIO package, and wanted to work on some networking related code to better my understanding of how netty works in the background. The over-all error makes sense, but why the error is being thrown...

Java NIO TCP timeout issue

java,multithreading,tcp,nio,socketchannel
I am using one SocketChannel in 2 threads, one thread for sending the data and another for receiving the data. SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(ip,port)); socketChannel.configureBlocking(false); Thread 1: uses the above socketchannel to write the data Thread 2: uses the same socketchannel to read the data I am not using...

How to find a file that's been moved/renamed in Java

java,nio,log-rotation
I would like to have some sort of file handle to a file that survives file moves/renames. This does not have to be platform independent, it's fine if it only works on Java7+linux, and even fine if the files can be assumed to not change directory. The use-case is that...

SocketChannel issue

java,nio,socketchannel
I wrote an app which connects to server by TCP with SocketChannel but I have two issues: The first is minor - sometimes for some unknown reason I send concatenated messages and the second is crucial - periodically the app stops sends/receives messages Any idea what is wrong? import java.io.IOException;...

NIO Selector Thread, handling channels as expected, but how do I ensure channels are correctly closed after use?

java,multithreading,server,nio
So I have the following code in my ServerRunnable class: public class FirmwareServerRunnable implements Runnable { private static Logger log = Logger.getLogger(FirmwareServerRunnable.class .getName()); private LinkedTransferQueue<CommunicationState> communicationQueue; private int serverPort = 48485; public FirmwareServerRunnable(int port, LinkedTransferQueue<CommunicationState> communicationQueue) { serverPort = port; this.communicationQueue = communicationQueue; } private boolean running; private ServerSocketChannel...

Trying to copy files in specified path with specified extension and replace them with new extension

java,file,copy,nio
I have most of it down but when I try to make the copy, no copy is made. It finds the files in the specified directory like it is supposed to do and I think the copy function executes but there aren't any more files in the specified directory. Any...

Java NIO SeletionKey iterator and key handling, am I doing it right?

java,multithreading,sockets,nio
I'm following this tutorial on Java NIO. I found what appears to be the correct java file Here. I've adjusted my code to operate on a single port instead of multiple ports, and to simply output the data to the screen rather than echoing it back to the client. When...

Why do we need a half-close socket?

networking,tcp,nio
According to this blog, it seems half open connection is what we want to avoid. So why does Java still provides the facility to make a socket half close?...

Using nio.relativize for a normalized path

java,path,nio
Normally, a path ignores all the . (this directory) it contains. So, c:\\personal\\.\\photos\\readme.txt and c:\\personal\\photos\\readme.txt should give identical results for different operations, but in the following code, the normalized path gives a different result. Can anyone explain the reason for this? Path p1 = Paths.get("c:\\personal\\.\\photos\\readme.txt"); Path p2 = Paths.get("c:\\personal\\index.html"); Path...

Java 7 nio list directory with wildcard

java,nio
I'd like to find a file in a directory using wildcard. I have this in Java 6 but want to convert the code to Java 7 NIO: File dir = new File(mydir); FileFilter fileFilter = new WildcardFileFilter(identifier+".*"); File[] files = dir.listFiles(fileFilter); There is no WildcardFileFilter, and I've played around a...

Best way to write huge number of files

java,nio,java-io
I am writing a lots of files like bellow. public void call(Iterator<Tuple2<Text, BytesWritable>> arg0) throws Exception { // TODO Auto-generated method stub while (arg0.hasNext()) { Tuple2<Text, BytesWritable> tuple2 = arg0.next(); System.out.println(tuple2._1().toString()); PrintWriter writer = new PrintWriter("/home/suv/junk/sparkOutPut/"+tuple2._1().toString(), "UTF-8"); writer.println(new String(tuple2._2().getBytes())); writer.close(); } } Is there any better way to write the...

NoSuchFileException when creating a file using nio

java,file-io,nio
I am trying to create a new file using java nio, and I'm running into a createFile error. Error looks like this: createFile error: java.nio.file.NoSuchFileException: /Users/jchang/result_apache_log_parser_2015/06/09_10:53:49 code segment looks like this: String filename = "/Users/jchang/result_apache_log_parser_" + filename_date; Path file = Paths.get(filename); try { Files.createFile(file); } catch (FileAlreadyExistsException x) { System.err.format("file...

Java.NIO on Write confussion

java,nio
I have a confusion on the onWrite method of Java NIO, when the selector is ready to write should I: 1: Register/Change a OP_Write 2: If Write returns 0 what should I do? 3: After writing should I Register or Change ops? The channel is set to Non-Blocking ....onAccept register...

java.nio.file.NoSuchFileException: why nio not creating file [closed]

java,file-io,nio
I am using java.nio.file package and tried to create file with the following code. private static void printReport(String filename, String str)throws Exception{ ErrorCheck ec = new ErrorCheck(); String fileName = "/var/Emails_log/"+filename; Path filePath = Paths.get(fileName); File file = new File(fileName); final BufferedWriter out = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8, StandardOpenOption.APPEND); try{ final Path...

Is it safe to set interestOps of SelectionKey in other thread than selector thread? (Java Nio)

java,nio
I have seen many JavaNio examples. All of them set interestOps of SelectionKey in selector thread. Why they do it? Is it safe to set interestOps of SelectionKey in other thread than selector thread? Or there are abnormal situations that can occur, so they do it?...

How sync and async request processing differs in Tomcat?

java,tomcat,asynchronous,sync,nio
I cannot figure out what difference is between sync and async calls in Tomcat. Everywhere I use NIO. I have thousand connections managed by few Tomcat threads. When long sync request incomes a thread borrows from Tomcat thread pool and processes the request. This thread waits for long process to...

NIO client giving exception : java.net.ConnectException: Connection refused: no further information

java,nio
I modified the sample code available here for Client and Server My client : public class Client { public static void main(String[] args) { int n=10000; SocketTest [] st= new SocketTest[n]; for(int i=0;i<n;i++) st[i]= new SocketTest("hi"); for(int i=0;i<n;i++) new Thread(st[i]).start(); } } class SocketTest implements Runnable { private String message...

Netty NioSocketChannel got broken message on multithread write

java,multithreading,netty,nio,messagepack
I'm writing my own simple game server using Netty 4.0.25. I want to support msgpack binary data so every message send/receive on a channel always encoded/decoded by msgpack. my server: bossGroup = new NioEventLoopGroup(4); workerGroup = new NioEventLoopGroup(4); bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.handler(new LoggingHandler(LogLevel.DEBUG)); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {...

Mixing Java NIO with IO input streams read operation

java,sockets,io,nio
I need to use NIO to allow the server side to support timeout on write operations, but handling reading operations on the socket channels complicates my program. I was wondering if it's possible to write to the sockets using NIO but to read using regular IO, something like ((SocketChannel) selectedKey.channel()).socket().getInputStream().read(buffer)...

How to bypass java.nio.file.DirectoryNotEmptyException? [duplicate]

java,file,nio
This question already has an answer here: How to delete a folder with files using java 12 answers Is there a way to bypass a java.nio.file.DirectoryNotEmptyException? I want to be able to delete a folder with content in it....

Search text From Arraylist

java,file-io,arraylist,nio
Source Code GitHub i'm Working File Handing Library Project.Books Data Save in a File Name NameList.txt Ready whole File and save into ArrayList then try to search a Book Name "Head First jQuery" But can find. please help how can i find Desire Text From Arraylist for Text file ....

Why doesn't my way of using FileChannel, ByteBuffer and CharBuffer work like the other way?

java,nio
Given the file Orange Purple Indigo Pink Why won't the myWay method in the code below give me the content of the ByteBuffer via the Charset.decode? Notice I validate that the ByteBuffer has the file content, but it seems no matter what methodology I use from within myWay, I cannot...

Advantage of ChannelInitializer over Channel Handler in Netty

java,netty,nio,channel
What are the advantages of using ChannelInitializer over a chain of ChannelHandlers directly? For example with a server bootstrap I could do: bootstrap.childHandler(channel_handler); Add in the implementation of channel_handler i would implement the following class simple_channel_handler implements ChannelHandler { public void handlerAdded(ChannelHandlerContext ctx) throws Exception { // TODO Auto-generated method...

is there transferFrom like functionality in Netty for zero copy?

java,http,netty,nio,zero-copy
I'm developing a Http server that processes fairly large size of payloads. Since Netty provide zero-copy, I thought of using zero-coping of the payload using Netty's zero-copy. But it seems Netty only provide transferTo(WritableByteChannel target, long position) but not providing transferFrom() like method to read the content directly in to...