Browse Source

Encoding fixed. #157

master
Nikita 11 years ago
parent
commit
3677978c1b
  1. 21
      src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java
  2. 24
      src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

21
src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java

@ -17,7 +17,6 @@ package com.corundumstudio.socketio.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufProcessor;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
@ -52,7 +51,7 @@ public class PacketDecoder {
Integer length = Integer.valueOf(len);
packet = packet.substring(splitIndex+1, splitIndex+length+1);
packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8);
// packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8);
return Unpooled.wrappedBuffer(packet.getBytes(CharsetUtil.UTF_8));
}
@ -90,18 +89,6 @@ public class PacketDecoder {
}
}
private static int find(ByteBuf buffer, final char item) {
int index = buffer.forEachByte(buffer.readerIndex(), buffer.readableBytes(), new ByteBufProcessor() {
@Override
public boolean process(byte value) throws Exception {
return value != item;
}
});
return index;
}
public Packet decodePackets(ByteBuf buffer, UUID uuid) throws IOException {
boolean isString = buffer.getByte(buffer.readerIndex()) == 0x0;
if (isString) {
@ -144,10 +131,10 @@ public class PacketDecoder {
PacketType innerType = readInnerType(frame);
packet.setSubType(innerType);
int endIndex = find(frame, '[');
if (endIndex > frame.readerIndex()) {
int endIndex = frame.bytesBefore((byte)'[');
if (endIndex > 0) {
// TODO optimize
String nspAckId = readString(frame, endIndex - frame.readerIndex());
String nspAckId = readString(frame, endIndex);
if (nspAckId.contains(",")) {
String[] parts = nspAckId.split(",");
String nsp = parts[0];

24
src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java

@ -95,7 +95,7 @@ public class PacketEncoder {
buf.release();
// TODO optimize
packet = QUOTES_PATTERN.matcher(packet).replaceAll("\\\\\"");
packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1);
// packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1);
out.writeBytes(packet.getBytes(CharsetUtil.UTF_8));
out.writeBytes(JSONP_END);
@ -261,27 +261,7 @@ public class PacketEncoder {
if (jsonp) {
jsonSupport.writeJsonpValue(out, values);
} else {
if (binary) {
// handling websocket encoding bug
ByteBuf b = allocateBuffer(allocator);
try {
ByteBufOutputStream os = new ByteBufOutputStream(b);
jsonSupport.writeValue(os, values);
CharsetEncoder enc = CharsetUtil.ISO_8859_1.newEncoder();
String str = b.toString(CharsetUtil.ISO_8859_1);
if (enc.canEncode(str)) {
buf.writeBytes(str.getBytes(CharsetUtil.UTF_8));
} else {
buf.writeBytes(b);
}
}
finally {
b.release();
}
} else {
jsonSupport.writeValue(out, values);
}
jsonSupport.writeValue(out, values);
}
}
break;

Loading…
Cancel
Save