Seek() 공개 메소드

public Seek ( long offset, SeekOrigin loc ) : long
offset long
loc SeekOrigin
리턴 long
		public void WriteByte ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, 3, 5, FileAccess.ReadWrite);
			ums.WriteByte (testStreamData [0]);
			Assert.AreEqual (5, ums.Capacity, "#A1");
			Assert.AreEqual (1, ums.Position, "#A2");
			Assert.AreEqual (3, ums.Length, "#A3");
			ums.WriteByte (testStreamData [1]);
			Assert.AreEqual (5, ums.Capacity, "#B1");
			Assert.AreEqual (2, ums.Position, "#B2");
			Assert.AreEqual (3, ums.Length, "#B3");
			ums.WriteByte (testStreamData [2]);
			Assert.AreEqual (5, ums.Capacity, "#C1");
			Assert.AreEqual (3, ums.Position, "#C2");
			Assert.AreEqual (3, ums.Length, "#C3");
			ums.WriteByte (testStreamData [3]);
			Assert.AreEqual (5, ums.Capacity, "#D1");
			Assert.AreEqual (4, ums.Position, "#D2");
			Assert.AreEqual (4, ums.Length, "#D3");
			ums.WriteByte (testStreamData [4]);
			Assert.AreEqual (5, ums.Capacity, "#E1");
			Assert.AreEqual (5, ums.Position, "#E2");
			Assert.AreEqual (5, ums.Length, "#E3");
			ums.Seek (0, SeekOrigin.Begin);
			Assert.AreEqual (testStreamData [0], ums.ReadByte (), "#F1");
			Assert.AreEqual (testStreamData [1], ums.ReadByte (), "#F2");
			Assert.AreEqual (testStreamData [2], ums.ReadByte (), "#F3");
			Assert.AreEqual (testStreamData [3], ums.ReadByte (), "#F4");
			Assert.AreEqual (testStreamData [4], ums.ReadByte (), "#F5");
			ums.Close ();
		}
 public static void PlaySoundResource(UnmanagedMemoryStream unmanagedMemoryStream)
 {
     try
     {
         // http://msdn.microsoft.com/en-us/library/3w5b27z4.aspx
         SoundPlayer player = new SoundPlayer();
         unmanagedMemoryStream.Seek(0, SeekOrigin.Begin);
         player.Stream = unmanagedMemoryStream;
         player.Play();
     }
     catch (Exception x)
     {
         Console.WriteLine("PlaySoundResource: Error trying to access or play the resource: " + x.Message);
     }
 }
		public void Seek_Stream_Closed () 
		{
			UnmanagedMemoryStream ums = new
				UnmanagedMemoryStream(mem_byteptr, length);
			ums.Close ();
			ums.Seek (0, SeekOrigin.Begin);
		}
		public void Constructor1 ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length);
			Assert.AreEqual ((long) length, ums.Capacity, "#1");
			Assert.AreEqual ((long) length, ums.Length, "#2");
			Assert.AreEqual (0L, ums.Position, "#3");
			ums.Position = (length-2);
			Assert.AreEqual ((long)(length - 2), ums.Position, "#4");
			ums.Position = 0;
			ums.Seek(3L, SeekOrigin.Begin);
			Assert.AreEqual (3L, ums.Position, "#5");
			Assert.IsTrue (ums.CanRead, "#6");
			Assert.IsFalse (ums.CanWrite, "#7");
			ums.Close();
		}
		public void Seek_Begin_Overflow ()
		{
			using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream (mem_byteptr, 5, 10, FileAccess.Read)) {
				Assert.AreEqual (Int64.MaxValue, ums.Seek (Int64.MaxValue, SeekOrigin.Begin), "Seek");
				Assert.AreEqual (Int64.MaxValue, ums.Position, "Position");
				try {
					byte* p = ums.PositionPointer;
					Assert.Fail ("#1");
				}
				catch (IndexOutOfRangeException ex) {
					Assert.AreEqual (typeof (IndexOutOfRangeException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
				}
			}
		}
		public void Seek_End_Overflow ()
		{
			using (UnmanagedMemoryStream ums = new UnmanagedMemoryStream (mem_byteptr, 5, 10, FileAccess.Read)) {
				try {
					ums.Seek (Int64.MaxValue, SeekOrigin.End);
					Assert.Fail ("#1");
				}
				catch (IOException ex) {
					Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
				}
			}
		}
		public void Seek_Origin_Invalid ()
		{
			UnmanagedMemoryStream ums = new
				UnmanagedMemoryStream(mem_byteptr, 5, 10, FileAccess.Read);
			try {
				ums.Seek(1, (SeekOrigin) 666);
				Assert.Fail ("#1");
			} catch (ArgumentException ex) {
				// Invalid seek origin
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNull (ex.ParamName, "#5");
			}
			ums.Close();
		}
        private string TryWriteBamlResource(string resourcePath, bool isBamlResource, UnmanagedMemoryStream unmanagedStream)
        {
            if (unmanagedStream == null)
            {
                return null;
            }

            string resourceDir = Path.GetDirectoryName(resourcePath);
            if (!Directory.Exists(resourceDir))
            {
                Directory.CreateDirectory(resourceDir);
            }

            Stream sourceStream;
            string fullClassName = null;
#if ENGINEONLYBUILD
            sourceStream = unmanagedStream;
#else
            XDocument xamlDoc = null;

            bool exceptionOccurred = false;
            //TODO: refactor
            if (isBamlResource)
            {
                sourceStream = new MemoryStream();
                try
                {
                    unmanagedStream.Seek(0, SeekOrigin.Begin);
                    xamlDoc = BamlToXamlConverter.DecompileToDocument(unmanagedStream, currentAssemblyResolver, assemblyPath);
#if !NET35
                    xamlDoc.Save(sourceStream);
#else
                    xamlDoc.Save(new StreamWriter(sourceStream));
#endif
                }
                catch
                {
                    exceptionOccurred = true;
                    unmanagedStream.Seek(0, SeekOrigin.Begin);
                    sourceStream = unmanagedStream;
                }
            }
            else
            {
                sourceStream = unmanagedStream;
            }

            if (isBamlResource && !exceptionOccurred)
            {
                fullClassName = Utilities.GetXamlTypeFullName(xamlDoc);
                if (fullClassName != null)
                {
                    xamlGeneratedFields.Add(fullClassName, Utilities.GetXamlGeneratedFields(xamlDoc));
                }
            }
#endif

            using (FileStream fileStream = new FileStream(resourcePath, FileMode.Create, FileAccess.Write))
            {
                using (sourceStream)
                {
                    sourceStream.Seek(0, SeekOrigin.Begin);
                    sourceStream.CopyTo(fileStream);
                }
            }

            return fullClassName;
        }
		public void ReadByte_EndOfStream ()
		{
			UnmanagedMemoryStream ums = new
				UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			ums.Position = 0;
			ums.Seek(length, SeekOrigin.Begin);
			Assert.AreEqual (-1, ums.ReadByte (), "#3");
			ums.Seek(length + 1, SeekOrigin.Begin);
			Assert.AreEqual (-1, ums.ReadByte (), "#4");
			ums.Seek(capacity, SeekOrigin.Begin);
			Assert.AreEqual (-1, ums.ReadByte (), "#5");
			ums.Seek(capacity + 1, SeekOrigin.Begin);
			Assert.AreEqual (-1, ums.ReadByte (), "#6");
			ums.Close();
		}
		public void Seek ()
		{
			UnmanagedMemoryStream ums = new
				UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.Write);
			Assert.AreEqual (5, ums.Seek (5, SeekOrigin.Begin), "#A1");
			Assert.AreEqual (5, ums.Position, "#A2");
			Assert.AreEqual (length, ums.Length, "#A3");

			ums.Seek (2, SeekOrigin.Current);
			//Assert.AreEqual (7, ums.Seek (2, SeekOrigin.Current), "#B1");
			Assert.AreEqual (7, ums.Position, "#B2");
			Assert.AreEqual (length, ums.Length, "#B3");

			Assert.AreEqual (length + 2, ums.Seek (2, SeekOrigin.End), "#C1");
			Assert.AreEqual (length + 2, ums.Position, "#C2");
			Assert.AreEqual (length, ums.Length, "#C3");

			Assert.AreEqual (0, ums.Seek (0, SeekOrigin.Begin), "#D1");
			Assert.AreEqual (0, ums.Position, "#D2");
			Assert.AreEqual (length, ums.Length, "#D3");

			Assert.AreEqual (length - 2, ums.Seek (-2, SeekOrigin.End), "#E1");
			Assert.AreEqual (length - 2, ums.Position, "#E2");
			Assert.AreEqual (length, ums.Length, "#E3");

			Assert.AreEqual (length - 5, ums.Seek (-3, SeekOrigin.Current), "#F1");
			Assert.AreEqual (length - 5, ums.Position, "#F2");
			Assert.AreEqual (length, ums.Length, "#F3");

			Assert.AreEqual (capacity + 5, ums.Seek (capacity + 5, SeekOrigin.Begin), "#G1");
			Assert.AreEqual (capacity + 5, ums.Position, "#G2");
			Assert.AreEqual (length, ums.Length, "#G3");
		}
		public void Read_EndOfStream ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length, length * 2, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			Assert.AreEqual (0, ums.Read (readData, 0, 1), "#1");
			ums.Seek(length + 1, SeekOrigin.Begin);
			Assert.AreEqual (0, ums.Read (readData, 0, 1), "#2");
			ums.Seek(length - 3, SeekOrigin.Begin);
			Assert.AreEqual (3, ums.Read (readData, 0, 5), "#3");
			ums.Seek(capacity + 1, SeekOrigin.Begin);
			Assert.AreEqual (0, ums.Read (readData, 0, 1), "#4");
			ums.Close ();
		}
		public void Read ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			ums.Position = 0;

			Assert.AreEqual (length / 2, ums.Read (readData, 0, (length / 2)), "#1");
			VerifyTestData ("#2", readData, 0, (length / 2));
			Assert.AreEqual (length / 2, ums.Position, "#3");
			
			//Seek back to begining
			ums.Seek (0, SeekOrigin.Begin);
			
			//Read complete stream
			Assert.AreEqual (length, ums.Read (readData, 0, length), "#4");
			VerifyTestData ("#5", readData, 0, length);
			Assert.AreEqual (length, ums.Position, "#6");
			
			//Seek to mid of the stream and read till end
			ums.Seek ((length / 2), SeekOrigin.Begin);
			ums.Read (readData, 0, (length / 2));
			VerifyTestData ("#7", readData, (length / 2), (length / 2));
			Assert.AreEqual (length, ums.Position, "#8");
			ums.Close ();
		}
		public void CanWrite ()
		{
			UnmanagedMemoryStream ums = new 
				UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
			Assert.IsTrue (ums.CanWrite, "#1");
			ums.Seek (length, SeekOrigin.Begin);
			Assert.IsTrue (ums.CanWrite, "#2");
			ums.Seek (capacity, SeekOrigin.Begin);
			Assert.IsTrue (ums.CanWrite, "#3");
			ums.Seek (capacity + 1, SeekOrigin.Begin);
			Assert.IsTrue (ums.CanWrite, "#4");
			ums.Seek (0, SeekOrigin.Begin);
			Assert.IsTrue (ums.CanWrite, "#5");
			ums.Close ();
		}
