views:

101

answers:

3

I want to parse all bytes downloaded by http request if they are > 100kb, if they are not, to concat them to a buffer and when the buffer gets > 100kb to parse them again and null the uffer. But not to parse all the file data and in the final chunk to get ex: 2kb. It must check the next chunk and if it's final to concat it (ex: final_chunk.Length + 2kb)

The point is that I need to parse at least 100kb chunks every time, nothing less.

+1  A: 

As PoweRoy Said you already got algorithm you just need code it... Let me put it step by step.

  1. Declare local buffer object
  2. Make Async/Synchronous HTTP request (I prefer Async Call)
  3. Save it to buffer
  4. Check buffer size (if it is > 100kb)
  5. If buffer size not >100kb Append the Data to buffer
  6. if it >100kb read the buffer and clear it.
  7. Goto step 2
Prashant
A: 

I guess that will do the trick. I just have to rewrite it with byte arrays. Do you find any bugs?

        class SmartChunk {

        private int ChunkSize;
        private int DataSize;
        private int pos;
        private int len;
        private string buff = "";

        public SmartChunk(int InitChunkSize, int DataLen) {
            pos = 0;
            len = DataLen;
            ChunkSize = InitChunkSize;
        }

        public string Append(string s) {
            if (pos + ChunkSize * 2 > len) ChunkSize = len - pos;

            if (s.Length >= ChunkSize) {
                return s;
            }
            else {
                buff += s;
                if (buff.Length >= ChunkSize) {
                    pos += buff.Length;
                    string b = buff;
                    buff = "";
                    return b;
                }
            }

            return null;
        }
    }
blez
A: 

And here it is with list of byte arrays:

class SmartChunk {
        private int ChunkSize;
        private int DataSize;
        private int pos;
        private int len;
        private List<byte[]> buff;

        public SmartChunk(int InitChunkSize, int DataLen) {
            buff = new List<byte[]>();
            pos = 0;
            len = DataLen;
            ChunkSize = InitChunkSize;
        }

        public List<byte[]> Append(byte[] b) {
            if (pos + ChunkSize * 2 > len) ChunkSize = len - pos;

            if (b.Length >= ChunkSize) {
                List<byte[]> priv = new List<byte[]>();
                priv.Add(b);
                return priv;
            }
            else {
                buff.Add(b);
                int total_size = 0;
                foreach(byte[] inner in buff){
                    total_size += inner.Length; 
                }

                if (total_size >= ChunkSize) {
                    pos += total_size;

                    List<byte[]> temp = new List<byte[]>(buff);
                    //foreach (byte[] tmp in buff) temp.Add(tmp);
                    buff.Clear();
                    return temp;
                }
            }

            return null;
        }
    }
blez