Skip to content

Commit

Permalink
Merge pull request #84 from Tlantic/fix/android-charset
Browse files Browse the repository at this point in the history
Fix/android charset
  • Loading branch information
diogoqueiros authored Nov 2, 2018
2 parents adec425 + e425c26 commit b277f8f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
28 changes: 27 additions & 1 deletion src/android/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -24,6 +26,7 @@ public class Connection extends Thread {
private Boolean mustClose;
private String host;
private int port;
private String charset;


/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions src/android/SocketPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,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 @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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());
}
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 b277f8f

Please sign in to comment.