Exemple #1
0
        public override void ReadFile(string url, ResourceResponse response)
        {
                #if COHERENT_UNITY_UNSUPPORTED_PLATFORM
            throw new ApplicationException("Coherent UI doesn't support the target platform!");
                #else
            string cleanUrl = GetFilepath(url);

            if (!File.Exists(cleanUrl))
            {
                response.SignalFailure();
                return;
            }

            byte[] bytes = File.ReadAllBytes(cleanUrl);

            IntPtr buffer = response.GetBuffer((uint)bytes.Length);
            if (buffer == IntPtr.Zero)
            {
                response.SignalFailure();
                return;
            }

            Marshal.Copy(bytes, 0, buffer, bytes.Length);

            response.SignalSuccess();
                #endif
        }
		public override void ReadFile(string url, ResourceResponse response)
		{
		#if COHERENT_UNITY_UNSUPPORTED_PLATFORM
			throw new ApplicationException("Coherent UI doesn't support the target platform!");
		#else
			string cleanUrl = GetFilepath(url);
			
			if (!File.Exists(cleanUrl))
			{
				response.SignalFailure();
				return;
			}

			byte[] bytes = File.ReadAllBytes(cleanUrl);

			IntPtr buffer = response.GetBuffer((uint)bytes.Length);
			if (buffer == IntPtr.Zero)
			{
				response.SignalFailure();
				return;
			}

			Marshal.Copy(bytes, 0, buffer, bytes.Length);

			response.SignalSuccess();
		#endif
		}
Exemple #3
0
		public override void ReadFile(string url, ResourceResponse response)
		{
			string cleanUrl = GetFilepath(url);
			
			if (!File.Exists(cleanUrl))
			{
				response.SignalFailure();
				return;
			}

			byte[] bytes = File.ReadAllBytes(cleanUrl);

			IntPtr buffer = response.GetBuffer((uint)bytes.Length);
			if (buffer == IntPtr.Zero)
			{
				response.SignalFailure();
				return;
			}

			Marshal.Copy(bytes, 0, buffer, bytes.Length);

			response.SignalSuccess();
		}
Exemple #4
0
		public override void ReadFile(string url, URLRequestBase request,
			ResourceResponse response)
		{
		#if COHERENT_UNITY_UNSUPPORTED_PLATFORM
			throw new ApplicationException("Coherent UI doesn't support the target platform!");
		#else
			string cleanUrl = GetFilepath(url);

			if (!File.Exists(cleanUrl))
			{
				response.SignalFailure();
				return;
			}

			if (request.GetExtraHeaderIndex("Range") < 0)
			{
				DoCompleteRead(cleanUrl, request, response);
			}
			else
			{
				DoPartialRead(cleanUrl, request, response);
			}
		#endif
		}
Exemple #5
0
		private void DoCompleteRead(string cleanUrl, URLRequestBase request,
									ResourceResponse response)
		{
			byte[] bytes = File.ReadAllBytes(cleanUrl);

			IntPtr buffer = response.GetBuffer((uint)bytes.Length);
			if (buffer == IntPtr.Zero)
			{
				response.SignalFailure();
				return;
			}

			Marshal.Copy(bytes, 0, buffer, bytes.Length);

			response.SetStatus(200);
			response.SignalSuccess();
		}
Exemple #6
0
		private void DoPartialRead(string cleanUrl, URLRequestBase request,
								   ResourceResponse response)
		{
			string rangeValue = request.GetExtraHeader("Range");
			Match match = m_RangeRequestValue.Match (rangeValue);
			if (!match.Success)
			{
				response.SignalFailure();
				return;
			}

			long fileSize = new FileInfo(cleanUrl).Length;

			long startByte = long.Parse (match.Groups ["From"].Value);
			string endByteString = match.Groups ["To"].Value;
			long endByte = fileSize - 1;
			if (string.IsNullOrEmpty(endByteString))
			{
				// Clamp to a maximum chunk size
				const long MaxPartialReadSize = 16 * 1024 * 1024;
				if (endByte - startByte > MaxPartialReadSize)
				{
					endByte = startByte + MaxPartialReadSize;
				}
			}
			else
			{
				endByte = long.Parse(endByteString);
			}

			// Clamp to int.MaxValue since that's the type BinaryReader
			// allows us to read; if it could read more bytes, then we would
			// clamp the size to uint.MaxValue since ResourceResponse.GetBuffer
			// expects an uint value.
			long bufferSize = Math.Min((long)int.MaxValue,
									   endByte - startByte + 1);

			byte[] bytes = new byte[bufferSize];
			using (BinaryReader reader = new BinaryReader(
				new FileStream(cleanUrl, FileMode.Open)))
			{
				reader.BaseStream.Seek(startByte, SeekOrigin.Begin);
				reader.Read(bytes, 0, (int)bufferSize);
			}

			IntPtr buffer = response.GetBuffer((uint)bytes.Length);
			if (buffer == IntPtr.Zero)
			{
				response.SignalFailure();
				return;
			}

			Marshal.Copy(bytes, 0, buffer, bytes.Length);

			// Set required response headers
			response.SetStatus(206);
			response.SetResponseHeader("Accept-Ranges", "bytes");
			response.SetResponseHeader("Content-Range", "bytes " + startByte +
									   "-" + endByte + "/" + fileSize);
			response.SetResponseHeader("Content-Length",
									   bufferSize.ToString());

			response.SignalSuccess();
		}