Skip to content

Commit

Permalink
WFCORE-1510 Fix code to not use default platform dependant encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ctomc committed Nov 17, 2016
1 parent 3cfb06e commit 69e4d2a
Show file tree
Hide file tree
Showing 106 changed files with 407 additions and 1,369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
package org.jboss.as.cli.gui.metacommand;

import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.jboss.as.cli.gui.CliGuiContext;
import org.jboss.as.cli.gui.component.CLIOutput;
import org.jboss.as.cli.gui.component.ScriptMenu;
import org.jboss.as.protocol.StreamUtils;

/**
* Abstract action that runs scripts.
Expand Down Expand Up @@ -85,22 +84,11 @@ protected void runScript(File script) {

// read the file as a list of text lines
private List<String> getCommandLines(File file) {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
} catch (Throwable e) {
return Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Failed to process file " + file.getAbsolutePath(), e);
} finally {
StreamUtils.safeClose(reader);
}

return lines;
}

// We need this class because we have to pass on whether or not a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.Random;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -104,8 +105,7 @@ public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFo
}
ctx.printLine("Processing script '" + script + "'.");

try {
BufferedReader reader = new BufferedReader(new FileReader(scriptFile));
try (BufferedReader reader = Files.newBufferedReader(scriptFile.toPath(), StandardCharsets.UTF_8)){
String line = reader.readLine();
while (!ctx.isTerminated() && line != null) {
ctx.handle(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Collection;

import org.jboss.as.cli.CommandContext;
Expand Down Expand Up @@ -91,8 +92,9 @@ public void handle(CommandContext ctx) throws CommandLineException {
protected void printHelp(CommandContext ctx) throws CommandLineException {
InputStream helpInput = WildFlySecurityManager.getClassLoaderPrivileged(CommandHandlerWithHelp.class).getResourceAsStream(filename);
if(helpInput != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(helpInput));
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(helpInput, StandardCharsets.UTF_8));
/* String helpLine = reader.readLine();
while(helpLine != null) {
ctx.printLine(helpLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -658,7 +659,7 @@ public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFo

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(scriptFile));
reader = Files.newBufferedReader(scriptFile.toPath(), StandardCharsets.UTF_8);
String line = reader.readLine();
while (!ctx.isTerminated() && line != null) {
ctx.handle(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -351,7 +352,7 @@ public ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFo

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(scriptFile));
reader = Files.newBufferedReader(scriptFile.toPath(), StandardCharsets.UTF_8);
String line = reader.readLine();
while (!ctx.isTerminated() && line != null) {
ctx.handle(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -150,7 +151,7 @@ protected void doHandle(CommandContext ctx) throws CommandLineException {

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(f));
reader = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8);
String line = reader.readLine();
batchManager.activateNewBatch();
final Batch batch = batchManager.getActiveBatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
*/
package org.jboss.as.cli.handlers.batch;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import org.jboss.as.cli.Attachments;

