Skip to content

Commit

Permalink
fix(socket): add optional param charset to connect api
Browse files Browse the repository at this point in the history
  • Loading branch information
diogoqueiros committed Nov 2, 2018
1 parent dda547c commit e425c26
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/android/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ 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
*
Expand Down
20 changes: 14 additions & 6 deletions src/android/SocketPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private void connect (JSONArray args, CallbackContext callbackContext) {
String key;
String host;
int port;
String charset = null;
Connection socket;

// validating parameters
Expand All @@ -93,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);
}
Expand Down Expand Up @@ -127,7 +137,6 @@ private void send(JSONArray args, CallbackContext callbackContext) {
String key = args.getString(0);
String data = args.getString(1);
String format = args.getString(2);
String charset = args.getString(3);

// getting socket
socket = this.pool.get(key);
Expand All @@ -144,15 +153,14 @@ private void send(JSONArray args, CallbackContext callbackContext) {

} else {
if (format.equals("base64")) {
String _charset = "UTF-8";
String charset = socket.getCharset();

if (charset.equals("cp1252")) {
_charset = "Windows-1252";
socket.setCharset(_charset);
if (charset == null) {
charset = "UTF-8";
}

byte[] decodedData = Base64.decode(data, Base64.DEFAULT);
CharBuffer charBuffer = Charset.forName(_charset).decode(ByteBuffer.wrap(decodedData));
CharBuffer charBuffer = Charset.forName(charset).decode(ByteBuffer.wrap(decodedData));
data = String.valueOf(charBuffer);
}

Expand Down
4 changes: 2 additions & 2 deletions www/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};

//
Expand Down

0 comments on commit e425c26

Please sign in to comment.