2323import java .io .UnsupportedEncodingException ;
2424import java .nio .charset .Charset ;
2525import 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
3336public 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 /**
0 commit comments