Skip to content

Commit

Permalink
Issue #1566: Reduce FileText's constructor length
Browse files Browse the repository at this point in the history
  • Loading branch information
baratali authored and romani committed Aug 26, 2015
1 parent 3fca3c9 commit d6bdd7e
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/main/java/com/puppycrawl/tools/checkstyle/api/FileText.java
Expand Up @@ -129,24 +129,7 @@ public FileText(File file, String charsetName) throws IOException {
throw ex2;
}

final StringBuilder buf = new StringBuilder();
final FileInputStream stream = new FileInputStream(file);
final Reader reader = new InputStreamReader(stream, decoder);
try {
final char[] chars = new char[READ_BUFFER_SIZE];
while (true) {
final int len = reader.read(chars);
if (len < 0) {
break;
}
buf.append(chars, 0, len);
}
}
finally {
Closeables.closeQuietly(reader);
}
// buf.trimToSize(); // could be used instead of toString().
fullText = buf.toString();
fullText = readFile(file, decoder);

// Use the BufferedReader to break down the lines as this
// is about 30% faster than using the
Expand Down Expand Up @@ -200,6 +183,34 @@ public FileText(FileText fileText) {
lineBreaks = ArrayUtils.clone(fileText.lineBreaks);
}

/**
* Reads file using specific decoder and returns all its content as a String.
* @param inputFile File to read
* @param decoder Charset decoder
* @return File's text
* @throws IOException Unable to open or read the file
*/
private String readFile(final File inputFile, final CharsetDecoder decoder)
throws IOException {
final StringBuilder buf = new StringBuilder();
final FileInputStream stream = new FileInputStream(inputFile);
final Reader reader = new InputStreamReader(stream, decoder);
try {
final char[] chars = new char[READ_BUFFER_SIZE];
while (true) {
final int len = reader.read(chars);
if (len < 0) {
break;
}
buf.append(chars, 0, len);
}
}
finally {
Closeables.closeQuietly(reader);
}
return buf.toString();
}

/**
* Compatibility conversion.
*
Expand Down

0 comments on commit d6bdd7e

Please sign in to comment.