One of the minor additions to JDK 9 that can make a sometimes routine task in Java even easier is the addition of the method InputStream.transferTo(OutputStream). This method, as its name suggests, allows for the easy transfer (copy) of bytes from the input stream represented by the object the method is called upon to the output stream provided to that method. Or, as the method's Javadoc comment states, InputStream.transferTo(OutputStream)
"reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read."
There is more to the Javadoc comment on the InputStream.transferTo(OutputStream)
method including these statements:
- "This method does not close either stream."
- " It is strongly recommended that both streams be promptly closed if an I/O error occurs."
The easiest way to deal with the two concerns shown above that are expressed in the Javadoc comment for the InputStream.transferTo(OutputStream)
method is to instantiate both the source InputStream and the target OutputStream in a try-with-resources statement. An example of this is shown in the next code listing.
StreamsTransfer.java
: Using InputStream.transferTo(OutputStream)
package dustin.examples.iostreams; import static java.lang.System.out; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Demonstrate InputStream.transferTo(OutputStream) added in JDK 9. */ public class StreamsTransfer { /** * Demonstrate use of InputStream.transferTo(OutputStream) using * FileInputStream and FileOutputStream implementations of * InputStream and OutputStream respectively. * * @param arguments Command-line arguments: one expected, * which is the name of the input file. */ public static void main(final String[] arguments) { if (arguments.length < 1) { out.println("USAGE StreamsTransfer <fileName>"); System.exit(-1); } final String fileName = arguments[0]; try (final InputStream is = new FileInputStream(fileName); final OutputStream os = new FileOutputStream(fileName + ".copy")) { is.transferTo(os); } catch (IOException exception) { out.println("Exception encountered: " + exception); } } }
The try-with-resources statement in the above code listing opens the two resources (instances of InputStream
and OutputStream
) and so ensures that they are always closed. The implementation of InputStream
used in this example is FileInputStream and the implementation of OutputStream
used in this example is FileOutputStream.
Although the file copying implemented in the above example could be more easily accomplished in Java with a different mechanism (such as using one of the overloaded Files.copy methods), the approach shown in the code listing above is only intended for easy illustration and can be generalized to any implementations of InputStream
and OutputStream
. For another example, Josh Bloch discusses use of InputStream.transferTo(OutputStream)
in Item 59 of Effective Java (Third Edition) and his illustration uses URL.openStream() as the InputStream
and System.out as the OutputStream
.
When the above example is executed, it will copy the provided file to another file with the same name with ".copy
" added to the end of the new file's name. The two I/O streams are closed even if an exception occurs during processing of either one.
The addition of InputStream.transferTo(OutputStream)
seems to be generally welcomed among the Java development community. Ali Dehghani talks about this method in the post "Least significant bits of Java 9." This method is also included in the post "5 things made easier in Java 9" (which also points out that effectively final variables can now be used in try-with-resources rather than explicitly making them final
like I did in my example). The Reddit /r/java subreddit includes an interesting discussion titled "New method in JDK 9: InputStream.transferTo(OutputStream)."
Not everyone is fan of the adding of InputStream.transferTo(OutputStream)
in JDK 9. In the post "Java 9: The Good, The Bad, and Private Interface Methods", Yegor Bugayenko describes InputStream
as an "already over bloated class" and writes that the addition of InputStream.transferTo(OutputStream)
is "one of the most typical mistakes young OOP programmers are making: they make their interfaces big ... just because they need more functionality." He also points that IOUtils.copy(InputStream, OutputStream) was already available via Apache Commons.
The addition of the InputStream.transferTo(OutputStream) method with JDK 9 is a small but sometimes very handy addition to the standard JDK that is especially easy to use in conjunction with the try-with-resources statement.
2 comments:
This post has been replicated at https://dzone.com/articles/transferring-inputstream-to-outputstream-in-jdk-9?fromrel=true
is it authorized ?
Shueb,
Thanks for asking and pointing this out. Fortunately, DZone did request my permission to syndicate posts that they thought would be of interest to DZone readers and I did give that permission. I like that they did ask first and I like that they provide links to the original posts when a post is syndicated.
I appreciate you taking the time to notify me of this.
Thanks!
Dustin
Post a Comment