JDK-4274770 : FileDescriptor.dup() to restore io, out, and err streams
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1999-09-23
  • Updated: 2007-11-15
  • Resolved: 2007-11-15
Related Reports
Duplicate :  
Relates :  
Description
Currently its impossible to restore the standard io streams if they have been closed. One needs the flexibility to temporarily close the standard io and make System.in (out and err) use different streams and then restore the standard io streams. 

This can be achieved by duplicating FileDescriptors, i.e have the ability to create an additional entry in the File descriptor table pointing to the same file. Thus even if one of the file descriptors is freed one can still use the duplicate fd to reestablish a file stream. 

The FileDescriptors System.in(out and err) are set to -1, if the FileStream using them is closed, rendering them useless. There is no other portable way of dealing with stdio other than using file descriptors.

Assuming FileDescriptor.dup() exists, the code could look like,

....
FileDescriptor backupStdin = FileDescriptor.in.dup();
System.in.close();

// Set a different stream (file) to be used as stdin
System.setIn(....)

... Do Stuff using System.in ....

// Restore Stdin using backed up fd.
System.setIn(new BufferedInputStream(new FileInputStream(backupStdin));

... Now use regular Stdin, with System.in



naveen.sanjeeva@eng 1999-09-22

Comments
EVALUATION Yes, this is essentially the same issue as 4209999.
15-11-2007

EVALUATION Possible duplicate of 4209999.
25-08-2005