stream.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package jsoniter
  2. import (
  3. "io"
  4. )
  5. var bytesNull []byte
  6. var digits []uint8;
  7. func init() {
  8. bytesNull = []byte("null")
  9. digits = []uint8{
  10. '0', '1', '2', '3', '4', '5',
  11. '6', '7', '8', '9', 'a', 'b',
  12. 'c', 'd', 'e', 'f', 'g', 'h',
  13. 'i', 'j', 'k', 'l', 'm', 'n',
  14. 'o', 'p', 'q', 'r', 's', 't',
  15. 'u', 'v', 'w', 'x', 'y', 'z',
  16. }
  17. }
  18. type Stream struct {
  19. out io.Writer
  20. buf []byte
  21. n int
  22. Error error
  23. }
  24. func NewStream(out io.Writer, bufSize int) *Stream {
  25. return &Stream{out, make([]byte, bufSize), 0, nil}
  26. }
  27. // Available returns how many bytes are unused in the buffer.
  28. func (b *Stream) Available() int {
  29. return len(b.buf) - b.n
  30. }
  31. // Buffered returns the number of bytes that have been written into the current buffer.
  32. func (b *Stream) Buffered() int {
  33. return b.n
  34. }
  35. // Write writes the contents of p into the buffer.
  36. // It returns the number of bytes written.
  37. // If nn < len(p), it also returns an error explaining
  38. // why the write is short.
  39. func (b *Stream) Write(p []byte) (nn int, err error) {
  40. for len(p) > b.Available() && b.Error == nil {
  41. var n int
  42. if b.Buffered() == 0 {
  43. // Large write, empty buffer.
  44. // Write directly from p to avoid copy.
  45. n, b.Error = b.out.Write(p)
  46. } else {
  47. n = copy(b.buf[b.n:], p)
  48. b.n += n
  49. b.Flush()
  50. }
  51. nn += n
  52. p = p[n:]
  53. }
  54. if b.Error != nil {
  55. return nn, b.Error
  56. }
  57. n := copy(b.buf[b.n:], p)
  58. b.n += n
  59. nn += n
  60. return nn, nil
  61. }
  62. // WriteByte writes a single byte.
  63. func (b *Stream) WriteByte(c byte) error {
  64. if b.Error != nil {
  65. return b.Error
  66. }
  67. if b.Available() <= 0 && b.Flush() != nil {
  68. return b.Error
  69. }
  70. b.buf[b.n] = c
  71. b.n++
  72. return nil
  73. }
  74. // Flush writes any buffered data to the underlying io.Writer.
  75. func (b *Stream) Flush() error {
  76. if b.Error != nil {
  77. return b.Error
  78. }
  79. if b.n == 0 {
  80. return nil
  81. }
  82. n, err := b.out.Write(b.buf[0:b.n])
  83. if n < b.n && err == nil {
  84. err = io.ErrShortWrite
  85. }
  86. if err != nil {
  87. if n > 0 && n < b.n {
  88. copy(b.buf[0:b.n - n], b.buf[n:b.n])
  89. }
  90. b.n -= n
  91. b.Error = err
  92. return err
  93. }
  94. b.n = 0
  95. return nil
  96. }
  97. func (b *Stream) WriteString(s string) {
  98. for len(s) > b.Available() && b.Error == nil {
  99. n := copy(b.buf[b.n:], s)
  100. b.n += n
  101. s = s[n:]
  102. b.Flush()
  103. }
  104. if b.Error != nil {
  105. return
  106. }
  107. n := copy(b.buf[b.n:], s)
  108. b.n += n
  109. }
  110. func (stream *Stream) WriteNull() {
  111. stream.Write(bytesNull)
  112. }
  113. func (stream *Stream) WriteUint8(val uint8) {
  114. if stream.Available() < 3 {
  115. stream.Flush()
  116. }
  117. charPos := stream.n
  118. if val <= 9 {
  119. charPos += 1;
  120. } else {
  121. if val <= 99 {
  122. charPos += 2;
  123. } else {
  124. charPos += 3;
  125. }
  126. }
  127. stream.n = charPos
  128. var q uint8
  129. var r uint8
  130. for {
  131. q = val / 10
  132. r = val - ((q << 3) + (q << 1)) // r = i-(q*10) ...
  133. charPos--
  134. stream.buf[charPos] = digits[r]
  135. val = q;
  136. if val == 0 {
  137. break
  138. }
  139. }
  140. }
  141. func (stream *Stream) WriteInt8(val int8) {
  142. if stream.Available() < 4 {
  143. stream.Flush()
  144. }
  145. charPos := stream.n
  146. if (val < 0) {
  147. charPos += 1
  148. val = -val
  149. stream.buf[stream.n] = '-'
  150. }
  151. if val <= 9 {
  152. charPos += 1;
  153. } else {
  154. if val <= 99 {
  155. charPos += 2;
  156. } else {
  157. charPos += 3;
  158. }
  159. }
  160. stream.n = charPos
  161. var q int8
  162. var r int8
  163. for {
  164. q = val / 10
  165. r = val - ((q << 3) + (q << 1)) // r = i-(q*10) ...
  166. charPos--
  167. stream.buf[charPos] = digits[r]
  168. val = q;
  169. if val == 0 {
  170. break
  171. }
  172. }
  173. }
  174. func (stream *Stream) WriteUint16(val uint16) {
  175. if stream.Available() < 5 {
  176. stream.Flush()
  177. }
  178. charPos := stream.n
  179. if val <= 99 {
  180. if val <= 9 {
  181. charPos += 1;
  182. } else {
  183. charPos += 2;
  184. }
  185. } else {
  186. if val <= 999 {
  187. charPos += 3;
  188. } else {
  189. if val <= 9999 {
  190. charPos += 4;
  191. } else {
  192. charPos += 5;
  193. }
  194. }
  195. }
  196. stream.n = charPos
  197. var q uint16
  198. var r uint16
  199. for {
  200. q = val / 10
  201. r = val - ((q << 3) + (q << 1)) // r = i-(q*10) ...
  202. charPos--
  203. stream.buf[charPos] = digits[r]
  204. val = q;
  205. if val == 0 {
  206. break
  207. }
  208. }
  209. }
  210. func (stream *Stream) WriteVal(val interface{}) {
  211. }