示例#1
0
        public virtual void TestWriteTo()
        {
            FSDataInputStream     fsdin = new FSDataInputStream(new MockFSInputStream());
            ByteArrayOutputStream os    = new ByteArrayOutputStream();

            // new int[]{s_1, c_1, s_2, c_2, ..., s_n, c_n} means to test
            // reading c_i bytes starting at s_i
            int[] pairs = new int[] { 0, 10000, 50, 100, 50, 6000, 1000, 2000, 0, 1, 0, 0, 5000
                                      , 0 };
            NUnit.Framework.Assert.IsTrue("Pairs array must be even", pairs.Length % 2 == 0);
            for (int i = 0; i < pairs.Length; i += 2)
            {
                StreamFile.CopyFromOffset(fsdin, os, pairs[i], pairs[i + 1]);
                Assert.AssertArrayEquals("Reading " + pairs[i + 1] + " bytes from offset " + pairs
                                         [i], GetOutputArray(pairs[i], pairs[i + 1]), os.ToByteArray());
                os.Reset();
            }
        }
示例#2
0
 public virtual void TestSendPartialData()
 {
     FSDataInputStream     @in = new FSDataInputStream(new MockFSInputStream());
     ByteArrayOutputStream os  = new ByteArrayOutputStream();
     {
         // test if multiple ranges, then 416
         IList <InclusiveByteRange> ranges   = StrToRanges("0-,10-300", 500);
         HttpServletResponse        response = Org.Mockito.Mockito.Mock <HttpServletResponse>();
         StreamFile.SendPartialData(@in, os, response, 500, ranges);
         // Multiple ranges should result in a 416 error
         Org.Mockito.Mockito.Verify(response).SetStatus(416);
     }
     {
         // test if no ranges, then 416
         os.Reset();
         HttpServletResponse response = Org.Mockito.Mockito.Mock <HttpServletResponse>();
         StreamFile.SendPartialData(@in, os, response, 500, null);
         // No ranges should result in a 416 error
         Org.Mockito.Mockito.Verify(response).SetStatus(416);
     }
     {
         // test if invalid single range (out of bounds), then 416
         IList <InclusiveByteRange> ranges   = StrToRanges("600-800", 500);
         HttpServletResponse        response = Org.Mockito.Mockito.Mock <HttpServletResponse>();
         StreamFile.SendPartialData(@in, os, response, 500, ranges);
         // Single (but invalid) range should result in a 416
         Org.Mockito.Mockito.Verify(response).SetStatus(416);
     }
     {
         // test if one (valid) range, then 206
         IList <InclusiveByteRange> ranges   = StrToRanges("100-300", 500);
         HttpServletResponse        response = Org.Mockito.Mockito.Mock <HttpServletResponse>();
         StreamFile.SendPartialData(@in, os, response, 500, ranges);
         // Single (valid) range should result in a 206
         Org.Mockito.Mockito.Verify(response).SetStatus(206);
         Assert.AssertArrayEquals("Byte range from 100-300", GetOutputArray(100, 201), os.
                                  ToByteArray());
     }
 }
示例#3
0
        /// <exception cref="Javax.Servlet.ServletException"/>
        /// <exception cref="System.IO.IOException"/>
        protected override void DoGet(HttpServletRequest request, HttpServletResponse response
                                      )
        {
            string path        = ServletUtil.GetDecodedPath(request, "/streamFile");
            string rawPath     = ServletUtil.GetRawPath(request, "/streamFile");
            string filename    = JspHelper.ValidatePath(path);
            string rawFilename = JspHelper.ValidatePath(rawPath);

            if (filename == null)
            {
                response.SetContentType("text/plain");
                PrintWriter @out = response.GetWriter();
                @out.Write("Invalid input");
                return;
            }
            Enumeration <string> reqRanges = request.GetHeaders("Range");

            if (reqRanges != null && !reqRanges.MoveNext())
            {
                reqRanges = null;
            }
            DFSClient dfs;

            try
            {
                dfs = GetDFSClient(request);
            }
            catch (Exception e)
            {
                response.SendError(400, e.Message);
                return;
            }
            HdfsDataInputStream @in   = null;
            OutputStream        out_1 = null;

            try
            {
                @in   = dfs.CreateWrappedInputStream(dfs.Open(filename));
                out_1 = response.GetOutputStream();
                long fileLen = @in.GetVisibleLength();
                if (reqRanges != null)
                {
                    IList <InclusiveByteRange> ranges = InclusiveByteRange.SatisfiableRanges(reqRanges
                                                                                             , fileLen);
                    StreamFile.SendPartialData(@in, out_1, response, fileLen, ranges);
                }
                else
                {
                    // No ranges, so send entire file
                    response.SetHeader("Content-Disposition", "attachment; filename=\"" + rawFilename
                                       + "\"");
                    response.SetContentType("application/octet-stream");
                    response.SetHeader(ContentLength, string.Empty + fileLen);
                    StreamFile.CopyFromOffset(@in, out_1, 0L, fileLen);
                }
                @in.Close();
                @in = null;
                out_1.Close();
                out_1 = null;
                dfs.Close();
                dfs = null;
            }
            catch (IOException ioe)
            {
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("response.isCommitted()=" + response.IsCommitted(), ioe);
                }
                throw;
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
                IOUtils.Cleanup(Log, out_1);
                IOUtils.Cleanup(Log, dfs);
            }
        }
示例#4
0
 public TestStreamFile()
 {
     sfile = new _StreamFile_93(this);
 }