Skip to content

Commit

Permalink
Merge pull request #25119 from dmatej/common-util-vs-spotbugs
Browse files Browse the repository at this point in the history
The common-util: Fixed dependencies on default charset
  • Loading branch information
dmatej authored Aug 28, 2024
2 parents 47579ba + 66d95bc commit 1e81265
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 299 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -39,6 +39,7 @@
import static com.sun.enterprise.admin.servermgmt.services.Constants.START_ARG_START;
import static com.sun.enterprise.admin.servermgmt.services.Constants.STOP_ARG_END;
import static com.sun.enterprise.admin.servermgmt.services.Constants.STOP_ARG_START;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Warning: there is lots of file twiddling going on in this class. It is the nature of the beast.
Expand Down Expand Up @@ -300,7 +301,7 @@ private void install() throws ProcessManagerException {
if (info.dryRun) {
try {
// dry-run not so useful on Windows. Very useful on UNIX...
xmlFileCopy = Strings.get("xmlfiledump") + FileUtils.readSmallFile(targetXml);
xmlFileCopy = Strings.get("xmlfiledump") + FileUtils.readSmallFile(targetXml, UTF_8);
} catch (IOException ex) {
// oh well....
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -29,13 +29,14 @@

import org.glassfish.api.admin.CommandException;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

/**
* @author Rajiv Mordani
*/
public class SSHUtil {

private static final List<Session> activeConnections = new ArrayList<>();
private static final String NL = System.lineSeparator();

/**
* Registers a connection for cleanup when the plugin is stopped.
Expand Down Expand Up @@ -98,9 +99,9 @@ public static File getDefaultKeyFile() {
public static boolean isEncryptedKey(File keyFile) throws CommandException {
boolean res = false;
try {
String f = FileUtils.readSmallFile(keyFile);
String f = FileUtils.readSmallFile(keyFile, ISO_8859_1).trim();
if (f.startsWith("-----BEGIN ") && f.contains("ENCRYPTED")
&& f.endsWith(" PRIVATE KEY-----" + NL)) {
&& f.endsWith(" PRIVATE KEY-----")) {
res=true;
}
}
Expand All @@ -124,11 +125,11 @@ public static boolean validateKeyFile(File file) throws CommandException {
if (!file.getName().endsWith(".pub")) {
String key = null;
try {
key = FileUtils.readSmallFile(file);
key = FileUtils.readSmallFile(file, ISO_8859_1).trim();
} catch (IOException ioe) {
throw new CommandException(Strings.get("unable.to.read.key", file, ioe.getMessage()));
}
if (!key.startsWith("-----BEGIN ") && !key.endsWith(" PRIVATE KEY-----" + NL)) {
if (!key.startsWith("-----BEGIN ") && !key.endsWith(" PRIVATE KEY-----")) {
throw new CommandException(Strings.get("invalid.key.file", file));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -31,6 +31,8 @@
import java.util.Map;
import java.util.Properties;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

/**
* This class provides static methods to make accessible the version as well as
* the individual parts that make up the version
Expand Down Expand Up @@ -232,7 +234,7 @@ private static Properties loadVersionProp() {
File directory = new File(installRoot).toPath().resolve(Path.of("config", "branding")).toFile();
if (directory.isDirectory()) {
for (File file : directory.listFiles(f1 -> f1.getName().endsWith(".properties") && f1.canRead())) {
try (FileReader fr = new FileReader(file)) {
try (FileReader fr = new FileReader(file, ISO_8859_1)) {
Properties p = new Properties();
p.load(fr);
VERSION_PROPERTIES.add(p);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -20,6 +21,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
Expand Down Expand Up @@ -131,7 +133,7 @@ public synchronized String getPasswordForAlias(final String alias)

final Key key = _pwdStore.getKey(alias, getMasterPassword());
if (key != null) {
passwordString = new String(key.getEncoded());
passwordString = new String(key.getEncoded(), Charset.defaultCharset());
}

return passwordString;
Expand Down Expand Up @@ -251,8 +253,7 @@ private KeyStore duplicateKeyStore(final char[] newMasterPassword)
*/
private static void writeKeyStoreToFile(final KeyStore keyStore, final File file, final char[] masterPassword)
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
final FileOutputStream out = new FileOutputStream(file);
try (out) {
try (FileOutputStream out = new FileOutputStream(file)) {
keyStore.store(out, masterPassword);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -40,6 +41,7 @@
import static com.sun.enterprise.util.SystemPropertyConstants.PRODUCT_ROOT_PROPERTY;
import static com.sun.enterprise.util.SystemPropertyConstants.UNIX_ASENV_FILENAME;
import static com.sun.enterprise.util.SystemPropertyConstants.WINDOWS_ASENV_FILENAME;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Class ASenvPropertyReader
Expand Down Expand Up @@ -237,7 +239,7 @@ private void setProperties(File configDir) {

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(asenv));
reader = new BufferedReader(new FileReader(asenv, UTF_8));
String line;
while ((line = reader.readLine()) != null) {
setProperty(line);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.io.PrintWriter;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -186,7 +187,8 @@ private void writeStdin(Process process) throws ProcessManagerException {
if (process == null) {
throw new ProcessManagerException("Parameter process was null.");
}
try (PrintWriter pipe = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())))) {
try (PrintWriter pipe = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), Charset.defaultCharset())))) {
for (String stdinLine : stdinLines) {
LOG.log(Level.DEBUG, "InputLine --> {0} <--", stdinLine);
pipe.println(stdinLine);
Expand Down Expand Up @@ -224,7 +226,7 @@ static class ReaderThread extends Thread {

ReaderThread(InputStream stream, boolean echo, String threadName, Thread threadWaitingForProcess, String textToWaitFor) {
setName(threadName);
this.reader = new BufferedReader(new InputStreamReader(stream));
this.reader = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset()));
this.sb = new StringBuilder();
this.echo = echo;
this.threadWaitingForProcess = threadWaitingForProcess;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -20,57 +21,53 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;

class ProcessStreamDrainerWorker implements Runnable {

///////////////////////////////////////////////////////////////////////////
private final BufferedInputStream reader;
private final PrintStream redirect;
private StringBuilder sb;

class ProcessStreamDrainerWorker implements Runnable
{
ProcessStreamDrainerWorker(InputStream in, PrintStream Redirect, boolean save)
{
if(in == null)
ProcessStreamDrainerWorker(InputStream in, PrintStream Redirect, boolean save) {
if (in == null) {
throw new NullPointerException("InputStream argument was null.");
}

reader = new BufferedInputStream(in);
redirect = Redirect;

if(save) {
if (save) {
sb = new StringBuilder();
}
}

public void run()
{
if(reader == null)

@Override
public void run() {
if (reader == null) {
return;
}

try
{
try {
byte[] buffer = new byte[8192];
Charset charset = Charset.defaultCharset();
int count = 0;
byte[] buffer = new byte[4096];

while ((count = reader.read(buffer)) != -1)
{
if(redirect != null)
while ((count = reader.read(buffer)) != -1) {
if (redirect != null) {
redirect.write(buffer, 0, count);
}

if(sb != null)
sb.append(new String(buffer, 0, count));
if (sb != null) {
sb.append(new String(buffer, 0, count, charset));
}
}
}
catch (IOException e)
{
} catch (IOException e) {
}
}


String getString() {
if(sb != null)
return sb.toString();
else
return "";
return sb == null ? "" : sb.toString();
}

private final BufferedInputStream reader;
private final PrintStream redirect;
private StringBuilder sb;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -39,6 +39,7 @@
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.TRACE;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

/**
* Includes a somewhat kludgy way to get the pid for "me". Another casualty of
Expand Down Expand Up @@ -86,7 +87,7 @@ public static File getExe(String name) {
* @throws IOException
*/
public static void saveCurrentPid(final File pidFile) throws IOException {
FileUtils.writeStringToFile(Long.toString(ProcessHandle.current().pid()), pidFile);
FileUtils.writeStringToFile(Long.toString(ProcessHandle.current().pid()), pidFile, ISO_8859_1);
}


Expand Down Expand Up @@ -135,7 +136,7 @@ public static boolean isAlive(final long pid) {
*/
public static long loadPid(final File pidFile) throws IllegalArgumentException {
try {
return Long.parseLong(FileUtils.readSmallFile(pidFile).trim());
return Long.parseLong(FileUtils.readSmallFile(pidFile, ISO_8859_1).trim());
} catch (NumberFormatException | IOException e) {
throw new IllegalArgumentException("Could not parse the PID file: " + pidFile, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2008, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -44,6 +44,7 @@

import org.glassfish.main.jul.handler.GlassFishLogHandlerProperty;

import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.xml.stream.XMLStreamConstants.END_DOCUMENT;
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
Expand Down Expand Up @@ -277,7 +278,7 @@ private void read() throws XMLStreamException, EndDocumentException, FileNotFoun
}

private void createParser() throws FileNotFoundException, XMLStreamException {
reader = new InputStreamReader(new FileInputStream(domainXml));
reader = new InputStreamReader(new FileInputStream(domainXml), UTF_8);
XMLInputFactory xif = getXmlInputFactory();
// Set the resolver so that any external entity references, such
// as a reference to a DTD, return an empty file. The domain.xml
Expand Down
Loading

0 comments on commit 1e81265

Please sign in to comment.