From c2fa0f2c0b3850f3fd1d68a760e569729b344888 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Sat, 19 Nov 2016 17:35:16 -0500 Subject: [PATCH] Added charset for communication Signed-off-by: Emmanuel Bigeon --- gclc-socket/pom.xml | 2 +- .../socket/SocketConsoleApplicationShell.java | 28 +++++++++++++------ .../socket/ThreadedServerConsoleManager.java | 6 ++-- .../fr/bigeon/gclc/socket/TestServer.java | 6 ++-- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/gclc-socket/pom.xml b/gclc-socket/pom.xml index 136d246..9c41a10 100644 --- a/gclc-socket/pom.xml +++ b/gclc-socket/pom.xml @@ -98,7 +98,7 @@ of Emmanuel Bigeon. --> fr.bigeon ebigeon-config - 1.7.0 + 1.7.1 GCLC Socket Socket implementation of GCLC diff --git a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java index 2981948..5bd2577 100644 --- a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java +++ b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java @@ -45,6 +45,7 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -108,19 +109,23 @@ public class SocketConsoleApplicationShell implements Runnable { private ServerSocket serverSocket; /** The application shutdown string */ private final String applicationShutdown; + /** The charset for the communication. */ + private Charset charset; /** Create a socket application shell which will listen on the given port * and close session upon the provided string reception by client * * @param port the port to listen to * @param close the session closing command - * @param applicationShutdown the appication shut down command */ + * @param applicationShutdown the appication shut down command + * @param charset the charset for communication */ public SocketConsoleApplicationShell(int port, String close, - String applicationShutdown) { + String applicationShutdown, Charset charset) { this.port = port; this.close = close; this.applicationShutdown = applicationShutdown; this.autoClose = false; + this.charset = charset; } /** Create a socket application shell which will listen on the given port @@ -129,13 +134,15 @@ public class SocketConsoleApplicationShell implements Runnable { * @param port the port to listen to * @param autoClose if the session must be closed once the request has been * sent - * @param applicationShutdown the application shutdown command */ + * @param applicationShutdown the appication shut down command + * @param charset the charset for communication */ public SocketConsoleApplicationShell(int port, boolean autoClose, - String applicationShutdown) { + String applicationShutdown, Charset charset) { this.port = port; this.autoClose = autoClose; this.applicationShutdown = applicationShutdown; this.close = autoClose ? null : "close"; //$NON-NLS-1$ + this.charset = charset; } /* (non-Javadoc) @@ -149,8 +156,10 @@ public class SocketConsoleApplicationShell implements Runnable { // Create the streams try (PipedOutputStream outStream = new PipedOutputStream(); BufferedWriter writer = new BufferedWriter( - new OutputStreamWriter(outStream)); - InputStreamReader isr = new InputStreamReader(consoleInput); + new OutputStreamWriter(outStream, + charset)); + InputStreamReader isr = new InputStreamReader(consoleInput, + charset); BufferedReader inBuf = new BufferedReader(isr)) { consoleInput.connect(outStream); consoleManager.setInput(inBuf); @@ -180,9 +189,12 @@ public class SocketConsoleApplicationShell implements Runnable { while (running) { try (Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter( - clientSocket.getOutputStream(), true); + new OutputStreamWriter(clientSocket.getOutputStream(), + charset), + true); InputStreamReader isr = new InputStreamReader( - clientSocket.getInputStream()); + clientSocket.getInputStream(), + charset); BufferedReader in = new BufferedReader(isr);) { // this is not threaded to avoid several clients at the same // time diff --git a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/ThreadedServerConsoleManager.java b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/ThreadedServerConsoleManager.java index aa9dcef..7b0484c 100644 --- a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/ThreadedServerConsoleManager.java +++ b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/ThreadedServerConsoleManager.java @@ -57,8 +57,10 @@ public class ThreadedServerConsoleManager implements ConsoleManager { /** The class logger */ private static final Logger LOGGER = Logger .getLogger(ThreadedServerConsoleManager.class.getName()); + /** The empty string constant */ + private static final String EMPTY = ""; //$NON-NLS-1$ /** The prompting sequence */ - private String prompt = new String(); + private String prompt = EMPTY; /** The buffer of data to send to the user */ private StringBuilder buffer = new StringBuilder(); /** The synchronized object */ @@ -105,7 +107,7 @@ public class ThreadedServerConsoleManager implements ConsoleManager { @Override public String prompt(String message) { buffer.append(message); - String userInput = new String(); + String userInput = EMPTY; boolean prompting = true; while (prompting) { // Send buffer content diff --git a/gclc-socket/src/test/java/fr/bigeon/gclc/socket/TestServer.java b/gclc-socket/src/test/java/fr/bigeon/gclc/socket/TestServer.java index 82cca37..465767c 100644 --- a/gclc-socket/src/test/java/fr/bigeon/gclc/socket/TestServer.java +++ b/gclc-socket/src/test/java/fr/bigeon/gclc/socket/TestServer.java @@ -34,6 +34,8 @@ */ package fr.bigeon.gclc.socket; +import java.nio.charset.Charset; + /** A test server * * @author Emmanuel Bigeon */ @@ -63,7 +65,7 @@ public class TestServer { private static SocketConsoleApplicationShell getShell() { if (SHELL == null) { SHELL = new SocketConsoleApplicationShell(3300, "close", - ConsoleTestApplication.EXIT); + ConsoleTestApplication.EXIT, Charset.forName("UTF-8")); final ConsoleTestApplication app = new ConsoleTestApplication( SHELL.getConsoleManager()); SHELL.setApplication(app); @@ -74,7 +76,7 @@ public class TestServer { public static Thread startServer(boolean autoClose) { if (SHELL == null) { SHELL = new SocketConsoleApplicationShell(3300, autoClose, - ConsoleTestApplication.EXIT); + ConsoleTestApplication.EXIT, Charset.forName("UTF-8")); final ConsoleTestApplication app = new ConsoleTestApplication( SHELL.getConsoleManager()); SHELL.setApplication(app);