You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/03/30 16:01:45 UTC
svn commit: r1307428 - in /commons/proper/io/trunk/src:
main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
test/java/org/apache/commons/io/FileUtilsTestCase.java
Author: ggregory
Date: Fri Mar 30 14:01:45 2012
New Revision: 1307428
URL: http://svn.apache.org/viewvc?rev=1307428&view=rev
Log:
[IO-318] Add Charset sister APIs to method that take a String charset name. ReversedLinesFileReader.
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.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=1307428&r1=1307427&r2=1307428&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 Fri Mar 30 14:01:45 2012
@@ -23,6 +23,9 @@ import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
+import java.nio.charset.UnsupportedCharsetException;
+
+import org.apache.commons.io.Charsets;
/**
* Reads lines in a file reversely (similar to a BufferedReader, but starting at
@@ -33,7 +36,7 @@ import java.nio.charset.CharsetEncoder;
public class ReversedLinesFileReader implements Closeable {
private final int blockSize;
- private final String encoding;
+ private final Charset encoding;
private final RandomAccessFile randomAccessFile;
@@ -71,8 +74,9 @@ public class ReversedLinesFileReader imp
* @param encoding
* the encoding of the file
* @throws IOException if an I/O error occurs
+ * @since 2.3
*/
- public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException {
+ public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException {
this.blockSize = blockSize;
this.encoding = encoding;
@@ -90,7 +94,7 @@ public class ReversedLinesFileReader imp
currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
// --- check & prepare encoding ---
- Charset charset = Charset.forName(encoding);
+ Charset charset = Charsets.toCharset(encoding);
CharsetEncoder charsetEncoder = charset.newEncoder();
float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
if(maxBytesPerChar==1f) {
@@ -119,7 +123,25 @@ public class ReversedLinesFileReader imp
newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) };
avoidNewlineSplitBufferSize = newLineSequences[0].length;
+ }
+ /**
+ * Creates a ReversedLinesFileReader with the given block size and encoding.
+ *
+ * @param file
+ * the file to be read
+ * @param blockSize
+ * size of the internal buffer (for ideal performance this should
+ * match with the block size of the underlying file system).
+ * @param encoding
+ * the encoding of the file
+ * @throws IOException if an I/O error occurs
+ * @throws UnsupportedCharsetException
+ * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
+ * supported.
+ */
+ public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException {
+ this(file, blockSize, Charsets.toCharset(encoding));
}
/**
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1307428&r1=1307427&r2=1307428&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Fri Mar 30 14:01:45 2012
@@ -2327,10 +2327,10 @@ public class FileUtilsTestCase extends F
public void testIO276() throws Exception {
File dir = new File("target", "IO276");
- assertTrue(dir+" should not be present",dir.mkdirs());
- File file = new File(dir,"IO276.txt");
- assertTrue(file+" should not be present",file.createNewFile());
- FileUtils.forceDeleteOnExit(dir);
+ assertTrue(dir + " should not be present", dir.mkdirs());
+ File file = new File(dir, "IO276.txt");
+ assertTrue(file + " should not be present", file.createNewFile());
+ FileUtils.forceDeleteOnExit(dir);
// If this does not work, test will fail next time (assuming target is not cleaned)
}