Exemple #1
0
        public void addVariableULong(ulong number, int index = -1)
        {
            byte[] target = null;
            BytesBuilder.VariableULongToBytes(number, ref target);

            add(target, index);
        }
Exemple #2
0
        public static unsafe byte[] CloneBytes(byte *b, long start, long PostEnd)
        {
            var result = new byte[PostEnd - start];

            fixed(byte *r = result)
            BytesBuilder.CopyTo(PostEnd, PostEnd - start, b, r, 0, -1, start);

            return(result);
        }
Exemple #3
0
        public void clear(bool fast = false)
        {
            if (!fast)
            {
                foreach (byte[] e in bytes)
                {
                    BytesBuilder.ToNull(e);
                }
            }

            count = 0;
            bytes.Clear();
        }
Exemple #4
0
        public long RemoveBlockAt(int position)
        {
            if (count == 0 || position < 0 || position >= bytes.Count)
            {
                return(0);
            }

            long removedLength = bytes[position].LongLength;
            var  tmp           = bytes[position];

            bytes.RemoveAt(position);
            BytesBuilder.BytesToNull(tmp);

            count -= removedLength;
            return(removedLength);
        }
Exemple #5
0
        public long RemoveLastBlock()
        {
            if (count == 0)
            {
                return(0);
            }

            long removedLength = bytes[bytes.Count - 1].LongLength;
            var  tmp           = bytes[bytes.Count - 1];

            bytes.RemoveAt(bytes.Count - 1);
            BytesBuilder.BytesToNull(tmp);

            count -= removedLength;
            return(removedLength);
        }
Exemple #6
0
        public long RemoveBlocks(int position, int endPosition)
        {
            if (count == 0 || position < 0 || position >= bytes.Count || position > endPosition || endPosition >= bytes.Count)
            {
                return(0);
            }

            long removedLength = 0;

            for (int i = position; i <= endPosition; i++)
            {
                var tmp = bytes[position];
                removedLength += RemoveBlockAt(position);
                BytesBuilder.BytesToNull(tmp);
            }

            return(removedLength);
        }
Exemple #7
0
        public void add(byte[] bytesToAdded, int index = -1, bool isNoConstant = false)
        {
            if (isNoConstant)
            {
                var b = new byte[bytesToAdded.LongLength];
                BytesBuilder.CopyTo(bytesToAdded, b);
                bytesToAdded = b;
            }

            if (index == -1)
            {
                bytes.Add(bytesToAdded);
            }
            else
            {
                bytes.Insert((int)index, bytesToAdded);
            }

            count += bytesToAdded.LongLength;
        }
Exemple #8
0
        public byte[] getFile(string hostName, string referer)
        {
            HttpStatusCode status = 0;
            long           ctLen  = 0;
            var            a      = 0;
            BytesBuilder   bb;

            byte[] bt = null;
            try
            {
                bt = getFile(hostName, referer, out ctLen, out status);

                if (bt.Length >= ctLen)
                {
                    return(bt);
                }

                bb = new BytesBuilder();
                bb.add(bt);

                a = bt.Length;
            }
            catch
            {
                bb = new BytesBuilder();
            }

            var size = a;

            if (size < 1400)
            {
                size = 64 * 1024;
            }

            var tryCount = 0;

            do
            {
                long ctLen2 = 0;

                try
                {
                    updator.toUpdateLog("try to partial download from " + a + ", with size " + size + "\r\n\r\n");

                    status   = 0;
                    bt       = getFile(hostName, referer, out ctLen2, out status, a, (a + size < (int)ctLen) ? a + size : (int)ctLen);
                    a       += bt.Length;
                    tryCount = 0;
                }
                catch (WebException e)
                {
                    tryCount++;

                    try
                    {
                        if (e.Response != null && ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        updator.toUpdateLog("error (getFile) with message " + ex.Message + "\r\n" + ex.StackTrace + "\r\n");
                    }

                    updator.toUpdateLog("error (getFile) with message " + e.Message + "\r\n" + e.StackTrace + "\r\n\r\n");

                    size -= size >> 2;
                    if (size < 1400 * 4)
                    {
                        size = 1400 * 4;
                    }

                    if (tryCount > 16)
                    {
                        throw;
                    }

                    continue;
                }
                catch (Exception e)
                {
                    tryCount++;

                    updator.toUpdateLog("error (getFile) with message " + e.Message + "\r\n" + e.StackTrace + "\r\n\r\n");

                    size -= size >> 2;
                    if (size < 1400 * 4)
                    {
                        size = 1400 * 4;
                    }

                    if (tryCount > 16)
                    {
                        throw;
                    }

                    continue;
                }

                if (bt.Length <= 0)
                {
                    break;
                }

                updator.toUpdateLog("success partial download for one part with size " + bt.Length + "\r\n\r\n");
                bb.add(bt);
            }while (a < ctLen || ctLen == 0);

            return(bb.getBytes());
        }