Over a million developers have joined DZone.

Writing to stdin

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

System.in or stdin is usually used as an InputStream. However on Linux you can access this stream in other ways.

Accessing file descriptor 0

In linux, each file descriptor is accessible via /proc/{process-id}/fd/{file-id} You can use this to see what files a process has open but also see the contents of a file.

Writing and memory mapping

Getting the process id in Java is obscure, but once you have this you can re-open existing file descriptors such as stdin, file descriptor 0.

int processId = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
RandomAccessFile raf = new RandomAccessFile("/proc/" + processId + "/fd/0", "rw");
final FileChannel fc = raf.getChannel();
final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());

This opens the System.in is a read-write mode and memory maps it before changing its contents.

When you run this program it looks like
$ echo "        " > file.dat
$ od -xc file.dat
0000000    2020    2020    2020    2020    000a
$ java -cp . Main < file.dat
$ od -xc file.dat
0000000    0000    5522    11a9    79c6    000a
         \0  \0   "   U 251 021 306   y  \n
At first is rather surprising you can write to stdin or even memory map it. What happens if stdin is not a real file.
$ echo "        " | java -cp . Main
Exception in thread "main" java.io.FileNotFoundException: /proc/7935/fd/0 (Text file busy)
	at java.io.RandomAccessFile.open(Native Method)
	at java.io.RandomAccessFile.<init>(RandomAccessFile.java:233)
	at java.io.RandomAccessFile.<init>(RandomAccessFile.java:118)
	at Main.main(Main.java:10)


From http://vanillajava.blogspot.com/2011/08/writing-to-stdin.html

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}