Esempio n. 1
0
        public void SetContentFromInputStream()
        {
            string         json = "{\"hello\":\"world\",\"foo\":\"bar\"}";
            DiskFileUpload f1   = new DiskFileUpload("file3", "file3", "application/json", null, null, 0);

            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                var    buf   = Unpooled.WrappedBuffer(bytes);
                var    bs    = new ByteBufferStream(buf);
                try
                {
                    f1.SetContent(bs);
                    Assert.Equal(json, f1.GetString());
                    Assert.True(bytes.AsSpan().SequenceEqual(f1.GetBytes()));
                    var file = f1.GetFile();
                    Assert.Equal(bytes.Length, file.Length);
                    Assert.True(bytes.AsSpan().SequenceEqual(DoReadFile(file, bytes.Length)));
                }
                finally
                {
                    buf.Release();
                }
            }
            finally
            {
                f1.Delete();
            }
        }
Esempio n. 2
0
        public void AddContents()
        {
            DiskFileUpload f1 = new DiskFileUpload("file1", "file1", "application/json", null, null, 0);

            try
            {
                string json  = "{\"foo\":\"bar\"}";
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                f1.AddContent(Unpooled.WrappedBuffer(bytes), true);
                Assert.Equal(json, f1.GetString());
                Assert.True(bytes.AsSpan().SequenceEqual(f1.GetBytes()));
                var fis = f1.GetFile();
                Assert.Equal(bytes.Length, fis.Length);

                byte[] buf    = new byte[bytes.Length];
                int    offset = 0;
                int    read   = 0;
                int    len    = buf.Length;
                fis.Position = 0;
                while ((read = fis.Read(buf, offset, len)) > 0)
                {
                    len    -= read;
                    offset += read;
                    if (len <= 0 || offset >= buf.Length)
                    {
                        break;
                    }
                }
                Assert.True(bytes.AsSpan().SequenceEqual(buf));
            }
            finally
            {
                f1.Delete();
            }
        }
Esempio n. 3
0
        public void SetContentFromByteBuf()
        {
            DiskFileUpload f1 = new DiskFileUpload("file2", "file2", "application/json", null, null, 0);

            try
            {
                string json  = "{\"hello\":\"world\"}";
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                f1.SetContent(Unpooled.WrappedBuffer(bytes));
                Assert.Equal(json, f1.GetString());
                Assert.True(bytes.AsSpan().SequenceEqual(f1.GetBytes()));
                var file = f1.GetFile();
                Assert.Equal(bytes.Length, file.Length);
                file.Position = 0;
                Assert.True(bytes.AsSpan().SequenceEqual(DoReadFile(file, bytes.Length)));
            }
            finally
            {
                f1.Delete();
            }
        }