Don't use File(In/Out)putStream in FileBackedOutputStream 58/56958/2
authorTom Pantelis <tompantelis@gmail.com>
Fri, 12 May 2017 15:13:19 +0000 (11:13 -0400)
committerRobert Varga <nite@hq.sk>
Fri, 12 May 2017 18:53:43 +0000 (18:53 +0000)
As per https://www.cloudbees.com/blog/fileinputstream-fileoutputstream-considered-harmful
FileInputStream/FileOutputStream can incur unnecessary GC overhead due to
finalize. Use Files.newInputStream and Files.newOutputStream instead.

Change-Id: Ic4130ba650fef312e82a5039e9e11a573bd9d406
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/io/FileBackedOutputStream.java

index 452611b..38249da 100644 (file)
@@ -16,11 +16,10 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import java.util.Iterator;
 import java.util.Set;
 import javax.annotation.Nonnull;
@@ -100,7 +99,7 @@ public class FileBackedOutputStream extends OutputStream {
                 public InputStream openStream() throws IOException {
                     synchronized (FileBackedOutputStream.this) {
                         if (file != null) {
-                            return new FileInputStream(file);
+                            return Files.newInputStream(file.toPath());
                         } else {
                             return new ByteArrayInputStream(memory.getBuffer(), 0, memory.getCount());
                         }
@@ -201,15 +200,16 @@ public class FileBackedOutputStream extends OutputStream {
         }
 
         if (file == null && memory.getCount() + len > fileThreshold) {
-            File temp = File.createTempFile("FileBackedOutputStream", null, new File(fileDirectory));
+            File temp = File.createTempFile("FileBackedOutputStream", null,
+                    fileDirectory == null ? null : new File(fileDirectory));
             temp.deleteOnExit();
 
             LOG.debug("Byte count {} has exceeded threshold {} - switching to file: {}", memory.getCount() + len,
                     fileThreshold, temp);
 
-            FileOutputStream transfer = null;
+            OutputStream transfer = null;
             try {
-                transfer = new FileOutputStream(temp);
+                transfer = Files.newOutputStream(temp.toPath());
                 transfer.write(memory.getBuffer(), 0, memory.getCount());
                 transfer.flush();
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.