diff --git a/src/android/Connection.java b/src/android/Connection.java index f834dd7..a65cd79 100644 --- a/src/android/Connection.java +++ b/src/android/Connection.java @@ -3,9 +3,11 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; +import java.nio.charset.Charset; /** @@ -24,6 +26,7 @@ public class Connection extends Thread { private Boolean mustClose; private String host; private int port; + private String charset; /** @@ -72,6 +75,23 @@ public boolean isConnected() { return result; } + /** + * Get the charset name used in OutputStreamWriter + * @return String return charset name if socket was instantiated with that charset + */ + public String getCharset() { + return this.charset; + } + + /** + * Set the charset name for create an OutputStreamWriter that uses that charset + * + * @param charset String the charset to use + */ + public void setCharset(String charset) { + this.charset = charset; + } + /** * Closes socket connection. */ @@ -110,7 +130,13 @@ public void run() { // creating connection try { this.callbackSocket = new Socket(this.host, this.port); - this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true); + + if (this.charset != null) { + this.writer = new PrintWriter(new OutputStreamWriter(callbackSocket.getOutputStream(), Charset.forName(this.charset)), true); + } else { + this.writer = new PrintWriter(callbackSocket.getOutputStream(), true); + } + this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream())); // receiving data chunk diff --git a/src/android/SocketPlugin.java b/src/android/SocketPlugin.java index 35f9422..84752ad 100644 --- a/src/android/SocketPlugin.java +++ b/src/android/SocketPlugin.java @@ -1,11 +1,15 @@ package com.tlantic.plugins.socket; +import android.util.Base64; import android.annotation.SuppressLint; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; +import java.nio.charset.Charset; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; @@ -74,6 +78,7 @@ private void connect (JSONArray args, CallbackContext callbackContext) { String key; String host; int port; + String charset = null; Connection socket; // validating parameters @@ -89,9 +94,18 @@ private void connect (JSONArray args, CallbackContext callbackContext) { port = args.getInt(1); key = this.buildKey(host, port); + if (!args.isNull(2)) { + charset = args.getString(2); + } + // creating connection if (this.pool.get(key) == null) { socket = new Connection(this, host, port); + + if (charset != null && charset.equals("cp1252")) { + socket.setCharset("Windows-1252"); + } + socket.start(); this.pool.put(key, socket); } @@ -122,7 +136,8 @@ private void send(JSONArray args, CallbackContext callbackContext) { // retrieving parameters String key = args.getString(0); String data = args.getString(1); - + String format = args.getString(2); + // getting socket socket = this.pool.get(key); @@ -137,14 +152,24 @@ private void send(JSONArray args, CallbackContext callbackContext) { callbackContext.error("Cannot send empty data to " + key); } else { - + if (format.equals("base64")) { + String charset = socket.getCharset(); + + if (charset == null) { + charset = "UTF-8"; + } + + byte[] decodedData = Base64.decode(data, Base64.DEFAULT); + CharBuffer charBuffer = Charset.forName(charset).decode(ByteBuffer.wrap(decodedData)); + data = String.valueOf(charBuffer); + } + // write on output stream socket.write(data); - + // ending send process - callbackContext.success(); + callbackContext.success(); } - } catch (JSONException e) { callbackContext.error("Unexpected error sending information: " + e.getMessage()); } diff --git a/www/socket.js b/www/socket.js index e0b1419..e24eb95 100644 --- a/www/socket.js +++ b/www/socket.js @@ -10,9 +10,9 @@ function Socket(){ } // -Socket.prototype.connect = function (successCallback, errorCallback, host, port) { +Socket.prototype.connect = function (successCallback, errorCallback, host, port, charset) { 'use strict'; - exec(successCallback, errorCallback, this.pluginRef, 'connect', [host, port]); + exec(successCallback, errorCallback, this.pluginRef, 'connect', [host, port, charset]); }; //