Added charset for communication

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2016-11-19 17:35:16 -05:00
parent 5280ee98bd
commit c2fa0f2c0b
4 changed files with 29 additions and 13 deletions

View File

@ -98,7 +98,7 @@ of Emmanuel Bigeon. -->
<parent>
<groupId>fr.bigeon</groupId>
<artifactId>ebigeon-config</artifactId>
<version>1.7.0</version>
<version>1.7.1</version>
</parent>
<name>GCLC Socket</name>
<description>Socket implementation of GCLC</description>

View File

@ -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

View File

@ -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

View File

@ -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);