You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by kr...@apache.org on 2015/12/07 18:41:27 UTC

svn commit: r1718429 - /commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Author: krosenvold
Date: Mon Dec  7 17:41:27 2015
New Revision: 1718429

URL: http://svn.apache.org/viewvc?rev=1718429&view=rev
Log:
Changed order of checking supported charset and opening file.

This avoids file handle leak in testcase when unsupported charsets would leave file open

(Could also happen in real code, but probably not in real-life issues)

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java?rev=1718429&r1=1718428&r2=1718429&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java Mon Dec  7 17:41:27 2015
@@ -96,19 +96,6 @@ public class ReversedLinesFileReader imp
         this.blockSize = blockSize;
         this.encoding = encoding;
 
-        randomAccessFile = new RandomAccessFile(file, "r");
-        totalByteLength = randomAccessFile.length();
-        int lastBlockLength = (int) (totalByteLength % blockSize);
-        if (lastBlockLength > 0) {
-            totalBlockCount = totalByteLength / blockSize + 1;
-        } else {
-            totalBlockCount = totalByteLength / blockSize;
-            if (totalByteLength > 0) {
-                lastBlockLength = blockSize;
-            }
-        }
-        currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
-
         // --- check & prepare encoding ---
         final Charset charset = Charsets.toCharset(encoding);
         final CharsetEncoder charsetEncoder = charset.newEncoder();
@@ -121,7 +108,7 @@ public class ReversedLinesFileReader imp
             // http://en.wikipedia.org/wiki/UTF-8
             byteDecrement = 1;
         } else if(charset == Charset.forName("Shift_JIS") || // Same as for UTF-8
-                                      // http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
+                // http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
                 charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
                 charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
                 charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
@@ -138,10 +125,26 @@ public class ReversedLinesFileReader imp
             throw new UnsupportedEncodingException("Encoding " + encoding + " is not supported yet (feel free to " +
                     "submit a patch)");
         }
+
         // NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n
         newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) };
 
         avoidNewlineSplitBufferSize = newLineSequences[0].length;
+
+        // Open file
+        randomAccessFile = new RandomAccessFile(file, "r");
+        totalByteLength = randomAccessFile.length();
+        int lastBlockLength = (int) (totalByteLength % blockSize);
+        if (lastBlockLength > 0) {
+            totalBlockCount = totalByteLength / blockSize + 1;
+        } else {
+            totalBlockCount = totalByteLength / blockSize;
+            if (totalByteLength > 0) {
+                lastBlockLength = blockSize;
+            }
+        }
+        currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
+
     }
 
     /**