OK, so my understanding of the question is this:
- You are processing an incoming stream of uint8 encoded characters.
- After decoding from uint8, there is another encoding, the HTTP transfer encoding type "chunked". Which also needs decoding
- However, the buffer on the incoming stream, and hence the data you get on read, doesn't match the "chunks" of the wrapped encoding.
I'm going to give you a prose answer, because its late and its going to be hard to write and test something with all the libs you are using and stuff. But I think the idea should be fairly simple. (Also because haters gona close the question before it can be answered!!)
Implement a second buffer. and keep track of whether you are at the end of a chunk or not.Implement a second buffer. and keep track of whether you are at the end of a chunk or not.
When you receive the first write, you are guaranteed to be at the start of a chunk, start decoding as normal,
- Convert each character to unicode
Convert each character to unicode (or request body encoding from the header?)
- When you hit the 13, 10 you have the length. SAVE THIS
When you hit the 13 (/r), 10 (/l) you have the length. SAVE THIS
eg 49,48,48,48 utf-8 => 1000
Read this as hex digits 1000 = 4096
- Read the next length characters put this in your buffer
Read the next length number of ints and put this in your buffer
- Put the buffer in your decoded output.
Put the buffer in your output.
- Wipe the buffer data
Wipe the buffer data
- Loop
Loop
All good so far, but OH NO! you hit the end of the array before you get to the end of a chunk!
- Skip moving the buffer and wiping it.
- Next write comes in
- Is the buffer empty? No
- continue decodingreading and adding to the buffer until the buffer length matches the length you stored earlier
- Put the buffer in your decoded output.
- Wipe the buffer data
- Loop