Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8304561

Need way to create non-closable FileOutputStream on standard streams

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When creating a FileOutputStream through the FileOutputStream(FileDescriptor fdObj) constructor and passing e.g. FileDescriptor.out as the constructor argument, that output stream will end up in the otherParents list of the file descriptor and never be removed unless you close the output stream. However, closing it has the unwanted side effect of closing the standard out file descriptor so that it is no longer possible to write to standard output.

      I consider this a bug in the java.io API. There needs to be a way to detach a FileOutputStream from the FileDescriptor without closing the file descriptor, so that the FileOutputStream instance can be garbage collected (or perhaps a viable alternative would be that FileDescriptor uses weak references and cleans up unreachable objects in its attach() method).

      My use case is that I want to efficiently write to standard output as a FileChannel, without the intermediate buffering and character set conversions performed by PrintStream and StreamByteBuffer. So I have the code:

      var os = new FileOutputStream(FileDescriptor.out);
      var channel = os.getChannel();

      and then write to the channel using ByteBuffer operations. But once the FileOutputStream object has been created there is no way for me to get rid of it.


      FREQUENCY : always


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: