handle utf8 decode errors

This commit is contained in:
Naoyuki Kanezawa
2014-06-30 10:09:41 +09:00
parent 3e64ad64f9
commit b1e43ba1b2
5 changed files with 65 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ package com.github.nkzawa.engineio.parser;
import com.github.nkzawa.utf8.UTF8;
import com.github.nkzawa.utf8.UTF8Exception;
import java.nio.ByteBuffer;
import java.util.ArrayList;
@@ -73,7 +74,11 @@ public class Parser {
} catch (IndexOutOfBoundsException e) {
type = -1;
}
data = UTF8.decode(data);
try {
data = UTF8.decode(data);
} catch (UTF8Exception e) {
return err;
}
if (type < 0 || type >= packetslist.size()) {
return err;
@@ -164,7 +169,6 @@ public class Parser {
if (msg.length() != 0) {
Packet<String> packet = decodePacket(msg);
if (err.type.equals(packet.type) && err.data.equals(packet.data)) {
callback.call(err, 0, 1);
return;

View File

@@ -27,7 +27,7 @@ public class UTF8 {
return byteString.toString();
}
public static String decode(String byteString) {
public static String decode(String byteString) throws UTF8Exception {
byteArray = uc2decode(byteString);
byteCount = byteArray.length;
byteIndex = 0;
@@ -74,7 +74,7 @@ public class UTF8 {
return Character.toChars(((codePoint >> shift) & 0x3F) | 0x80);
}
private static int decodeSymbol() {
private static int decodeSymbol() throws UTF8Exception {
int byte1;
int byte2;
int byte3;
@@ -82,7 +82,7 @@ public class UTF8 {
int codePoint;
if (byteIndex > byteCount) {
throw new RuntimeException("Invalid byte index");
throw new UTF8Exception("Invalid byte index");
}
if (byteIndex == byteCount) {
@@ -102,7 +102,7 @@ public class UTF8 {
if (codePoint >= 0x80) {
return codePoint;
} else {
throw new RuntimeException("Invalid continuation byte");
throw new UTF8Exception("Invalid continuation byte");
}
}
@@ -113,7 +113,7 @@ public class UTF8 {
if (codePoint >= 0x0800) {
return codePoint;
} else {
throw new RuntimeException("Invalid continuation byte");
throw new UTF8Exception("Invalid continuation byte");
}
}
@@ -127,12 +127,12 @@ public class UTF8 {
}
}
throw new RuntimeException("Invalid continuation byte");
throw new UTF8Exception("Invalid continuation byte");
}
private static int readContinuationByte() {
private static int readContinuationByte() throws UTF8Exception {
if (byteIndex >= byteCount) {
throw new RuntimeException("Invalid byte index");
throw new UTF8Exception("Invalid byte index");
}
int continuationByte = byteArray[byteIndex] & 0xFF;
@@ -142,7 +142,7 @@ public class UTF8 {
return continuationByte & 0x3F;
}
throw new RuntimeException("Invalid continuation byte");
throw new UTF8Exception("Invalid continuation byte");
}
private static String ucs2encode(int[] array) {

View File

@@ -0,0 +1,24 @@
package com.github.nkzawa.utf8;
import java.io.IOException;
public class UTF8Exception extends IOException {
public String data;
public UTF8Exception() {
super();
}
public UTF8Exception(String message) {
super(message);
}
public UTF8Exception(String message, Throwable cause) {
super(message, cause);
}
public UTF8Exception(Throwable cause) {
super(cause);
}
}