Skip to content

Commit

Permalink
Issue 387: Use more specific "Charset" for encoding over just "String"
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jul 29, 2016
1 parent 88c2192 commit a9ecd8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.List;

import com.github.javaparser.ast.CompilationUnit;
Expand Down Expand Up @@ -58,15 +59,15 @@ public InstanceJavaParser(InputStream input) throws IOException {
this(new StreamProvider(input));
}

public InstanceJavaParser(InputStream input, String encoding) throws IOException {
this(new StreamProvider(input, encoding));
public InstanceJavaParser(InputStream input, Charset encoding) throws IOException {
this(new StreamProvider(input, encoding.name()));
}

public InstanceJavaParser(File file) throws IOException {
this(new FileInputStream(file));
}

public InstanceJavaParser(File file, String encoding) throws IOException {
public InstanceJavaParser(File file, Charset encoding) throws IOException {
this(new FileInputStream(file), encoding);
}

Expand Down
Expand Up @@ -36,8 +36,6 @@
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.Statement;


import java.io.*;
import java.nio.charset.Charset;
import java.util.List;

Expand All @@ -54,6 +52,7 @@
*/
public final class JavaParser {
private static final CommentsInserter commentsInserter = new CommentsInserter();
private static final Charset UTF8 = Charset.forName("utf-8");

private JavaParser() {
// hide the constructor
Expand All @@ -76,7 +75,7 @@ public static void setDoNotAssignCommentsPreceedingEmptyLines(boolean newValue)
}

public static CompilationUnit parse(final InputStream in,
final String encoding) throws ParseException {
final Charset encoding) throws ParseException {
return parse(in,encoding,true);
}

Expand All @@ -93,14 +92,11 @@ public static CompilationUnit parse(final InputStream in,
* if the source code has parser errors
*/
public static CompilationUnit parse(final InputStream in,
String encoding,
Charset encoding,
boolean considerComments) throws ParseException {
try {
InputStreamReader inputStreamReader = new InputStreamReader(in, encoding);
try {
try (InputStreamReader inputStreamReader = new InputStreamReader(in, encoding)) {
return parse(inputStreamReader, considerComments);
} finally {
inputStreamReader.close();
}
} catch (IOException ioe) {
throw new ParseException(ioe.getMessage());
Expand All @@ -120,10 +116,10 @@ public static CompilationUnit parse(final InputStream in,
*/
public static CompilationUnit parse(final InputStream in)
throws ParseException {
return parse(in, "UTF-8", true);
return parse(in, UTF8, true);
}

public static CompilationUnit parse(final File file, final String encoding)
public static CompilationUnit parse(final File file, final Charset encoding)
throws ParseException, IOException {
return parse(file,encoding,true);
}
Expand All @@ -140,14 +136,11 @@ public static CompilationUnit parse(final File file, final String encoding)
* @throws ParseException
* if the source code has parser errors
*/
public static CompilationUnit parse(final File file, final String encoding, boolean considerComments)
public static CompilationUnit parse(final File file, final Charset encoding, boolean considerComments)
throws ParseException {
try {
final FileInputStream in = new FileInputStream(file);
try {
try (FileInputStream in = new FileInputStream(file)) {
return parse(in, encoding, considerComments);
} finally {
in.close();
}
} catch (IOException ioe) {
throw new ParseException(ioe.getMessage());
Expand All @@ -169,7 +162,7 @@ public static CompilationUnit parse(final File file, final String encoding, bool
*/
public static CompilationUnit parse(final File file) throws ParseException,
IOException {
return parse(file, "UTF-8", true);
return parse(file, UTF8, true);
}

public static CompilationUnit parse(final Reader reader)
Expand Down

0 comments on commit a9ecd8f

Please sign in to comment.