Skip to content

Commit 109a557

Browse files
committed
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) git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1718429 13f79535-47bb-0310-9956-ffa450edef68
1 parent bf1d3e4 commit 109a557

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,6 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Chars
9696
this.blockSize = blockSize;
9797
this.encoding = encoding;
9898

99-
randomAccessFile = new RandomAccessFile(file, "r");
100-
totalByteLength = randomAccessFile.length();
101-
int lastBlockLength = (int) (totalByteLength % blockSize);
102-
if (lastBlockLength > 0) {
103-
totalBlockCount = totalByteLength / blockSize + 1;
104-
} else {
105-
totalBlockCount = totalByteLength / blockSize;
106-
if (totalByteLength > 0) {
107-
lastBlockLength = blockSize;
108-
}
109-
}
110-
currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
111-
11299
// --- check & prepare encoding ---
113100
final Charset charset = Charsets.toCharset(encoding);
114101
final CharsetEncoder charsetEncoder = charset.newEncoder();
@@ -121,7 +108,7 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Chars
121108
// http://en.wikipedia.org/wiki/UTF-8
122109
byteDecrement = 1;
123110
} else if(charset == Charset.forName("Shift_JIS") || // Same as for UTF-8
124-
// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
111+
// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
125112
charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
126113
charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
127114
charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
@@ -138,10 +125,26 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Chars
138125
throw new UnsupportedEncodingException("Encoding " + encoding + " is not supported yet (feel free to " +
139126
"submit a patch)");
140127
}
128+
141129
// NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n
142130
newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) };
143131

144132
avoidNewlineSplitBufferSize = newLineSequences[0].length;
133+
134+
// Open file
135+
randomAccessFile = new RandomAccessFile(file, "r");
136+
totalByteLength = randomAccessFile.length();
137+
int lastBlockLength = (int) (totalByteLength % blockSize);
138+
if (lastBlockLength > 0) {
139+
totalBlockCount = totalByteLength / blockSize + 1;
140+
} else {
141+
totalBlockCount = totalByteLength / blockSize;
142+
if (totalByteLength > 0) {
143+
lastBlockLength = blockSize;
144+
}
145+
}
146+
currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
147+
145148
}
146149

147150
/**

0 commit comments

Comments
 (0)