Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[FIXED JENKINS-16222] Ignore invalid encodings when reading files.
  • Loading branch information
uhafner committed Feb 13, 2013
1 parent 7e79c79 commit 3b9fbe464d1b04c8a2520652fb94e855b8a5ba91
@@ -19,6 +19,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Project;

import hudson.plugins.analysis.util.EncodingValidator;
import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.analysis.util.model.AbstractAnnotation;
import hudson.plugins.analysis.util.model.AnnotationContainer;
@@ -450,7 +451,7 @@ private void logExceptionToFile(final IOException exception, final File masterFi

private void print(final FileOutputStream outputStream, final String message,
final Object... arguments) throws IOException {
IOUtils.write(String.format(message, arguments), outputStream, getDefaultEncoding());
IOUtils.write(String.format(message, arguments), outputStream, EncodingValidator.getEncoding(getDefaultEncoding()));
}

/**
@@ -11,6 +11,8 @@
import java.util.HashSet;
import java.util.Set;

import javax.annotation.CheckForNull;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;
@@ -31,7 +33,8 @@ public class EncodingValidator implements Validator {

static {
try {
allCharacterSets = Collections.unmodifiableSet(new HashSet<String>(Charset.availableCharsets().keySet()));
allCharacterSets = Collections.unmodifiableSet(new HashSet<String>(Charset
.availableCharsets().keySet()));
}
// CHECKSTYLE:OFF
catch (Exception exception) {
@@ -54,11 +57,11 @@ public static Set<String> getAvailableCharsets() {
* default encoding is empty or <code>null</code>, or if the charset is not
* valid then the default encoding of the platform is returned.
*
* @param defaultEncoding identifier of the character set
*
* @param defaultEncoding
* identifier of the character set
* @return the default charset for the specified encoding string
*/
public static Charset defaultCharset(final String defaultEncoding) {
public static Charset defaultCharset(@CheckForNull final String defaultEncoding) {
try {
if (StringUtils.isNotBlank(defaultEncoding)) {
return Charset.forName(defaultEncoding);
@@ -88,7 +91,8 @@ public static Charset defaultCharset(final String defaultEncoding) {
* Signals that an I/O exception has occurred during reading of
* the file.
*/
public static LineIterator readFile(final String fileName, final String encoding) throws FileNotFoundException, IOException {
public static LineIterator readFile(final String fileName, @CheckForNull final String encoding)
throws FileNotFoundException, IOException {
FileInputStream stream = new FileInputStream(new File(fileName));
if (StringUtils.isNotBlank(encoding)) {
return IOUtils.lineIterator(stream, encoding);
@@ -122,5 +126,20 @@ public FormValidation check(final String encoding) throws FormValidation {
}
throw FormValidation.error(Messages.FieldValidator_Error_DefaultEncoding());
}
}

/**
* Returns the encoding used to read a file. If the specified
* encoding is empty or <code>null</code>, or if the encoding is not valid
* then the default encoding of the platform is returned.
*
* @param encoding
* identifier of the character set
* @return the default encoding for the specified encoding string
*/
public static String getEncoding(@CheckForNull final String encoding) {
if (StringUtils.isNotBlank(encoding) && Charset.forName(encoding).canEncode()) {
return encoding;
}
return Charset.defaultCharset().name();
}
}
@@ -1,5 +1,9 @@
package hudson.plugins.analysis.util;

import static org.junit.Assert.*;

import java.nio.charset.Charset;

import org.junit.Test;

/**
@@ -32,6 +36,17 @@ public void testInvalidEncodings() {
assertThatInputIsInvalid("ISO-8859-42");
}

/**
* Verifies that the platform encoding is used if encoding is invalid.
*/
@Test
public void testDefaultEncoding() {
assertEquals("Wrong encoding used", "UTF-8", EncodingValidator.getEncoding("UTF-8"));
String osCharset = Charset.defaultCharset().name();
assertEquals("Wrong encoding used", osCharset, EncodingValidator.getEncoding(""));
assertEquals("Wrong encoding used", osCharset, EncodingValidator.getEncoding(null));
}

@Override
protected Validator createValidator() {
return new EncodingValidator();

0 comments on commit 3b9fbe4

Please sign in to comment.