DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Writing to stdin

Writing to stdin

Peter Lawrey user avatar by
Peter Lawrey
·
Aug. 29, 11 · Interview
Like (0)
Save
Tweet
Share
6.98K Views

Join the DZone community and get the full member experience.

Join For Free

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());
bb.putLong(System.nanoTime());
bb.force();
raf.close();

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
                                         \n
0000011
$ java -cp . Main < file.dat
$ od -xc file.dat
0000000    0000    5522    11a9    79c6    000a
         \0  \0   "   U 251 021 306   y  \n
0000011
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)
</init></init>

 

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

Memory map Memory (storage engine) Linux (operating system) Java (programming language) Stream (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Steps for Getting Started in Deep Learning
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: