Skip to content

Commit

Permalink
Removes direct use of FileInputStream to prevent GC overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
albfernandez committed Dec 6, 2017
1 parent 02eaa48 commit aedaf63
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class TestDetector
public static void main(String[] args)
{
byte[] buf = new byte[4096];
java.io.FileInputStream fis = new java.io.FileInputStream("test.txt");
java.io.InputStream fis = java.nio.file.Files.newInputStream(java.nio.file.Paths.get("test.txt"));

// (1)
UniversalDetector detector = new UniversalDetector();
Expand Down Expand Up @@ -152,7 +152,7 @@ public class TestCreateReaderFromFile {
java.io.Reader reader = null;
try {
java.io.File file = new java.io.File(args[0]);
reader = ReaderFactory.createReaderFromFile(file);
reader = ReaderFactory.createBufferedReader(file);

// Do whatever you want with the reader
}
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/org/mozilla/universalchardet/ReaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
package org.mozilla.universalchardet;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

/**
Expand All @@ -50,22 +52,45 @@ private ReaderFactory() {
* @param file The file to read from
* @param defaultCharset defaultCharset to use if can't be determined
*/
public static Reader createReaderFromFile(File file, Charset defaultCharset) throws IOException {

public static BufferedReader createBufferedReader(File file, Charset defaultCharset) throws IOException {
Charset cs = Objects.requireNonNull(defaultCharset, "defaultCharset must be not null");
String detectedEncoding = UniversalDetector.detectCharset(file);
if (detectedEncoding != null) {
cs = Charset.forName(detectedEncoding);
}
if (!cs.toString().contains("UTF")) {
return new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), cs);
return Files.newBufferedReader(file.toPath(), cs);
}
return new InputStreamReader(new UnicodeBOMInputStream(new BufferedInputStream(new FileInputStream(file))), cs);
Path path = file.toPath();
return new BufferedReader(new InputStreamReader(new UnicodeBOMInputStream(new BufferedInputStream(Files.newInputStream(path))), cs));
}
/**
* Create a reader from a file with correct encoding. If charset cannot be determined,
* it uses the system default charset.
* @param file The file to read from
*/
public static BufferedReader createBufferedReader(File file) throws IOException {
return createBufferedReader(file, Charset.defaultCharset());
}

/**
* Create a reader from a file with correct encoding
* @param file The file to read from
* @param defaultCharset defaultCharset to use if can't be determined
* @deprecated Use {@link #createBufferedReader(File, Charset)}
*/
@Deprecated
public static Reader createReaderFromFile(File file, Charset defaultCharset) throws IOException {
return createBufferedReader(file, defaultCharset);
}
/**
* Create a reader from a file with correct encoding. If charset cannot be determined,
* it uses the system default charset.
* @param file The file to read from
* @deprecated Use {@link #createBufferedReader(File)}
*/
@Deprecated
public static Reader createReaderFromFile(File file) throws IOException {
return createReaderFromFile(file, Charset.defaultCharset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@

package org.mozilla.universalchardet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import org.mozilla.universalchardet.prober.CharsetProber;
import org.mozilla.universalchardet.prober.EscCharsetProber;
Expand Down Expand Up @@ -325,7 +327,7 @@ public void reset()

public static String detectCharset(File file) throws IOException {

try (FileInputStream fis = new FileInputStream(file)) {
try (InputStream fis = new BufferedInputStream(Files.newInputStream(file.toPath()))) {

byte[] buf = new byte[4096];

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/mozilla/universalchardet/BOMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private String getFirstLine(String testFileName) throws IOException{
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(ReaderFactory.createReaderFromFile(file));
reader = ReaderFactory.createBufferedReader(file);
// return first line
return reader.readLine();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.mozilla.universalchardet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -64,7 +65,7 @@ private String getFileEncoding(String testFileName) throws IOException{
EncodingDetectorInputStream edis = null;
EncodingDetectorOutputStream edos = null;
try {
edis = new EncodingDetectorInputStream(new FileInputStream(file));
edis = new EncodingDetectorInputStream(new BufferedInputStream(Files.newInputStream(file.toPath())));
edos = new EncodingDetectorOutputStream(NullOutputStream.NULL_OUTPUT_STREAM);
byte[] buffer = new byte[1024];
int read = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/mozilla/universalchardet/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private String getSecondLine(String testFileName) throws IOException{
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(ReaderFactory.createReaderFromFile(file));
reader = ReaderFactory.createBufferedReader(file);
// Skip first line
reader.readLine();
// return second line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void main (String[] args) throws java.io.IOException {
java.io.Reader reader = null;
try {
java.io.File file = new java.io.File(args[0]);
reader = ReaderFactory.createReaderFromFile(file);
reader = ReaderFactory.createBufferedReader(file);

// Do whatever you want with the reader
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
* ***** END LICENSE BLOCK ***** */
package org.mozilla.universalchardet.example;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.mozilla.universalchardet.UniversalDetector;

Expand All @@ -50,7 +52,7 @@ public static void main(String[] args) throws java.io.IOException {

byte[] buf = new byte[4096];
String fileName = args[0];
try (FileInputStream fis = new FileInputStream(fileName)) {
try (InputStream fis = Files.newInputStream(Paths.get(fileName))) {

// (1)
UniversalDetector detector = new UniversalDetector();
Expand Down

0 comments on commit aedaf63

Please sign in to comment.