You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2019/10/12 12:08:42 UTC

[jspwiki] 01/05: changes on FileUtils:

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit 283cf393a47d1e60a6a75948cb317d0c0b1fab32
Author: juanpablo <ju...@apache.org>
AuthorDate: Sat Oct 12 14:01:08 2019 +0200

    changes on FileUtils:
    
    * readContents uses try with resources
    * buffer doubles its size so it copyContents(InputStream, OutputStream) method mimics private Files.copyContents(InputStream, OutputStream) more closely
    * copyContents(InputStream, OutputStream) enforces writes to disk for FileOutputStream through their associated FileDescriptor
---
 .../main/java/org/apache/wiki/util/FileUtil.java   | 32 +++++++---------------
 1 file changed, 10 insertions(+), 22 deletions(-)

diff --git a/jspwiki-util/src/main/java/org/apache/wiki/util/FileUtil.java b/jspwiki-util/src/main/java/org/apache/wiki/util/FileUtil.java
index 30c1354..89f0299 100644
--- a/jspwiki-util/src/main/java/org/apache/wiki/util/FileUtil.java
+++ b/jspwiki-util/src/main/java/org/apache/wiki/util/FileUtil.java
@@ -23,6 +23,7 @@ import org.apache.log4j.Logger;
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileDescriptor;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -47,7 +48,7 @@ import java.nio.charset.StandardCharsets;
 public final class FileUtil {
 
     /** Size of the buffer used when copying large chunks of data. */
-    private static final int      BUFFER_SIZE = 4096;
+    private static final int      BUFFER_SIZE = 8192;
     private static final Logger   log         = Logger.getLogger(FileUtil.class);
 
     /**
@@ -166,6 +167,12 @@ public final class FileUtil {
         }
 
         out.flush();
+
+        // FileOutputStream.flush is an empty method, so in this case we grab the underlying file descriptor and force from there thw write to disk
+        if( out instanceof FileOutputStream ) {
+            FileDescriptor fd = ( ( FileOutputStream )out ).getFD();
+            fd.sync();
+        }
     }
 
     /**
@@ -231,30 +238,11 @@ public final class FileUtil {
      *  @return String read from the Reader
      *  @throws IOException If reading fails.
      */
-    public static String readContents( Reader in )
-        throws IOException
-    {
-        Writer out = null;
-
-        try
-        {
-            out = new StringWriter();
-
+    public static String readContents( final Reader in ) throws IOException {
+        try( Writer out = new StringWriter() ) {
             copyContents( in, out );
-
             return out.toString();
         }
-        finally
-        {
-            try
-            {
-                out.close();
-            }
-            catch( Exception e )
-            {
-                log.error("Not able to close the stream while reading contents.");
-            }
-        }
     }
 
     /**