feature_stream.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 (stream *Stream) Reset(out io.Writer) {
  24. stream.out = out
  25. stream.n = 0
  26. }
  27. // Available returns how many bytes are unused in the buffer.
  28. func (stream *Stream) Available() int {
  29. return len(stream.buf) - stream.n
  30. }
  31. // Buffered returns the number of bytes that have been written into the current buffer.
  32. func (stream *Stream) Buffered() int {
  33. return stream.n
  34. }
  35. func (stream *Stream) Buffer() []byte {
  36. return stream.buf[:stream.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 (stream *Stream) Write(p []byte) (nn int, err error) {
  43. for len(p) > stream.Available() && stream.Error == nil {
  44. if stream.out == nil {
  45. stream.growAtLeast(len(p))
  46. } else {
  47. var n int
  48. if stream.Buffered() == 0 {
  49. // Large write, empty buffer.
  50. // Write directly from p to avoid copy.
  51. n, stream.Error = stream.out.Write(p)
  52. } else {
  53. n = copy(stream.buf[stream.n:], p)
  54. stream.n += n
  55. stream.Flush()
  56. }
  57. nn += n
  58. p = p[n:]
  59. }
  60. }
  61. if stream.Error != nil {
  62. return nn, stream.Error
  63. }
  64. n := copy(stream.buf[stream.n:], p)
  65. stream.n += n
  66. nn += n
  67. return nn, nil
  68. }
  69. // WriteByte writes a single byte.
  70. func (stream *Stream) writeByte(c byte) {
  71. if stream.Error != nil {
  72. return
  73. }
  74. if stream.Available() < 1 {
  75. stream.growAtLeast(1)
  76. }
  77. stream.buf[stream.n] = c
  78. stream.n++
  79. }
  80. func (stream *Stream) writeTwoBytes(c1 byte, c2 byte) {
  81. if stream.Error != nil {
  82. return
  83. }
  84. if stream.Available() < 2 {
  85. stream.growAtLeast(2)
  86. }
  87. stream.buf[stream.n] = c1
  88. stream.buf[stream.n+1] = c2
  89. stream.n += 2
  90. }
  91. func (stream *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
  92. if stream.Error != nil {
  93. return
  94. }
  95. if stream.Available() < 3 {
  96. stream.growAtLeast(3)
  97. }
  98. stream.buf[stream.n] = c1
  99. stream.buf[stream.n+1] = c2
  100. stream.buf[stream.n+2] = c3
  101. stream.n += 3
  102. }
  103. func (stream *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
  104. if stream.Error != nil {
  105. return
  106. }
  107. if stream.Available() < 4 {
  108. stream.growAtLeast(4)
  109. }
  110. stream.buf[stream.n] = c1
  111. stream.buf[stream.n+1] = c2
  112. stream.buf[stream.n+2] = c3
  113. stream.buf[stream.n+3] = c4
  114. stream.n += 4
  115. }
  116. func (stream *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
  117. if stream.Error != nil {
  118. return
  119. }
  120. if stream.Available() < 5 {
  121. stream.growAtLeast(5)
  122. }
  123. stream.buf[stream.n] = c1
  124. stream.buf[stream.n+1] = c2
  125. stream.buf[stream.n+2] = c3
  126. stream.buf[stream.n+3] = c4
  127. stream.buf[stream.n+4] = c5
  128. stream.n += 5
  129. }
  130. // Flush writes any buffered data to the underlying io.Writer.
  131. func (stream *Stream) Flush() error {
  132. if stream.out == nil {
  133. return nil
  134. }
  135. if stream.Error != nil {
  136. return stream.Error
  137. }
  138. if stream.n == 0 {
  139. return nil
  140. }
  141. n, err := stream.out.Write(stream.buf[0:stream.n])
  142. if n < stream.n && err == nil {
  143. err = io.ErrShortWrite
  144. }
  145. if err != nil {
  146. if n > 0 && n < stream.n {
  147. copy(stream.buf[0:stream.n-n], stream.buf[n:stream.n])
  148. }
  149. stream.n -= n
  150. stream.Error = err
  151. return err
  152. }
  153. stream.n = 0
  154. return nil
  155. }
  156. func (stream *Stream) ensure(minimal int) {
  157. available := stream.Available()
  158. if available < minimal {
  159. if stream.n > 1024 {
  160. stream.Flush()
  161. }
  162. stream.growAtLeast(minimal)
  163. }
  164. }
  165. func (stream *Stream) growAtLeast(minimal int) {
  166. toGrow := len(stream.buf)
  167. if toGrow < minimal {
  168. toGrow = minimal
  169. }
  170. newBuf := make([]byte, len(stream.buf)+toGrow)
  171. copy(newBuf, stream.Buffer())
  172. stream.buf = newBuf
  173. }
  174. func (stream *Stream) WriteRaw(s string) {
  175. stream.ensure(len(s))
  176. if stream.Error != nil {
  177. return
  178. }
  179. n := copy(stream.buf[stream.n:], s)
  180. stream.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. if stream.indention > 0 {
  206. stream.writeTwoBytes(':', ' ')
  207. } else {
  208. stream.writeByte(':')
  209. }
  210. }
  211. func (stream *Stream) WriteObjectEnd() {
  212. stream.writeIndention(stream.cfg.indentionStep)
  213. stream.indention -= stream.cfg.indentionStep
  214. stream.writeByte('}')
  215. }
  216. func (stream *Stream) WriteEmptyObject() {
  217. stream.writeByte('{')
  218. stream.writeByte('}')
  219. }
  220. func (stream *Stream) WriteMore() {
  221. stream.writeByte(',')
  222. stream.writeIndention(0)
  223. }
  224. func (stream *Stream) WriteArrayStart() {
  225. stream.indention += stream.cfg.indentionStep
  226. stream.writeByte('[')
  227. stream.writeIndention(0)
  228. }
  229. func (stream *Stream) WriteEmptyArray() {
  230. stream.writeByte('[')
  231. stream.writeByte(']')
  232. }
  233. func (stream *Stream) WriteArrayEnd() {
  234. stream.writeIndention(stream.cfg.indentionStep)
  235. stream.indention -= stream.cfg.indentionStep
  236. stream.writeByte(']')
  237. }
  238. func (stream *Stream) writeIndention(delta int) {
  239. if stream.indention == 0 {
  240. return
  241. }
  242. stream.writeByte('\n')
  243. toWrite := stream.indention - delta
  244. stream.ensure(toWrite)
  245. for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
  246. stream.buf[stream.n] = ' '
  247. stream.n++
  248. }
  249. }