Expand Down Expand Up @@ -175,7 +177,7 @@ protected ModelNode buildRequestWOValidation(CommandContext ctx) throws CommandF

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(f));
reader = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8);
String line = reader.readLine();
batchManager.activateNewBatch();
while(line != null) {
Expand Down
5 changes: 3 additions & 2 deletions cli/src/main/java/org/jboss/as/cli/impl/CliLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -333,7 +334,7 @@ private static void processFile(File file, final CommandContext cmdCtx) {

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);
String line = reader.readLine();
while (cmdCtx.getExitCode() == 0 && !cmdCtx.isTerminated() && line != null) {
cmdCtx.handleSafe(line.trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.Certificate;
Expand Down Expand Up @@ -1576,14 +1578,11 @@ protected void setOutputTarget(String filePath) {
this.outputTarget = null;
return;
}
FileWriter writer;
try {
writer = new FileWriter(filePath, false);
this.outputTarget = Files.newBufferedWriter(Paths.get(filePath), StandardCharsets.UTF_8);
} catch (IOException e) {
error(e.getLocalizedMessage());
return;
}
this.outputTarget = new BufferedWriter(writer);
}

protected void notifyListeners(CliEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* All methods on this class should be called with {@link org.jboss.as.controller.audit.ManagedAuditLoggerImpl}'s lock taken.
Expand All @@ -41,7 +42,7 @@
* @author <a href="mailto:[email protected]">Ivo Studensky</a>
*/
public abstract class AbstractFileAuditLogHandler extends AuditLogHandler {
protected static final byte[] LINE_TERMINATOR = String.format("%n").getBytes();
protected static final byte[] LINE_TERMINATOR = System.lineSeparator().getBytes(StandardCharsets.UTF_8);
private final PathManagerService pathManager;
private final String path;
private final String relativeTo;
Expand Down Expand Up @@ -93,7 +94,7 @@ void writeLogItem(String formattedItem) throws IOException {
final FileOutputStream fos = new FileOutputStream(file, true);
final BufferedOutputStream output = new BufferedOutputStream(fos);
try {
output.write(formattedItem.getBytes());
output.write(formattedItem.getBytes(StandardCharsets.UTF_8));
output.write(LINE_TERMINATOR);

//Flush and force the file to sync
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* All methods on this class should be called with {@link org.jboss.as.controller.audit.ManagedAuditLoggerImpl}'s lock taken.
Expand Down Expand Up @@ -85,7 +86,7 @@ protected void rotateLogFile(final File file) {
@Override
void writeLogItem(String formattedItem) throws IOException {
super.writeLogItem(formattedItem);
currentSize += formattedItem.getBytes().length;
currentSize += formattedItem.getBytes(StandardCharsets.UTF_8).length;
currentSize += LINE_TERMINATOR.length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void execute(OperationContext context, ModelNode operation) {
} finally {
safeClose(baos);
}
String xml = new String(baos.toByteArray());
String xml = new String(baos.toByteArray(), StandardCharsets.UTF_8);
context.getResult().set(xml);
} catch (RuntimeException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void updateModel(final OperationContext context, ModelNode model, ModelNode list
/**
* Add element to list, with optional index where to put it
* <p/>
* <pre>:list-remove(name=list-attribute, value="some value")</pre>
* <pre>:list-get(name=list-attribute, index=5)</pre>
*
* @author Tomaz Cerar (c) 2014 Red Hat Inc.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

package org.jboss.as.controller.transform;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;

import org.jboss.as.controller.OperationContext;
Expand All @@ -43,7 +45,7 @@
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;

/**
/** todo move to test code, no need to have it at proper runtime
* @author <a href="mailto:[email protected]">Tomaz Cerar</a>
*/
public class SubsystemDescriptionDump implements OperationStepHandler {
Expand Down Expand Up @@ -80,7 +82,7 @@ public static void dumpManagementResourceRegistration(final ImmutableManagementR
SubsystemInformation info = registry.getSubsystemInfo(subsystem);
ModelNode desc = readFullModelDescription(PathAddress.pathAddress(pe), registration);
String name = subsystem + "-" + info.getManagementInterfaceMajorVersion() + "." + info.getManagementInterfaceMinorVersion() +"."+info.getManagementInterfaceMicroVersion()+ ".dmr";
PrintWriter pw = new PrintWriter(new File(path, name));
PrintWriter pw = new PrintWriter(Files.newBufferedWriter(Paths.get(path,name), StandardCharsets.UTF_8));
desc.writeString(pw, false);
pw.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@
import static org.junit.Assert.assertThat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.AclEntry;
Expand All @@ -62,7 +59,6 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xnio.IoUtils;

/**
*
Expand Down Expand Up @@ -916,12 +912,7 @@ private File createFile(File dir, String name, String contents) throws IOExcepti
checkDirectoryExists(dir);
File file = new File(dir, name);
if (contents != null) {
Writer out = new BufferedWriter(new FileWriter(file));
try {
out.write(contents);
} finally {
IoUtils.safeClose(out);
}
Files.write(file.toPath(), contents.getBytes(StandardCharsets.UTF_8));
}
return file;
}
Expand All @@ -945,15 +936,12 @@ private void delete(File file) {
private void assertFileContents(File file, String expectedContents) throws Exception {
Assert.assertTrue(file + " does not exist", file.exists());
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new FileReader(file));
try {
try (BufferedReader in = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)){
String s = in.readLine();
while (s != null) {
sb.append(s);
s = in.readLine();
}
} finally {
in.close();
}
Assert.assertEquals(expectedContents, sb.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -100,7 +101,7 @@ private static String readResource(final String name) throws IOException {
Assert.assertNotNull(name + " url is null", configURL);

StringWriter writer = new StringWriter();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(configURL.openStream()))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(configURL.openStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line + "\n");
Expand Down
Loading

0 comments on commit 69e4d2a

Please sign in to comment.