Exemplo n.º 1
0
        public static Stream FromUrl(string url)
        {
            try
            {
                int patience = 240; // seconds until we run out of patience waiting for generating a memory stream/tributary from a response to complete
                var start    = DateTime.Now;

                var request = (HttpWebRequest)WebRequest.Create(url);
                request.UserAgent         = "Platform-Installer/2.0.0.0(AppGal Automation v1.0)"; // used by the WebPI, bypasses problems like the license popup on CodePlex
                request.Timeout           = 90000;                                                // 1.5 minutes
                request.AllowAutoRedirect = true;
                request.Accept            = "*/*";
                request.KeepAlive         = true;
                request.CookieContainer   = new CookieContainer();

                // Even if we get a response within our allotted timeout (1.5 minutes, see above),
                // we still have to actually get all of the bits for the package. That means
                // going back and forth from this Web server to the server where the package
                // is kept, requesting a buffer's worth of bits at a time. If this takes
                // too long, then this Web server will throw an exception, thinking that
                // something has gone wrong with the connection, the response, the app pool,
                // etc. and there is nothing we can do here to catch that. We'll end up
                // going to the generic MS error page when the server thinks an exception
                // has occurred on the server. To avoid that nasty result, we are going to
                // time ourselves here. If we take more than 4 minutes overall we are going to
                // bail out.
                AbortIfDelayed(start, patience);

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    AbortIfDelayed(start, patience);

                    using (var responseStream = response.GetResponseStream())
                    {
                        AbortIfDelayed(start, patience);

                        var m      = new MemoryTributary();
                        var count  = 0;
                        var buffer = new byte[4096];
                        do
                        {
                            AbortIfDelayed(start, patience);

                            count = responseStream.Read(buffer, 0, buffer.Length);
                            m.Write(buffer, 0, count);
                        } while (count != 0);

                        return(m);
                    }
                }
            }
            catch (AbortGeneratingStreamException e)
            {
                throw e;
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        void ICommunicationService.UploadContent(UploadStreamMessage content)
        {
            Stream source = content.DataStream;
            MemoryTributary ms = new MemoryTributary();

            const int bufferLen = 65536;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = source.Read(buffer, 0, bufferLen)) > 0)
            {
                ms.Write(buffer, 0, count);
            }

            ms.Seek(0, SeekOrigin.Begin);
            try
            {
                if (owner != null)
                    owner.RouteData(content.Context, content.ContentType, ms);
            }
            catch
            {
            }
            //MessageBox.Show(content.ContentType + " " + content.Context + " " + ms.Length);
            ms.Close();
        }
Exemplo n.º 3
0
                public MemoryTributary GetHeaderLikeMemStream()
                {
                    MemoryTributary memStream = new MemoryTributary();
                    if (useTempFiles)
                    {
                        using (FileStream tmpFileStream = new FileStream(tmpHeaderFile, FileMode.Open))
                        {
                            tmpFileStream.CopyTo(memStream);
                        }
                    } else
                    {
                        if(this._pHeader.Length > 0)
                            memStream.Write(this._pHeader, 0, (int)this.HeaderSize);                        
                    }

                    memStream.Position = 0;
                    return memStream;
                }
Exemplo n.º 4
0
            private void ReadBlockData(BinaryReader inputFileStream, stBlockHeader? pBlockHeader, UInt32 elemHeaderAddr, out MemoryTributary pBlockDataStream, out UInt32 BlockDataSize)
            {
                pBlockDataStream = new MemoryTributary();

                if (pBlockHeader == null)
                    pBlockHeader = new stBlockHeader();

                UInt32 data_size = 0, page_size = 0, next_page_addr = 0;
                UInt32 read_in_bytes = 0, bytes_to_read = 0;

                BlockDataSize = 0;

                if (pBlockHeader != null)
                {
                    data_size = _httoi(((stBlockHeader)pBlockHeader).data_size_hex);
                    if (data_size == 0)
                    {
                        throw new Exception("ReadBlockData. Block мData == NULL.");
                    }
                }

                read_in_bytes = 0;
                UInt32 adr = elemHeaderAddr + stBlockHeader.Size(); // Конец header
                while (read_in_bytes < data_size)
                {
                    page_size = _httoi(((stBlockHeader)pBlockHeader).page_size_hex);
                    next_page_addr = _httoi(((stBlockHeader)pBlockHeader).next_page_addr_hex);

                    bytes_to_read = Math.Min(page_size, data_size - read_in_bytes);

                    inputFileStream.BaseStream.Position = adr;
                    pBlockDataStream.Write(inputFileStream.ReadBytes((int)bytes_to_read), 0, (int)bytes_to_read);
                    read_in_bytes += bytes_to_read;

                    if (next_page_addr != V8Formats.V8File.V8_FF_SIGNATURE) // есть следующая страница
                    {
                        adr = next_page_addr + stBlockHeader.Size();
                        inputFileStream.BaseStream.Position = next_page_addr;
                        pBlockHeader = new stBlockHeader(inputFileStream.ReadBytes((int)stBlockHeader.Size()), 0);
                    }
                    else
                        break;
                }

                BlockDataSize = data_size;
            }
Exemplo n.º 5
0
 public MemoryTributary DecompressEntry(int Index)
 {
     MemoryTributary result = new MemoryTributary();
     FileEntryStruct e = Files[Index];
    uint count = 0;
     byte[] inputBlock;
     byte[] outputBlock = new byte[Header.MaxBlockSize];
     long left = e.RealUncompressedSize;
     FileStream fs = new FileStream(MyFileName, FileMode.Open, FileAccess.Read);
     fs.Seek(e.BlockOffsets[0], SeekOrigin.Begin);
     byte[] buff;
     if (e.BlockSizeIndex == 0xFFFFFFFF)
     {
         buff = new byte[e.RealUncompressedSize];
         fs.Read(buff,0,buff.Length);
         result.Write(buff, 0, buff.Length);
     }
     else
     {
         while (left > 0)
         {
             uint compressedBlockSize = (uint)e.BlockSizes[count];
             if (compressedBlockSize == 0)
                 compressedBlockSize = Header.MaxBlockSize;
             if (compressedBlockSize == Header.MaxBlockSize || compressedBlockSize == left)
             {
                 buff = new byte[compressedBlockSize];
                 fs.Read(buff, 0, buff.Length);
                 result.Write(buff, 0, buff.Length);
                 left -= compressedBlockSize;
             }
             else
             {
                 var uncompressedBlockSize = (uint)Math.Min(left, Header.MaxBlockSize);
                 if (compressedBlockSize < 5)
                 {
                     throw new Exception("compressed block size smaller than 5");
                 }
                 inputBlock = new byte[compressedBlockSize];
                 fs.Read(inputBlock, 0, (int)compressedBlockSize);
                 uint actualUncompressedBlockSize = uncompressedBlockSize;
                 uint actualCompressedBlockSize = compressedBlockSize;
                 outputBlock = SevenZipHelper.Decompress(inputBlock, (int)actualUncompressedBlockSize);
                 if (outputBlock.Length != actualUncompressedBlockSize)
                     throw new Exception("Decompression Error");
                 result.Write(outputBlock, 0, (int)actualUncompressedBlockSize);
                 left -= uncompressedBlockSize;
             }
             count++;
         }
     }
     fs.Close();
     return result;
 }