feature_stream.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package jsoniter
  2. import (
  3. "io"
  4. )
  5. type Stream struct {
  6. cfg *frozenConfig
  7. out io.Writer
  8. buf []byte
  9. n int
  10. Error error
  11. indention int
  12. }
  13. func NewStream(cfg *frozenConfig, out io.Writer, bufSize int) *Stream {
  14. return &Stream{
  15. cfg: cfg,
  16. out: out,
  17. buf: make([]byte, bufSize),
  18. n: 0,
  19. Error: nil,
  20. indention: 0,
  21. }
  22. }
  23. func (b *Stream) Reset(out io.Writer) {
  24. b.out = out
  25. b.n = 0
  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. func (b *Stream) Buffer() []byte {
  36. return b.buf[:b.n]
  37. }
  38. // Write writes the contents of p into the buffer.
  39. // It returns the number of bytes written.
  40. // If nn < len(p), it also returns an error explaining
  41. // why the write is short.
  42. func (b *Stream) Write(p []byte) (nn int, err error) {
  43. for len(p) > b.Available() && b.Error == nil {
  44. if b.out == nil {
  45. b.growAtLeast(len(p))
  46. } else {
  47. var n int
  48. if b.Buffered() == 0 {
  49. // Large write, empty buffer.
  50. // Write directly from p to avoid copy.
  51. n, b.Error = b.out.Write(p)
  52. } else {
  53. n = copy(b.buf[b.n:], p)
  54. b.n += n
  55. b.Flush()
  56. }
  57. nn += n
  58. p = p[n:]
  59. }
  60. }
  61. if b.Error != nil {
  62. return nn, b.Error
  63. }
  64. n := copy(b.buf[b.n:], p)
  65. b.n += n
  66. nn += n
  67. return nn, nil
  68. }
  69. // WriteByte writes a single byte.
  70. func (b *Stream) writeByte(c byte) {
  71. if b.Error != nil {
  72. return
  73. }
  74. if b.Available() < 1 {
  75. b.growAtLeast(1)
  76. }
  77. b.buf[b.n] = c
  78. b.n++
  79. }
  80. func (b *Stream) writeTwoBytes(c1 byte, c2 byte) {
  81. if b.Error != nil {
  82. return
  83. }
  84. if b.Available() < 2 {
  85. b.growAtLeast(2)
  86. }
  87. b.buf[b.n] = c1
  88. b.buf[b.n+1] = c2
  89. b.n += 2
  90. }
  91. func (b *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
  92. if b.Error != nil {
  93. return
  94. }
  95. if b.Available() < 3 {
  96. b.growAtLeast(3)
  97. }
  98. b.buf[b.n] = c1
  99. b.buf[b.n+1] = c2
  100. b.buf[b.n+2] = c3
  101. b.n += 3
  102. }
  103. func (b *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
  104. if b.Error != nil {
  105. return
  106. }
  107. if b.Available() < 4 {
  108. b.growAtLeast(4)
  109. }
  110. b.buf[b.n] = c1
  111. b.buf[b.n+1] = c2
  112. b.buf[b.n+2] = c3
  113. b.buf[b.n+3] = c4
  114. b.n += 4
  115. }
  116. func (b *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
  117. if b.Error != nil {
  118. return
  119. }
  120. if b.Available() < 5 {
  121. b.growAtLeast(5)
  122. }
  123. b.buf[b.n] = c1
  124. b.buf[b.n+1] = c2
  125. b.buf[b.n+2] = c3
  126. b.buf[b.n+3] = c4
  127. b.buf[b.n+4] = c5
  128. b.n += 5
  129. }
  130. // Flush writes any buffered data to the underlying io.Writer.
  131. func (b *Stream) Flush() error {
  132. if b.out == nil {
  133. return nil
  134. }
  135. if b.Error != nil {
  136. return b.Error
  137. }
  138. if b.n == 0 {
  139. return nil
  140. }
  141. n, err := b.out.Write(b.buf[0:b.n])
  142. if n < b.n && err == nil {
  143. err = io.ErrShortWrite
  144. }
  145. if err != nil {
  146. if n > 0 && n < b.n {
  147. copy(b.buf[0:b.n-n], b.buf[n:b.n])
  148. }
  149. b.n -= n
  150. b.Error = err
  151. return err
  152. }
  153. b.n = 0
  154. return nil
  155. }
  156. func (b *Stream) ensure(minimal int) {
  157. available := b.Available()
  158. if available < minimal {
  159. if b.n > 1024 {
  160. b.Flush()
  161. }
  162. b.growAtLeast(minimal)
  163. }
  164. }
  165. func (b *Stream) growAtLeast(minimal int) {
  166. toGrow := len(b.buf)
  167. if toGrow < minimal {
  168. toGrow = minimal
  169. }
  170. newBuf := make([]byte, len(b.buf)+toGrow)
  171. copy(newBuf, b.Buffer())
  172. b.buf = newBuf
  173. }
  174. func (b *Stream) WriteRaw(s string) {
  175. b.ensure(len(s))
  176. if b.Error != nil {
  177. return
  178. }
  179. n := copy(b.buf[b.n:], s)
  180. b.n += n
  181. }
  182. func (stream *Stream) WriteNil() {
  183. stream.writeFourBytes('n', 'u', 'l', 'l')
  184. }
  185. func (stream *Stream) WriteTrue() {
  186. stream.writeFourBytes('t', 'r', 'u', 'e')
  187. }
  188. func (stream *Stream) WriteFalse() {
  189. stream.writeFiveBytes('f', 'a', 'l', 's', 'e')
  190. }
  191. func (stream *Stream) WriteBool(val bool) {
  192. if val {
  193. stream.WriteTrue()
  194. } else {
  195. stream.WriteFalse()
  196. }
  197. }
  198. func (stream *Stream) WriteObjectStart() {
  199. stream.indention += stream.cfg.indentionStep
  200. stream.writeByte('{')
  201. stream.writeIndention(0)
  202. }
  203. func (stream *Stream) WriteObjectField(field string) {
  204. stream.WriteString(field)
  205. stream.writeByte(':')
  206. }
  207. func (stream *Stream) WriteObjectEnd() {
  208. stream.writeIndention(stream.cfg.indentionStep)
  209. stream.indention -= stream.cfg.indentionStep
  210. stream.writeByte('}')
  211. }
  212. func (stream *Stream) WriteEmptyObject() {
  213. stream.writeByte('{')
  214. stream.writeByte('}')
  215. }
  216. func (stream *Stream) WriteMore() {
  217. stream.writeByte(',')
  218. stream.writeIndention(0)
  219. }
  220. func (stream *Stream) WriteArrayStart() {
  221. stream.indention += stream.cfg.indentionStep
  222. stream.writeByte('[')
  223. stream.writeIndention(0)
  224. }
  225. func (stream *Stream) WriteEmptyArray() {
  226. stream.writeByte('[')
  227. stream.writeByte(']')
  228. }
  229. func (stream *Stream) WriteArrayEnd() {
  230. stream.writeIndention(stream.cfg.indentionStep)
  231. stream.indention -= stream.cfg.indentionStep
  232. stream.writeByte(']')
  233. }
  234. func (stream *Stream) writeIndention(delta int) {
  235. if stream.indention == 0 {
  236. return
  237. }
  238. stream.writeByte('\n')
  239. toWrite := stream.indention - delta
  240. stream.ensure(toWrite)
  241. for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
  242. stream.buf[stream.n] = ' '
  243. stream.n++
  244. }
  245. }