import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
String param2 = "plouf";
Date param3 = new Date(System.currentTimeMillis());
Timestamp param4 = new Timestamp(System.currentTimeMillis());
- byte[] param5 = "blob".getBytes("UTF-8");
+ byte[] param5 = "blob".getBytes(UTF_8);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement);
package org.sonar.db.ce;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.sonar.core.util.CloseableIterator;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
public class LogsIteratorInputStreamTest {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("LogsIterator can't be empty or already read");
- new LogsIteratorInputStream(iterator, StandardCharsets.UTF_8);
+ new LogsIteratorInputStream(iterator, UTF_8);
}
private static LogsIteratorInputStream create(String... lines) {
- return new LogsIteratorInputStream(CloseableIterator.from(Arrays.asList(lines).iterator()), StandardCharsets.UTF_8);
+ return new LogsIteratorInputStream(CloseableIterator.from(Arrays.asList(lines).iterator()), UTF_8);
}
private static String read(LogsIteratorInputStream logsIteratorInputStream) throws IOException {
- return IOUtils.toString(logsIteratorInputStream, "UTF-8");
+ return IOUtils.toString(logsIteratorInputStream, UTF_8);
}
}
try {
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CRYPTO_KEY);
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, loadSecretFile());
- return Base64.encodeBase64String(cipher.doFinal(clearText.getBytes("UTF-8")));
+ return Base64.encodeBase64String(cipher.doFinal(clearText.getBytes(UTF_8)));
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
package org.sonar.server.badge.ws;
-import java.nio.charset.Charset;
+import static java.nio.charset.StandardCharsets.UTF_8;
public class ETagUtils {
// Format for Expires Header
* <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.11"></a>
*/
static String getETag(String output) {
- return "W/" + hash(output.getBytes(Charset.forName("UTF-8")));
+ return "W/" + hash(output.getBytes(UTF_8));
}
}
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import javax.xml.XMLConstants;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
private static String prettyFormatXml(String xml) {
try {
- Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
+ TransformerFactory factory = SAXTransformerFactory.newInstance();
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ Transformer serializer = factory.newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
json.endObject();
}
- private String measureValue(CustomMeasureDto measure, MetricDto metric) {
+ private static String measureValue(CustomMeasureDto measure, MetricDto metric) {
Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType());
Double doubleValue = measure.getValue();
String stringValue = measure.getTextValue();
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginRepository;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
@ScannerSide
public class DefaultI18n implements I18n, Startable {
private static String readInputStream(String filePath, InputStream input) {
String result;
try {
- result = IOUtils.toString(input, "UTF-8");
+ result = IOUtils.toString(input, UTF_8);
} catch (IOException e) {
throw new SonarException("Fail to load file: " + filePath, e);
} finally {
}
Path targetDirNormalizedPath = toDir.toPath().normalize();
- ZipInputStream zipStream = new ZipInputStream(stream);
- try {
+ try (ZipInputStream zipStream = new ZipInputStream(stream)) {
ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
if (filter.test(entry)) {
}
}
return toDir;
-
- } finally {
- zipStream.close();
}
}
}
Path targetDirNormalizedPath = toDir.toPath().normalize();
- ZipFile zipFile = new ZipFile(zip);
- try {
+ try (ZipFile zipFile = new ZipFile(zip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
}
}
return toDir;
-
- } finally {
- zipFile.close();
}
}
private static void copy(ZipInputStream zipStream, File to) throws IOException {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(to);
+ try (OutputStream fos = new FileOutputStream(to)) {
IOUtils.copy(zipStream, fos);
- } finally {
- IOUtils.closeQuietly(fos);
}
}
private static void copy(ZipFile zipFile, ZipEntry entry, File to) throws IOException {
- FileOutputStream fos = new FileOutputStream(to);
- InputStream input = null;
- try {
- input = zipFile.getInputStream(entry);
+ try (InputStream input = zipFile.getInputStream(entry); OutputStream fos = new FileOutputStream(to)) {
IOUtils.copy(input, fos);
- } finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(fos);
}
}
public static void zipDir(File dir, File zip) throws IOException {
- OutputStream out = null;
- ZipOutputStream zout = null;
- try {
- out = FileUtils.openOutputStream(zip);
- zout = new ZipOutputStream(out);
+ try (OutputStream out = FileUtils.openOutputStream(zip);
+ ZipOutputStream zout = new ZipOutputStream(out)) {
doZipDir(dir, zout);
-
- } finally {
- IOUtils.closeQuietly(zout);
- IOUtils.closeQuietly(out);
}
}