[infer][java] fix model of InputStreamReader to take into account charset given in lowercase

Summary:
The models for InputStreamReader and OutputStreamWriter are taking into consideration the charset passed as parameter in order to follow the exception branch when the charset is not valid. However, the previsous models were only considering encoding literals with uppercase letters. This diff adds the lowercase encoding names to the list.

Closes https://github.com/facebook/infer/issues/127
master
jrm 10 years ago
parent ef27abcc8f
commit 392cd0dee0

@ -0,0 +1,24 @@
// Copyright (c) 2015-Present Facebook. All rights reserved.
package com.facebook.infer.models;
public class InferUtils {
public static boolean isValidCharset(String charsetName) {
return charsetName == "UTF8"
|| charsetName == "utf8"
|| charsetName == "UTF-8"
|| charsetName == "utf-8"
|| charsetName == "US-ASCII"
|| charsetName == "us-ascii"
|| charsetName == "ISO-8859-1"
|| charsetName == "iso-8859-1"
|| charsetName == "UTF-16BE"
|| charsetName == "utf-16be"
|| charsetName == "UTF-16LE"
|| charsetName == "utf-16le"
|| charsetName == "UTF-16"
|| charsetName == "utf-16";
}
}

@ -1,3 +1,5 @@
// Copyright (c) 2015-Present Facebook. All rights reserved.
package java.io;
public class ByteArrayInputStream extends InputStream implements Closeable {

@ -1,3 +1,5 @@
// Copyright (c) 2015-Present Facebook. All rights reserved.
package java.io;
public class ByteArrayOutputStream extends OutputStream implements Closeable {

@ -6,6 +6,7 @@
package java.io;
import com.facebook.infer.models.InferUndefined;
import com.facebook.infer.models.InferUtils;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
@ -22,10 +23,7 @@ public class InputStreamReader extends Reader {
throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException("charsetName");
else if (charsetName == "UTF8" || charsetName == "UTF-8"
|| charsetName == "US-ASCII" || charsetName == "ISO-8859-1"
|| charsetName == "UTF-16BE" || charsetName == "UTF-16LE"
|| charsetName == "UTF-16") {
else if (InferUtils.isValidCharset(charsetName)) {
this.in = in;
} else
throw new UnsupportedEncodingException();

@ -6,6 +6,7 @@
package java.io;
import com.facebook.infer.models.InferUndefined;
import com.facebook.infer.models.InferUtils;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
@ -18,10 +19,7 @@ public class OutputStreamWriter extends Writer {
throws UnsupportedEncodingException {
if (charsetName == null)
throw new NullPointerException("charsetName");
else if (charsetName == "UTF8" || charsetName == "UTF-8"
|| charsetName == "US-ASCII" || charsetName == "ISO-8859-1"
|| charsetName == "UTF-16BE" || charsetName == "UTF-16LE"
|| charsetName == "UTF-16") {
else if (InferUtils.isValidCharset(charsetName)) {
this.out = out;
} else
throw new UnsupportedEncodingException();

@ -12,6 +12,7 @@ import com.google.common.base.Preconditions;
import com.google.common.io.Closeables;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
@ -20,6 +21,7 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
@ -880,4 +882,24 @@ public class ResourceLeaks {
}
}
public InputStreamReader withCharset(URLConnection urlConnection) {
InputStreamReader reader = null;
try {
reader = new InputStreamReader(
urlConnection.getInputStream(),
"iso-8859-1");
} catch (Exception e) {
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// do nothing
}
}
}
return reader;
}
}

Loading…
Cancel
Save