Skip to content

Commit e62f537

Browse files
committed
[IO-318] Add Charset sister APIs to method that take a String charset name. ReversedLinesFileReader.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1307428 13f79535-47bb-0310-9956-ffa450edef68
1 parent 55bae88 commit e62f537

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.io.UnsupportedEncodingException;
2424
import java.nio.charset.Charset;
2525
import java.nio.charset.CharsetEncoder;
26+
import java.nio.charset.UnsupportedCharsetException;
27+
28+
import org.apache.commons.io.Charsets;
2629

2730
/**
2831
* Reads lines in a file reversely (similar to a BufferedReader, but starting at
@@ -33,7 +36,7 @@
3336
public class ReversedLinesFileReader implements Closeable {
3437

3538
private final int blockSize;
36-
private final String encoding;
39+
private final Charset encoding;
3740

3841
private final RandomAccessFile randomAccessFile;
3942

@@ -71,8 +74,9 @@ public ReversedLinesFileReader(final File file) throws IOException {
7174
* @param encoding
7275
* the encoding of the file
7376
* @throws IOException if an I/O error occurs
77+
* @since 2.3
7478
*/
75-
public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException {
79+
public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException {
7680
this.blockSize = blockSize;
7781
this.encoding = encoding;
7882

@@ -90,7 +94,7 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Strin
9094
currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
9195

9296
// --- check & prepare encoding ---
93-
Charset charset = Charset.forName(encoding);
97+
Charset charset = Charsets.toCharset(encoding);
9498
CharsetEncoder charsetEncoder = charset.newEncoder();
9599
float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
96100
if(maxBytesPerChar==1f) {
@@ -119,7 +123,25 @@ public ReversedLinesFileReader(final File file, final int blockSize, final Strin
119123
newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) };
120124

121125
avoidNewlineSplitBufferSize = newLineSequences[0].length;
126+
}
122127

128+
/**
129+
* Creates a ReversedLinesFileReader with the given block size and encoding.
130+
*
131+
* @param file
132+
* the file to be read
133+
* @param blockSize
134+
* size of the internal buffer (for ideal performance this should
135+
* match with the block size of the underlying file system).
136+
* @param encoding
137+
* the encoding of the file
138+
* @throws IOException if an I/O error occurs
139+
* @throws UnsupportedCharsetException
140+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
141+
* supported.
142+
*/
143+
public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException {
144+
this(file, blockSize, Charsets.toCharset(encoding));
123145
}
124146

125147
/**

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,10 +2327,10 @@ public void testIO300() throws Exception {
23272327

23282328
public void testIO276() throws Exception {
23292329
File dir = new File("target", "IO276");
2330-
assertTrue(dir+" should not be present",dir.mkdirs());
2331-
File file = new File(dir,"IO276.txt");
2332-
assertTrue(file+" should not be present",file.createNewFile());
2333-
FileUtils.forceDeleteOnExit(dir);
2330+
assertTrue(dir + " should not be present", dir.mkdirs());
2331+
File file = new File(dir, "IO276.txt");
2332+
assertTrue(file + " should not be present", file.createNewFile());
2333+
FileUtils.forceDeleteOnExit(dir);
23342334
// If this does not work, test will fail next time (assuming target is not cleaned)
23352335
}
23362336

0 commit comments

Comments
 (0)