|
| 1 | +package json2msgpackStreamer |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "sort" |
| 6 | +) |
| 7 | + |
| 8 | +func newBlockBuf() *blockbuf { |
| 9 | + var first = newBlock() |
| 10 | + |
| 11 | + return &blockbuf{ |
| 12 | + first: first, |
| 13 | + current: first, |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func (b *blockbuf) reset() { |
| 18 | + b.first.index = 0 |
| 19 | + b.first.next = nil |
| 20 | +} |
| 21 | + |
| 22 | +func (b *blockbuf) getCurrentPos() *blockBufPos { |
| 23 | + return &blockBufPos{ |
| 24 | + block: b.current, |
| 25 | + index: b.current.index, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func (b *blockbuf) append(data []byte) { |
| 30 | + var ( |
| 31 | + dLen = len(data) |
| 32 | + lower = 0 |
| 33 | + copylen = 0 |
| 34 | + newblock *block |
| 35 | + ) |
| 36 | + |
| 37 | + for dLen > 0 { |
| 38 | + if b.current.index+dLen < bufsize { |
| 39 | + copylen = dLen |
| 40 | + } else { |
| 41 | + copylen = bufsize - b.current.index |
| 42 | + } |
| 43 | + copy(b.current.buf[b.current.index:], data[lower:lower+copylen]) |
| 44 | + b.current.index += copylen |
| 45 | + lower += copylen |
| 46 | + dLen -= copylen |
| 47 | + if b.current.index == bufsize { |
| 48 | + newblock = newBlock() |
| 49 | + b.current.next = newblock |
| 50 | + b.current = newblock |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (b *blockbuf) appendByte(data byte) { |
| 56 | + b.current.buf[b.current.index] = data |
| 57 | + b.current.index++ |
| 58 | + if b.current.index == bufsize { |
| 59 | + newblock := newBlock() |
| 60 | + b.current.next = newblock |
| 61 | + b.current = newblock |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func (b *blockbuf) writeToOffset(data []byte, pos *blockBufPos) { |
| 66 | + var ( |
| 67 | + dLen = len(data) |
| 68 | + lower = 0 |
| 69 | + copylen = 0 |
| 70 | + ) |
| 71 | + |
| 72 | + for dLen > 0 { |
| 73 | + if pos.index+dLen < bufsize { |
| 74 | + copylen = dLen |
| 75 | + } else { |
| 76 | + copylen = bufsize - pos.index |
| 77 | + } |
| 78 | + copy(pos.block.buf[pos.index:], data[lower:lower+copylen]) |
| 79 | + pos.index += copylen |
| 80 | + lower += copylen |
| 81 | + dLen -= copylen |
| 82 | + if pos.index == bufsize { |
| 83 | + pos.block = pos.block.next |
| 84 | + pos.index = 0 |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (b *blockbuf) writeByteToOffset(data byte, pos *blockBufPos) { |
| 90 | + pos.block.buf[pos.index] = data |
| 91 | +} |
| 92 | + |
| 93 | +func (b *blockbuf) flushToWriter(w io.Writer) error { |
| 94 | + var ( |
| 95 | + iter = b.first |
| 96 | + err error |
| 97 | + insert *blockInsertion |
| 98 | + index int |
| 99 | + ) |
| 100 | + |
| 101 | + for { |
| 102 | + index = 0 |
| 103 | + for _, insert = range iter.insertions { |
| 104 | + if _, err = w.Write(iter.buf[index:insert.pos]); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + if _, err = w.Write(insert.buf); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + index = insert.pos |
| 111 | + } |
| 112 | + |
| 113 | + if index < iter.index { |
| 114 | + if _, err = w.Write(iter.buf[index:iter.index]); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if iter.next == nil { |
| 120 | + break |
| 121 | + } |
| 122 | + iter = iter.next |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func (b *blockbuf) insertOnOffset(data []byte, pos *blockBufPos) { |
| 128 | + var index = sort.Search(len(pos.block.insertions), func(i int) bool { return pos.block.insertions[i].pos >= pos.index }) |
| 129 | + |
| 130 | + if index < len(pos.block.insertions) { |
| 131 | + pos.block.insertions = append(pos.block.insertions[:index+1], pos.block.insertions[index:]...) |
| 132 | + pos.block.insertions[index] = newInsertion(data, pos.index) |
| 133 | + } else { |
| 134 | + pos.block.insertions = append(pos.block.insertions, newInsertion(data, pos.index)) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// func (b *blockbuf) getBytes() []byte { |
| 139 | +// var ( |
| 140 | +// iter = b.first |
| 141 | +// out = make([]byte, 0) |
| 142 | +// ) |
| 143 | + |
| 144 | +// for { |
| 145 | +// out = append(out, iter.buf[:iter.index]...) |
| 146 | +// if iter.next == nil { |
| 147 | +// break |
| 148 | +// } else { |
| 149 | +// iter = iter.next |
| 150 | +// } |
| 151 | +// } |
| 152 | +// return out |
| 153 | +// } |
| 154 | + |
| 155 | +// func (b *blockbuf) Print() { |
| 156 | +// var iter = b.first |
| 157 | +// for { |
| 158 | +// fmt.Printf("%+v\n", iter.buf) |
| 159 | +// if iter.next == nil { |
| 160 | +// break |
| 161 | +// } else { |
| 162 | +// iter = iter.next |
| 163 | +// } |
| 164 | +// } |
| 165 | +// } |
| 166 | + |
| 167 | +func newBlock() *block { |
| 168 | + return &block{ |
| 169 | + buf: make([]byte, bufsize), |
| 170 | + next: nil, |
| 171 | + index: 0, |
| 172 | + insertions: make([]*blockInsertion, 0), |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func newInsertion(insertion []byte, pos int) *blockInsertion { |
| 177 | + var buf = make([]byte, len(insertion)) |
| 178 | + copy(buf, insertion) |
| 179 | + return &blockInsertion{ |
| 180 | + buf: insertion, |
| 181 | + pos: pos, |
| 182 | + } |
| 183 | +} |
0 commit comments