Esempio n. 1
0
 /// <summary>Create with the specified URLOpeners.</summary>
 /// <remarks>
 /// Create with the specified URLOpeners. Original url is used to open the
 /// stream for the first time. Resolved url is used in subsequent requests.
 /// </remarks>
 /// <param name="o">Original url</param>
 /// <param name="r">Resolved url</param>
 /// <exception cref="System.IO.IOException"/>
 public ByteRangeInputStream(ByteRangeInputStream.URLOpener o, ByteRangeInputStream.URLOpener
                             r)
 {
     this.originalURL = o;
     this.resolvedURL = r;
     GetInputStream();
 }
Esempio n. 2
0
        protected internal virtual InputStream OpenInputStream()
        {
            // Use the original url if no resolved url exists, eg. if
            // it's the first time a request is made.
            bool resolved = resolvedURL.GetURL() != null;

            ByteRangeInputStream.URLOpener opener = resolved ? resolvedURL : originalURL;
            HttpURLConnection connection          = opener.Connect(startPos, resolved);

            resolvedURL.SetURL(GetResolvedUrl(connection));
            InputStream @in = connection.GetInputStream();
            IDictionary <string, IList <string> > headers = connection.GetHeaderFields();

            if (IsChunkedTransferEncoding(headers))
            {
                // file length is not known
                fileLength = null;
            }
            else
            {
                // for non-chunked transfer-encoding, get content-length
                long streamlength = GetStreamLength(connection, headers);
                fileLength = startPos + streamlength;
                // Java has a bug with >2GB request streams.  It won't bounds check
                // the reads so the transfer blocks until the server times out
                @in = new BoundedInputStream(@in, streamlength);
            }
            return(@in);
        }
 /// <exception cref="System.IO.IOException"/>
 private ByteRangeInputStream.URLOpener GetMockURLOpener(Uri url)
 {
     ByteRangeInputStream.URLOpener opener = Org.Mockito.Mockito.Mock <ByteRangeInputStream.URLOpener
                                                                       >(Org.Mockito.Mockito.CallsRealMethods);
     opener.SetURL(url);
     Org.Mockito.Mockito.DoReturn(GetMockConnection("65535")).When(opener).Connect(Matchers.AnyLong
                                                                                       (), Matchers.AnyBoolean());
     return(opener);
 }
        public virtual void TestByteRange()
        {
            ByteRangeInputStream.URLOpener oMock = GetMockURLOpener(new Uri("http://test"));
            ByteRangeInputStream.URLOpener rMock = GetMockURLOpener(null);
            ByteRangeInputStream           bris  = new TestByteRangeInputStream.ByteRangeInputStreamImpl
                                                       (this, oMock, rMock);

            bris.Seek(0);
            NUnit.Framework.Assert.AreEqual("getPos wrong", 0, bris.GetPos());
            bris.Read();
            NUnit.Framework.Assert.AreEqual("Initial call made incorrectly (offset check)", 0
                                            , bris.startPos);
            NUnit.Framework.Assert.AreEqual("getPos should return 1 after reading one byte",
                                            1, bris.GetPos());
            Org.Mockito.Mockito.Verify(oMock, Org.Mockito.Mockito.Times(1)).Connect(0, false);
            bris.Read();
            NUnit.Framework.Assert.AreEqual("getPos should return 2 after reading two bytes",
                                            2, bris.GetPos());
            // No additional connections should have been made (no seek)
            Org.Mockito.Mockito.Verify(oMock, Org.Mockito.Mockito.Times(1)).Connect(0, false);
            rMock.SetURL(new Uri("http://resolvedurl/"));
            bris.Seek(100);
            bris.Read();
            NUnit.Framework.Assert.AreEqual("Seek to 100 bytes made incorrectly (offset Check)"
                                            , 100, bris.startPos);
            NUnit.Framework.Assert.AreEqual("getPos should return 101 after reading one byte"
                                            , 101, bris.GetPos());
            Org.Mockito.Mockito.Verify(rMock, Org.Mockito.Mockito.Times(1)).Connect(100, true
                                                                                    );
            bris.Seek(101);
            bris.Read();
            // Seek to 101 should not result in another request
            Org.Mockito.Mockito.Verify(rMock, Org.Mockito.Mockito.Times(1)).Connect(100, true
                                                                                    );
            Org.Mockito.Mockito.Verify(rMock, Org.Mockito.Mockito.Times(0)).Connect(101, true
                                                                                    );
            bris.Seek(2500);
            bris.Read();
            NUnit.Framework.Assert.AreEqual("Seek to 2500 bytes made incorrectly (offset Check)"
                                            , 2500, bris.startPos);
            Org.Mockito.Mockito.DoReturn(GetMockConnection(null)).When(rMock).Connect(Matchers.AnyLong
                                                                                          (), Matchers.AnyBoolean());
            bris.Seek(500);
            try
            {
                bris.Read();
                NUnit.Framework.Assert.Fail("Exception should be thrown when content-length is not given"
                                            );
            }
            catch (IOException e)
            {
                NUnit.Framework.Assert.IsTrue("Incorrect response message: " + e.Message, e.Message
                                              .StartsWith(HttpHeaders.ContentLength + " is missing: "));
            }
            bris.Close();
        }
 /// <exception cref="System.IO.IOException"/>
 public ByteRangeInputStreamImpl(TestByteRangeInputStream _enclosing, ByteRangeInputStream.URLOpener
                                 o, ByteRangeInputStream.URLOpener r)
     : base(o, r)
 {
     this._enclosing = _enclosing;
 }