예제 #14
0
		internal UnmanagedMemoryStream ResourceValueAsStream (string name, int index)
		{
			ResourceInfo ri = infos [index];
			if ((PredefinedResourceType)ri.TypeIndex != PredefinedResourceType.Stream)
				throw new InvalidOperationException (String.Format ("Resource '{0}' was not a Stream. Use GetObject() instead.", name));
			
			lock (readerLock) {
				reader.BaseStream.Seek (ri.ValuePosition, SeekOrigin.Begin);
				
				// here we return a Stream from exactly
				// current position so that the returned
				// Stream represents a single object stream.
				long slen = reader.ReadInt32();
				UnmanagedMemoryStream basePtrStream = reader.BaseStream as UnmanagedMemoryStream;
				unsafe {
					if (basePtrStream != null) {
						return new UnmanagedMemoryStream (basePtrStream.PositionPointer, slen);
					} else {
						IntPtr ptr = Marshal.AllocHGlobal ((int) slen);
						byte* addr = (byte*) ptr.ToPointer ();
						UnmanagedMemoryStream ms = new UnmanagedMemoryStream (addr, slen, slen, FileAccess.ReadWrite);
						// The memory resource must be freed
						// when the stream is disposed.
						ms.Closed += delegate (object o, EventArgs e) {
							Marshal.FreeHGlobal (ptr);
						};

						byte [] bytes = new byte [slen < 1024 ? slen : 1024];
						while (slen > 0 ) {
							int x = reader.Read (bytes, 0, (int)Math.Min (bytes.Length, slen));

							if (x == 0)
								throw new FormatException ("The resource data is corrupt. Resource stream ended");

							ms.Write (bytes, 0, x);
							slen -= x;
						}
						ms.Seek (0, SeekOrigin.Begin);
						return ms;
					}
				}
			}
		}
		public void Seek_Offset_Invalid ()
		{
			UnmanagedMemoryStream ums = new
				UnmanagedMemoryStream(mem_byteptr, length, capacity, FileAccess.ReadWrite);
			ums.Write (testStreamData, 0, testStreamData.Length);
			ums.Position = 0;
			try {
				ums.Seek(-1, SeekOrigin.Begin);
				Assert.Fail ("#A1");
			} catch (IOException ex) {
				// An attempt was made to move the position before the beginning
				// of the stream
				Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
			}

			ums.Position = 2;
			try {
				ums.Seek(-3, SeekOrigin.Current);
				Assert.Fail ("#B1");
			} catch (IOException ex) {
				// An attempt was made to move the position before the beginning
				// of the stream
				Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
			}

			ums.Close();
		}
예제 #16
0
 public override long Seek(long offset, SeekOrigin loc)
 {
     return(_unmanagedStream.Seek(offset, loc));
 }
예제 #17
0
파일: Memory.cs 프로젝트: txdv/Mono.Interop
		unsafe static public MemoryPatch Patch(IntPtr target, Action action)
		{
			int size = 8;
			var mr = GetPermissions(target);
			if (mr != null) {
				MemoryProtection(target, size, mprot.Read|mprot.Write);
				var ums = new UnmanagedMemoryStream((byte *)target, 0, size, FileAccess.ReadWrite);

				byte[] unpatched = new byte[size];
				ums.Read(unpatched, 0, size);

				ums.Seek(0, SeekOrigin.Begin);
				var writer = new X86Writer(ums, (IntPtr)size);
				writer.Mov32(X86Register32.EAX, Marshal.GetFunctionPointerForDelegate(action).ToInt32());
				writer.Jmp(X86Register32.EAX);

				ums.Seek(0, SeekOrigin.Begin);
				byte[] patched = new byte[size];
				ums.Read(patched, 0, size);

				return new MemoryPatch(target, action, patched, unpatched);
			} else {
				throw new Exception();
			}
		}