(data []byte)
| 75 | } |
| 76 | |
| 77 | func (w *StreamingResponseWriter) Write(data []byte) (int, error) { |
| 78 | if !w.headerSent { |
| 79 | w.WriteHeader(http.StatusOK) |
| 80 | } |
| 81 | |
| 82 | originalLen := len(data) |
| 83 | |
| 84 | // If we already have data in the buffer |
| 85 | if w.buffer.Len() > 0 { |
| 86 | // Fill the buffer up to maxChunkSize |
| 87 | spaceInBuffer := maxChunkSize - w.buffer.Len() |
| 88 | if spaceInBuffer > 0 { |
| 89 | // How much of the new data can fit in the buffer |
| 90 | toBuffer := spaceInBuffer |
| 91 | if toBuffer > len(data) { |
| 92 | toBuffer = len(data) |
| 93 | } |
| 94 | w.buffer.Write(data[:toBuffer]) |
| 95 | data = data[toBuffer:] // Advance data slice |
| 96 | } |
| 97 | |
| 98 | // If buffer is full, send it |
| 99 | if w.buffer.Len() == maxChunkSize { |
| 100 | w.sendChunk(w.buffer.Bytes()) |
| 101 | w.buffer.Reset() |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Send any full chunks from data |
| 106 | for len(data) >= maxChunkSize { |
| 107 | w.sendChunk(data[:maxChunkSize]) |
| 108 | data = data[maxChunkSize:] |
| 109 | } |
| 110 | |
| 111 | // Buffer any remaining data |
| 112 | if len(data) > 0 { |
| 113 | w.buffer.Write(data) |
| 114 | } |
| 115 | |
| 116 | return originalLen, nil |
| 117 | } |
| 118 | |
| 119 | func (w *StreamingResponseWriter) Close() error { |
| 120 | if !w.headerSent { |
no test coverage detected