stream.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package jsoniter
  2. import (
  3. "io"
  4. )
  5. var bytesNull []byte
  6. var bytesTrue []byte
  7. var bytesFalse []byte
  8. func init() {
  9. bytesNull = []byte("null")
  10. bytesTrue = []byte("true")
  11. bytesFalse = []byte("false")
  12. }
  13. type Stream struct {
  14. out io.Writer
  15. buf []byte
  16. n int
  17. Error error
  18. indention int
  19. IndentionStep int
  20. }
  21. func NewStream(out io.Writer, bufSize int) *Stream {
  22. return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
  23. }
  24. // Available returns how many bytes are unused in the buffer.
  25. func (b *Stream) Available() int {
  26. return len(b.buf) - b.n
  27. }
  28. // Buffered returns the number of bytes that have been written into the current buffer.
  29. func (b *Stream) Buffered() int {
  30. return b.n
  31. }
  32. // Write writes the contents of p into the buffer.
  33. // It returns the number of bytes written.
  34. // If nn < len(p), it also returns an error explaining
  35. // why the write is short.
  36. func (b *Stream) Write(p []byte) (nn int, err error) {
  37. for len(p) > b.Available() && b.Error == nil {
  38. var n int
  39. if b.Buffered() == 0 {
  40. // Large write, empty buffer.
  41. // Write directly from p to avoid copy.
  42. n, b.Error = b.out.Write(p)
  43. } else {
  44. n = copy(b.buf[b.n:], p)
  45. b.n += n
  46. b.Flush()
  47. }
  48. nn += n
  49. p = p[n:]
  50. }
  51. if b.Error != nil {
  52. return nn, b.Error
  53. }
  54. n := copy(b.buf[b.n:], p)
  55. b.n += n
  56. nn += n
  57. return nn, nil
  58. }
  59. // WriteByte writes a single byte.
  60. func (b *Stream) writeByte(c byte) error {
  61. if b.Error != nil {
  62. return b.Error
  63. }
  64. if b.Available() <= 0 && b.Flush() != nil {
  65. return b.Error
  66. }
  67. b.buf[b.n] = c
  68. b.n++
  69. return nil
  70. }
  71. // Flush writes any buffered data to the underlying io.Writer.
  72. func (b *Stream) Flush() error {
  73. if b.Error != nil {
  74. return b.Error
  75. }
  76. if b.n == 0 {
  77. return nil
  78. }
  79. n, err := b.out.Write(b.buf[0:b.n])
  80. if n < b.n && err == nil {
  81. err = io.ErrShortWrite
  82. }
  83. if err != nil {
  84. if n > 0 && n < b.n {
  85. copy(b.buf[0:b.n - n], b.buf[n:b.n])
  86. }
  87. b.n -= n
  88. b.Error = err
  89. return err
  90. }
  91. b.n = 0
  92. return nil
  93. }
  94. func (b *Stream) WriteRaw(s string) {
  95. for len(s) > b.Available() && b.Error == nil {
  96. n := copy(b.buf[b.n:], s)
  97. b.n += n
  98. s = s[n:]
  99. b.Flush()
  100. }
  101. if b.Error != nil {
  102. return
  103. }
  104. n := copy(b.buf[b.n:], s)
  105. b.n += n
  106. }
  107. func (b *Stream) WriteString(s string) {
  108. b.writeByte('"')
  109. for len(s) > b.Available() && b.Error == nil {
  110. n := copy(b.buf[b.n:], s)
  111. b.n += n
  112. s = s[n:]
  113. b.Flush()
  114. }
  115. if b.Error != nil {
  116. return
  117. }
  118. n := copy(b.buf[b.n:], s)
  119. b.n += n
  120. b.writeByte('"')
  121. }
  122. func (stream *Stream) WriteNull() {
  123. stream.Write(bytesNull)
  124. }
  125. func (stream *Stream) WriteTrue() {
  126. stream.Write(bytesTrue)
  127. }
  128. func (stream *Stream) WriteFalse() {
  129. stream.Write(bytesFalse)
  130. }
  131. func (stream *Stream) WriteBool(val bool) {
  132. if val {
  133. stream.Write(bytesTrue)
  134. } else {
  135. stream.Write(bytesFalse)
  136. }
  137. }
  138. func (stream *Stream) WriteObjectStart() {
  139. stream.indention += stream.IndentionStep
  140. stream.writeByte('{')
  141. stream.writeIndention(0)
  142. }
  143. func (stream *Stream) WriteObjectField(field string) {
  144. stream.WriteString(field)
  145. stream.writeByte(':')
  146. }
  147. func (stream *Stream) WriteObjectEnd() {
  148. stream.writeIndention(stream.IndentionStep)
  149. stream.indention -= stream.IndentionStep
  150. stream.writeByte('}')
  151. }
  152. func (stream *Stream) WriteEmptyObject() {
  153. stream.writeByte('{')
  154. stream.writeByte('}')
  155. }
  156. func (stream *Stream) WriteMore() {
  157. stream.writeByte(',')
  158. stream.writeIndention(0)
  159. }
  160. func (stream *Stream) WriteArrayStart() {
  161. stream.indention += stream.IndentionStep
  162. stream.writeByte('[')
  163. stream.writeIndention(0)
  164. }
  165. func (stream *Stream) WriteEmptyArray() {
  166. stream.writeByte('[')
  167. stream.writeByte(']')
  168. }
  169. func (stream *Stream) WriteArrayEnd() {
  170. stream.writeIndention(stream.IndentionStep)
  171. stream.indention -= stream.IndentionStep
  172. stream.writeByte(']')
  173. }
  174. func (stream *Stream) writeIndention(delta int) {
  175. if (stream.indention == 0) {
  176. return
  177. }
  178. stream.writeByte('\n')
  179. toWrite := stream.indention - delta
  180. i := 0
  181. for {
  182. for ; i < toWrite && stream.n < len(stream.buf); i++ {
  183. stream.buf[stream.n] = ' '
  184. stream.n ++
  185. }
  186. if i == toWrite {
  187. break;
  188. } else {
  189. stream.Flush()
  190. }
  191. }
  192. }