Esempio n. 1
0
		public void Should_throw_parameters_greater_than_source_length()
		{
			var ms = new MemoryStream(new byte[] { 1, 2, 3, 4 });
			ArgumentOutOfRangeException exceptionFrom = null;
			ArgumentOutOfRangeException exceptionTo = null;

			try
			{
				var tested = new NarrowedStream(ms, 10, 20);
			}
			catch (ArgumentOutOfRangeException ex)
			{
				exceptionFrom = ex;
			}

			try
			{
				var tested = new NarrowedStream(ms, 0, 20);
			}
			catch (ArgumentOutOfRangeException ex)
			{
				exceptionTo = ex;
			}
			
			Assert.NotNull(exceptionFrom);
			Assert.NotNull(exceptionTo);
			Assert.Contains("from", exceptionFrom.Message);
			Assert.Contains("to", exceptionTo.Message);
		}
Esempio n. 2
0
        public void Check_reading_from_some_offset()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 1, 6);

            var reader = new StreamReader(tested);
            var result = reader.ReadToEnd();
            Assert.Equal("000010", result);
        }
Esempio n. 3
0
        public void Check_reading_from_begining()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 0, 5);

            var reader = new StreamReader(tested);
            var result = reader.ReadToEnd();
            Assert.Equal("000001", result);
        }
Esempio n. 4
0
        public void Check_copy_async()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 0, ms.Length - 1);

            var reader = new StreamReader(tested);
            var result = new MemoryStream();
            tested.CopyToAsync(result).Wait();
            Assert.Equal(500000*6, result.Length);
        }
Esempio n. 5
0
        public void Check_reading_all()
        {
            var ms = PrepareSourceStream();
            var tested = new NarrowedStream(ms, 0, ms.Length - 1);

            var reader = new StreamReader(tested);
            var result = reader.ReadToEnd();
            Assert.Equal(500000 * 6, result.Length);
            Assert.Equal(500000 * 6, tested.Length);
        }
Esempio n. 6
0
		public SourceFilePart(NarrowedStream sourceChunk)
			: base(sourceChunk)
		{
			this.sourceChunk = sourceChunk;

			Headers.ContentDisposition = new ContentDispositionHeaderValue("file");
			Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(SyncingMultipartConstants.NeedType,SyncingNeedType));
			Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(SyncingMultipartConstants.RangeFrom, sourceChunk.From.ToString(CultureInfo.InvariantCulture)));
			Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(SyncingMultipartConstants.RangeTo, sourceChunk.To.ToString(CultureInfo.InvariantCulture)));

			Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
		}
		public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
		{
			if (parent == null)
				throw new ArgumentNullException("parent");

			if (headers == null)
				throw new ArgumentNullException("headers");

			var parameters = headers.ContentDisposition.Parameters.ToDictionary(t => t.Name);

			var needType = parameters[SyncingMultipartConstants.NeedType].Value;
			var from = Convert.ToInt64(parameters[SyncingMultipartConstants.RangeFrom].Value);
			var to = Convert.ToInt64(parameters[SyncingMultipartConstants.RangeTo].Value);
			var length = (to - from + 1);

			NumberOfFileParts++;

			if (needType == "source")
			{
				BytesTransfered += length;

				return synchronizingFile;
			}

			if (needType == "seed")
			{
				var limitedStream = new NarrowedStream(localFile, from, to);
				limitedStream.CopyTo(synchronizingFile, StorageConstants.MaxPageSize);

				BytesCopied += length;

				return Stream.Null; // we can return Stream.Null because 'seed' part is always empty
			}

			throw new ArgumentException(string.Format("Invalid need type: '{0}'", needType));
		}
Esempio n. 8
0
		public static Task CopyToAsync(this Stream self, Stream destination, long from, long to)
		{
			var limitedStream = new NarrowedStream(self, from, to);
			return limitedStream.CopyToAsync(destination, StorageConstants.MaxPageSize);
		}