Skip to content
Permalink
Browse files

Merge pull request #133 from wongoo/fix-string-chunk

Fix: chunk string error
  • Loading branch information...
AlexStocks committed Sep 23, 2019
2 parents 335b8c6 + 7a35c63 commit e3dd5d131df5c6da10a528a6fa10f46fdf618c1c
Showing with 18 additions and 13 deletions.
  1. +5 −4 encode_test.go
  2. +6 −6 string.go
  3. +7 −3 string_test.go
@@ -16,7 +16,6 @@ package hessian

import (
"bytes"
"fmt"
"os/exec"
"testing"
)
@@ -36,7 +35,7 @@ func encodeTarget(target interface{}) ([]byte, error) {
return e.Buffer(), nil
}

func javaDecodeValidate(method string, target interface{}) (string, error) {
func javaDecodeValidate(t *testing.T, method string, target interface{}) (string, error) {
b, err := encodeTarget(target)
if err != nil {
return "", err
@@ -47,7 +46,7 @@ func javaDecodeValidate(method string, target interface{}) (string, error) {

stdin, err := cmd.StdinPipe()
if err != nil {
fmt.Printf("call java error: %v", err)
t.Logf("call java error: %v", err)
return "", err
}

@@ -57,14 +56,16 @@ func javaDecodeValidate(method string, target interface{}) (string, error) {
}()

out, err := cmd.Output()

if err != nil {
t.Logf("get java result error: %v", err)
return "", err
}
return string(out), nil
}

func testJavaDecode(t *testing.T, method string, target interface{}) {
result, err := javaDecodeValidate(method, target)
result, err := javaDecodeValidate(t, method, target)
if err != nil {
t.Errorf("%s: encode fail with error: %v", method, err)
return
@@ -151,7 +151,6 @@ func encString(b []byte, v string) []byte {
var (
byteLen = 0
charLen = 0
last = false
vBuf = *bytes.NewBufferString(v)

byteRead = 0
@@ -170,12 +169,9 @@ func encString(b []byte, v string) []byte {

charCount = 0
byteCount = 0
for charCount <= CHUNK_SIZE {
for charCount < CHUNK_SIZE {
r, _, err := vBuf.ReadRune()
if err != nil {
if err == io.EOF {
last = true
}
break
}

@@ -184,8 +180,12 @@ func encString(b []byte, v string) []byte {
byteCount += byteLen
}

if charCount == 0 {
break
}

switch {
case !last && charCount >= CHUNK_SIZE:
case vBuf.Len() > 0 && charCount >= CHUNK_SIZE:
b = encByte(b, BC_STRING_CHUNK)
b = encByte(b, PackUint16(uint16(charCount))...)
case charCount <= int(STRING_DIRECT_MAX):
@@ -80,14 +80,18 @@ func TestEncRune(t *testing.T) {
)

e = NewEncoder()
v = "我化尘埃飞扬,追寻赤裸逆翔, 奔去七月刑场,时间烧灼滚烫, 回忆撕毁臆想,路上行走匆忙, 难能可贵世上,散播留香磁场, 我欲乘风破浪,踏遍黄沙海洋, 与其误会一场,也要不负勇往, 我愿你是个谎,从未出现南墙, 笑是神的伪装,笑是强忍的伤, 我想你就站在,站在大漠边疆, 我化尘埃飞扬,追寻赤裸逆翔," +
" 奔去七月刑场,时间烧灼滚烫, 回忆撕毁臆想,路上行走匆忙, 难能可贵世上,散播留香磁场, 我欲乘风破浪,踏遍黄沙海洋, 与其误会一场,也要不负勇往, 我愿你是个谎,从未出现南墙, 笑是神的伪装,笑是强忍的伤, 我想你就站在,站在大漠边疆."

v = "我化尘埃飞扬,追寻赤裸逆翔,奔去七月刑场,时间烧灼滚烫,回忆撕毁臆想,路上行走匆忙,难能可贵世上,散播留香磁场,我欲乘风破浪,踏遍黄沙海洋,与其误会一场,也要不负勇往,我愿你是个谎,从未出现南墙,笑是神的伪装,笑是强忍的伤,我想你就站在,站在大漠边疆,我化尘埃飞扬,追寻赤裸逆翔," +
"奔去七月刑场,时间烧灼滚烫,回忆撕毁臆想,路上行走匆忙,难能可贵世上,散播留香磁场,我欲乘风破浪,踏遍黄沙海洋,与其误会一场,也要不负勇往,我愿你是个谎,从未出现南墙,笑是神的伪装,笑是强忍的伤,我想你就站在,站在大漠边疆."

v = v + v + v + v + v
v = v + v + v + v + v
v = v + v + v + v + v
v = v + v + v + v + v
v = v + v + v + v + v
fmt.Printf("vlen:%d\n", len(v))

t.Logf("TestEncRune vlen: %d", len(v))

e.Encode(v)
if len(e.Buffer()) == 0 {
t.Fail()

0 comments on commit e3dd5d1

Please sign in to comment.
You can’t perform that action at this time.