Skip to content

Commit 2711943

Browse files
whanicebeiwei30
authored andcommitted
Optimize_hessian_desr_performance (apache#1705)
1 parent 7dd79b3 commit 2711943

File tree

1 file changed

+90
-73
lines changed

1 file changed

+90
-73
lines changed

hessian-lite/src/main/java/com/alibaba/com/caucho/hessian/io/Hessian2Input.java

Lines changed: 90 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class Hessian2Input
119119
private Reader _chunkReader;
120120
private InputStream _chunkInputStream;
121121
private Throwable _replyFault;
122-
private StringBuffer _sbuf = new StringBuffer();
122+
private StringBuilder _sbuf = new StringBuilder();
123123
// true if this is the last chunk
124124
private boolean _isLastChunk;
125125
// the chunk length
@@ -2493,11 +2493,9 @@ public Object readObject(List<Class<?>> expectedTypes) throws IOException {
24932493
_isLastChunk = tag == 'S';
24942494
_chunkLength = (read() << 8) + read();
24952495

2496-
int data;
24972496
_sbuf.setLength(0);
24982497

2499-
while ((data = parseChar()) >= 0)
2500-
_sbuf.append((char) data);
2498+
parseString(_sbuf);
25012499

25022500
return _sbuf.toString();
25032501
}
@@ -2538,11 +2536,9 @@ public Object readObject(List<Class<?>> expectedTypes) throws IOException {
25382536
_isLastChunk = true;
25392537
_chunkLength = tag - 0x00;
25402538

2541-
int data;
25422539
_sbuf.setLength(0);
25432540

2544-
while ((data = parseChar()) >= 0)
2545-
_sbuf.append((char) data);
2541+
parseString(_sbuf);
25462542

25472543
return _sbuf.toString();
25482544
}
@@ -2556,9 +2552,7 @@ public Object readObject(List<Class<?>> expectedTypes) throws IOException {
25562552

25572553
_sbuf.setLength(0);
25582554

2559-
int ch;
2560-
while ((ch = parseChar()) >= 0)
2561-
_sbuf.append((char) ch);
2555+
parseString(_sbuf);
25622556

25632557
return _sbuf.toString();
25642558
}
@@ -2772,6 +2766,23 @@ public Object readObject(List<Class<?>> expectedTypes) throws IOException {
27722766
}
27732767
}
27742768

2769+
private void parseString(StringBuilder sbuf)
2770+
throws IOException {
2771+
while (true) {
2772+
if (_chunkLength <= 0) {
2773+
if (!parseChunkLength())
2774+
return;
2775+
}
2776+
2777+
int length = _chunkLength;
2778+
_chunkLength = 0;
2779+
2780+
while (length-- > 0) {
2781+
sbuf.append((char) parseUTF8Char());
2782+
}
2783+
}
2784+
}
2785+
27752786
/**
27762787
* Reads an object definition:
27772788
* <p>
@@ -3157,80 +3168,86 @@ org.w3c.dom.Node parseXML()
31573168
throw new UnsupportedOperationException();
31583169
}
31593170

3160-
/**
3161-
* Reads a character from the underlying stream.
3162-
*/
3163-
private int parseChar()
3171+
private boolean parseChunkLength()
31643172
throws IOException {
3165-
while (_chunkLength <= 0) {
3166-
if (_isLastChunk)
3167-
return -1;
3173+
if (_isLastChunk)
3174+
return false;
31683175

3169-
int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();
3176+
int code = _offset < _length ? (_buffer[_offset++] & 0xff) : read();
31703177

3171-
switch (code) {
3172-
case BC_STRING_CHUNK:
3173-
_isLastChunk = false;
3178+
switch (code) {
3179+
case BC_STRING_CHUNK:
3180+
_isLastChunk = false;
31743181

3175-
_chunkLength = (read() << 8) + read();
3176-
break;
3182+
_chunkLength = (read() << 8) + read();
3183+
break;
31773184

3178-
case 'S':
3179-
_isLastChunk = true;
3185+
case 'S':
3186+
_isLastChunk = true;
31803187

3181-
_chunkLength = (read() << 8) + read();
3182-
break;
3188+
_chunkLength = (read() << 8) + read();
3189+
break;
31833190

3184-
case 0x00:
3185-
case 0x01:
3186-
case 0x02:
3187-
case 0x03:
3188-
case 0x04:
3189-
case 0x05:
3190-
case 0x06:
3191-
case 0x07:
3192-
case 0x08:
3193-
case 0x09:
3194-
case 0x0a:
3195-
case 0x0b:
3196-
case 0x0c:
3197-
case 0x0d:
3198-
case 0x0e:
3199-
case 0x0f:
3191+
case 0x00:
3192+
case 0x01:
3193+
case 0x02:
3194+
case 0x03:
3195+
case 0x04:
3196+
case 0x05:
3197+
case 0x06:
3198+
case 0x07:
3199+
case 0x08:
3200+
case 0x09:
3201+
case 0x0a:
3202+
case 0x0b:
3203+
case 0x0c:
3204+
case 0x0d:
3205+
case 0x0e:
3206+
case 0x0f:
32003207

3201-
case 0x10:
3202-
case 0x11:
3203-
case 0x12:
3204-
case 0x13:
3205-
case 0x14:
3206-
case 0x15:
3207-
case 0x16:
3208-
case 0x17:
3209-
case 0x18:
3210-
case 0x19:
3211-
case 0x1a:
3212-
case 0x1b:
3213-
case 0x1c:
3214-
case 0x1d:
3215-
case 0x1e:
3216-
case 0x1f:
3217-
_isLastChunk = true;
3218-
_chunkLength = code - 0x00;
3219-
break;
3208+
case 0x10:
3209+
case 0x11:
3210+
case 0x12:
3211+
case 0x13:
3212+
case 0x14:
3213+
case 0x15:
3214+
case 0x16:
3215+
case 0x17:
3216+
case 0x18:
3217+
case 0x19:
3218+
case 0x1a:
3219+
case 0x1b:
3220+
case 0x1c:
3221+
case 0x1d:
3222+
case 0x1e:
3223+
case 0x1f:
3224+
_isLastChunk = true;
3225+
_chunkLength = code - 0x00;
3226+
break;
32203227

3221-
// qian.lei 2010-7-21
3222-
case 0x30:
3223-
case 0x31:
3224-
case 0x32:
3225-
case 0x33:
3226-
_isLastChunk = true;
3227-
_chunkLength = ((code - 0x30) << 8) + read();
3228-
break;
3228+
case 0x30:
3229+
case 0x31:
3230+
case 0x32:
3231+
case 0x33:
3232+
_isLastChunk = true;
3233+
_chunkLength = (code - 0x30) * 256 + read();
3234+
break;
32293235

3230-
default:
3231-
throw expect("string", code);
3232-
}
3236+
default:
3237+
throw expect("string", code);
3238+
}
3239+
3240+
return true;
3241+
}
32333242

3243+
/**
3244+
* Reads a character from the underlying stream.
3245+
*/
3246+
private int parseChar()
3247+
throws IOException {
3248+
while (_chunkLength <= 0) {
3249+
if (!parseChunkLength())
3250+
return -1;
32343251
}
32353252

32363253
_chunkLength--;

0 commit comments

Comments
 (0)