Skip to content

Commit

Permalink
refactor & remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 26, 2016
1 parent 61cf82c commit 5ec128b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 108 deletions.
Expand Up @@ -222,7 +222,7 @@ public final boolean charArrayCompare(char[] chars) {
}

public byte[] bytesValue() {
return IOUtils.decodeFast(buf, np + 1, sp);
return IOUtils.decodeBase64(buf, np + 1, sp);
}

protected final void arrayCopy(int srcPos, char[] dest, int destPos, int length) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/parser/JSONScanner.java
Expand Up @@ -105,7 +105,7 @@ public final String addSymbol(int offset, int len, int hash, final SymbolTable s
}

public byte[] bytesValue() {
return IOUtils.decodeFast(text, np + 1, sp);
return IOUtils.decodeBase64(text, np + 1, sp);
}

// public int scanField2(char[] fieldName, Object object, FieldDeserializer fieldDeserializer) {
Expand Down
101 changes: 13 additions & 88 deletions src/main/java/com/alibaba/fastjson/util/IOUtils.java
Expand Up @@ -20,7 +20,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
Expand Down Expand Up @@ -362,7 +361,7 @@ public static boolean isIdent(char ch) {
* @param chars The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
* @return The decoded array of bytes. May be of length 0.
*/
public static byte[] decodeFast(char[] chars, int offset, int charsLen) {
public static byte[] decodeBase64(char[] chars, int offset, int charsLen) {
// Check special case
if (charsLen == 0) {
return new byte[0];
Expand Down Expand Up @@ -417,7 +416,7 @@ public static byte[] decodeFast(char[] chars, int offset, int charsLen) {
return bytes;
}

public static byte[] decodeFast(String chars, int offset, int charsLen) {
public static byte[] decodeBase64(String chars, int offset, int charsLen) {
// Check special case
if (charsLen == 0) {
return new byte[0];
Expand Down Expand Up @@ -483,7 +482,7 @@ public static byte[] decodeFast(String chars, int offset, int charsLen) {
* @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.
* @return The decoded array of bytes. May be of length 0.
*/
public static byte[] decodeFast(String s) {
public static byte[] decodeBase64(String s) {
// Check special case
int sLen = s.length();
if (sLen == 0) {
Expand Down Expand Up @@ -540,55 +539,29 @@ public static byte[] decodeFast(String s) {
return dArr;
}

private final static ThreadLocal<SoftReference<char[]>> charsBufLocal = new ThreadLocal<SoftReference<char[]>>();


private final static ThreadLocal<char[]> charsBufLocal = new ThreadLocal<char[]>();

public static void clearChars() {
charsBufLocal.set(null);
}

public static char[] getChars(int length) {
SoftReference<char[]> ref = charsBufLocal.get();

if (ref == null) {
return allocate(length);
}

char[] chars = ref.get();
char[] chars = charsBufLocal.get();

if (chars == null) {
return allocate(length);
}

if (chars.length < length) {
chars = allocate(length);
}

return chars;
}

private static char[] allocate(int length) {
final int minExp = 10;
final int CHARS_CACH_MAX_SIZE = 1024 * 128; // 128k, 2^17;

if(length> CHARS_CACH_MAX_SIZE) {
return new char[length];
}

int allocateLength;
{
int part = length >>> minExp;
if(part <= 0) {
allocateLength = 1<< minExp;
if (length <= 1024 * 64) {
chars = new char[1024 * 64];
charsBufLocal.set(chars);
} else {
allocateLength = 1 << 32 - Integer.numberOfLeadingZeros(length-1);
chars = new char[length];
}
} else if (chars.length < length) {
chars = new char[length];
}
char[] chars = new char[allocateLength];
charsBufLocal.set(new SoftReference<char[]>(chars));

return chars;
}


public static String toString(InputStream in) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Expand Down Expand Up @@ -759,52 +732,4 @@ public static int decodeUTF8(byte[] sa, int sp, int len, char[] da) {
}
return dp;
}

public static CoderResult malformedN(ByteBuffer src, int nb) {
switch (nb) {
case 1:
int b1 = src.get();
if ((b1 >> 2) == -2) {
// 5 bytes 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
if (src.remaining() < 4) return CoderResult.UNDERFLOW;

int n = 5;
for (int i = 1; i < n; i++) {
if ((src.get() & 0xc0) != 0x80) {
return CoderResult.malformedForLength(i);
}
}
return CoderResult.malformedForLength(n);
}
if ((b1 >> 1) == -2) {
// 6 bytes 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
if (src.remaining() < 5) {
return CoderResult.UNDERFLOW;
}

int n = 6;
for (int i = 1; i < n; i++) {
if ((src.get() & 0xc0) != 0x80) {
return CoderResult.malformedForLength(i);
}
}
return CoderResult.malformedForLength(n);
}
return CoderResult.malformedForLength(1);
case 2: // always 1
return CoderResult.malformedForLength(1);
case 3:
b1 = src.get();
int b2 = src.get(); // no need to lookup b3
return CoderResult.malformedForLength(((b1 == (byte) 0xe0 && (b2 & 0xe0) == 0x80) || (b2 & 0xc0) != 0x80) ? 1 : 2);
case 4: // we don't care the speed here
b1 = src.get() & 0xff;
b2 = src.get() & 0xff;
if (b1 > 0xf4 || (b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) || (b1 == 0xf4 && (b2 & 0xf0) != 0x80) || (b2 & 0xc0) != 0x80) return CoderResult.malformedForLength(1);
if ((src.get() & 0xc0) != 0x80) return CoderResult.malformedForLength(2);
return CoderResult.malformedForLength(3);
default:
throw new IllegalStateException();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Expand Up @@ -536,7 +536,7 @@ public static byte[] castToBytes(Object value) {
}

if (value instanceof String) {
return IOUtils.decodeFast((String) value);
return IOUtils.decodeBase64((String) value);
}
throw new JSONException("can not cast to int, value : " + value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/alibaba/json/bvt/Base64Test.java
Expand Up @@ -9,7 +9,7 @@

public class Base64Test extends TestCase {
public void test_base64() throws Exception {
Assert.assertEquals(IOUtils.decodeFast(new char[0], 0, 0).length, 0);
Assert.assertEquals(IOUtils.decodeFast("ABC".toCharArray(), 0, 3).length, 2);
Assert.assertEquals(IOUtils.decodeBase64(new char[0], 0, 0).length, 0);
Assert.assertEquals(IOUtils.decodeBase64("ABC".toCharArray(), 0, 3).length, 2);
}
}
18 changes: 9 additions & 9 deletions src/test/java/com/alibaba/json/bvt/Base64Test2.java
Expand Up @@ -14,15 +14,15 @@ public void test_base64_2() throws Exception {
byte[] bytes = text.getBytes("UTF-8");
{
String str = com.alibaba.json.test.Base64.encodeToString(bytes, true);
Assert.assertEquals(text, new String(IOUtils.decodeFast(str.toCharArray(), 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeFast(str), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeFast(str, 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str.toCharArray(), 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str, 0, str.length()), "UTF-8"));
}
{
String str = com.alibaba.json.test.Base64.encodeToString(bytes, false);
Assert.assertEquals(text, new String(IOUtils.decodeFast(str.toCharArray(), 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeFast(str), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeFast(str, 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str.toCharArray(), 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str, 0, str.length()), "UTF-8"));
}
text += ((char) i);

Expand All @@ -33,8 +33,8 @@ public void test_illegal() throws Exception {
String text = "abc";
byte[] bytes = text.getBytes("UTF-8");
String str = "\0" + com.alibaba.json.test.Base64.encodeToString(bytes, false) + "\0";
Assert.assertEquals(text, new String(IOUtils.decodeFast(str.toCharArray(), 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeFast(str), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeFast(str, 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str.toCharArray(), 0, str.length()), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str), "UTF-8"));
Assert.assertEquals(text, new String(IOUtils.decodeBase64(str, 0, str.length()), "UTF-8"));
}
}
Expand Up @@ -14,17 +14,19 @@ public class ThreadLocalCacheTest extends TestCase {
public void test() throws Exception {

IOUtils.clearChars();
Assert.assertEquals(IOUtils.getChars(0).length, 1024);
Assert.assertEquals(IOUtils.getChars(1024).length, 1024);
Assert.assertEquals(IOUtils.getChars(2048).length, 2048);
Assert.assertEquals(IOUtils.getChars(0).length, 2048);
Assert.assertEquals(IOUtils.getChars(0).length, 1024 * 64);
Assert.assertEquals(IOUtils.getChars(1024).length, 1024 * 64);
Assert.assertEquals(IOUtils.getChars(2048).length, 1024 * 64);
Assert.assertEquals(IOUtils.getChars(0).length, 1024 * 64);
Assert.assertEquals(IOUtils.getChars(1024 * 128).length, 1024 * 128);
Assert.assertEquals(IOUtils.getChars(0).length, 1024 * 64);

IOUtils.clearChars();
Assert.assertEquals(IOUtils.getChars(2048).length, 2048);
Assert.assertEquals(IOUtils.getChars(2048).length, 1024 * 64);

IOUtils.clearChars();
Assert.assertEquals(IOUtils.getChars(1024 * 256).length, 1024 * 256);
Assert.assertEquals(IOUtils.getChars(0).length, 1024);
Assert.assertEquals(IOUtils.getChars(0).length, 1024 * 64);
IOUtils.clearChars();

}
Expand Down

0 comments on commit 5ec128b

Please sign in to comment.