fix #10 use org.json 20090211
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.hasbinarydata;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
@@ -24,7 +25,13 @@ public class HasBinaryData {
|
||||
JSONArray _obj = (JSONArray)obj;
|
||||
int length = _obj.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (recursiveCheckForBinary(_obj.isNull(i) ? null : _obj.get(i))) {
|
||||
Object v;
|
||||
try {
|
||||
v = _obj.isNull(i) ? null : _obj.get(i);
|
||||
} catch (JSONException e) {
|
||||
return false;
|
||||
}
|
||||
if (recursiveCheckForBinary(v)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -33,7 +40,13 @@ public class HasBinaryData {
|
||||
Iterator keys = _obj.keys();
|
||||
while (keys.hasNext()) {
|
||||
String key = (String)keys.next();
|
||||
if (recursiveCheckForBinary(_obj.get(key))) {
|
||||
Object v;
|
||||
try {
|
||||
v = _obj.get(key);
|
||||
} catch (JSONException e) {
|
||||
return false;
|
||||
}
|
||||
if (recursiveCheckForBinary(v)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,18 +47,6 @@ public class Socket extends Emitter {
|
||||
put(EVENT_ERROR, 1);
|
||||
}};
|
||||
|
||||
private static boolean jsonArrayNativeRemove;
|
||||
|
||||
{
|
||||
try {
|
||||
jsonArrayNativeRemove = JSONArray.class.getMethod("remove", new Class[] {
|
||||
int.class
|
||||
}) != null;
|
||||
} catch (NoSuchMethodException e) {
|
||||
jsonArrayNativeRemove = false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean connected;
|
||||
private boolean disconnected = true;
|
||||
private int ids;
|
||||
@@ -170,12 +158,8 @@ public class Socket extends Emitter {
|
||||
if (_args.get(_args.size() - 1) instanceof Ack) {
|
||||
logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids));
|
||||
Socket.this.acks.put(Socket.this.ids, (Ack)_args.remove(_args.size() - 1));
|
||||
if (jsonArrayNativeRemove) {
|
||||
jsonArgs.remove(jsonArgs.length() - 1);
|
||||
} else {
|
||||
jsonArgs = remove(jsonArgs, jsonArgs.length() - 1);
|
||||
packet.data = jsonArgs;
|
||||
}
|
||||
jsonArgs = remove(jsonArgs, jsonArgs.length() - 1);
|
||||
packet.data = jsonArgs;
|
||||
packet.id = Socket.this.ids++;
|
||||
}
|
||||
|
||||
@@ -187,14 +171,16 @@ public class Socket extends Emitter {
|
||||
|
||||
private static JSONArray remove(JSONArray a, int pos) {
|
||||
JSONArray na = new JSONArray();
|
||||
try {
|
||||
for (int i = 0; i < a.length(); i++){
|
||||
if (i != pos) {
|
||||
na.put(a.get(i));
|
||||
for (int i = 0; i < a.length(); i++){
|
||||
if (i != pos) {
|
||||
Object v;
|
||||
try {
|
||||
v = a.get(i);
|
||||
} catch (JSONException e) {
|
||||
v = null;
|
||||
}
|
||||
na.put(v);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
return na;
|
||||
}
|
||||
@@ -392,7 +378,12 @@ public class Socket extends Emitter {
|
||||
int length = array.length();
|
||||
Object[] data = new Object[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
Object v = array.get(i);
|
||||
Object v;
|
||||
try {
|
||||
v = array.get(i);
|
||||
} catch (JSONException e) {
|
||||
v = null;
|
||||
}
|
||||
data[i] = v == JSONObject.NULL ? null : v;
|
||||
}
|
||||
return data;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -31,8 +32,12 @@ public class Binary {
|
||||
|
||||
if (data instanceof byte[]) {
|
||||
JSONObject placeholder = new JSONObject();
|
||||
placeholder.put(KEY_PLACEHOLDER, true);
|
||||
placeholder.put(KEY_NUM, buffers.size());
|
||||
try {
|
||||
placeholder.put(KEY_PLACEHOLDER, true);
|
||||
placeholder.put(KEY_NUM, buffers.size());
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
buffers.add((byte[])data);
|
||||
return placeholder;
|
||||
} else if (data instanceof JSONArray) {
|
||||
@@ -40,7 +45,11 @@ public class Binary {
|
||||
JSONArray _data = (JSONArray)data;
|
||||
int len = _data.length();
|
||||
for (int i = 0; i < len; i ++) {
|
||||
newData.put(i, _deconstructPacket(_data.get(i), buffers));
|
||||
try {
|
||||
newData.put(i, _deconstructPacket(_data.get(i), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return newData;
|
||||
} else if (data instanceof JSONObject) {
|
||||
@@ -49,7 +58,11 @@ public class Binary {
|
||||
Iterator<?> iterator = _data.keys();
|
||||
while (iterator.hasNext()) {
|
||||
String key = (String)iterator.next();
|
||||
newData.put(key, _deconstructPacket(_data.get(key), buffers));
|
||||
try {
|
||||
newData.put(key, _deconstructPacket(_data.get(key), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return newData;
|
||||
}
|
||||
@@ -67,7 +80,11 @@ public class Binary {
|
||||
JSONArray _data = (JSONArray)data;
|
||||
int len = _data.length();
|
||||
for (int i = 0; i < len; i ++) {
|
||||
_data.put(i, _reconstructPacket(_data.get(i), buffers));
|
||||
try {
|
||||
_data.put(i, _reconstructPacket(_data.get(i), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return _data;
|
||||
} else if (data instanceof JSONObject) {
|
||||
@@ -79,7 +96,11 @@ public class Binary {
|
||||
Iterator<?> iterator = _data.keys();
|
||||
while (iterator.hasNext()) {
|
||||
String key = (String)iterator.next();
|
||||
_data.put(key, _reconstructPacket(_data.get(key), buffers));
|
||||
try {
|
||||
_data.put(key, _reconstructPacket(_data.get(key), buffers));
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return _data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user