本文整理汇总了C#中System.IO.UnmanagedMemoryStream.Seek方法的典型用法代码示例。如果您正苦于以下问题:C# UnmanagedMemoryStream.Seek方法的具体用法?C# UnmanagedMemoryStream.Seek怎么用?C# UnmanagedMemoryStream.Seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.UnmanagedMemoryStream
的用法示例。
在下文中一共展示了UnmanagedMemoryStream.Seek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResourceValueAsStream
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;
}
}
}
}
示例2: WriteByte
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 ();
}
示例3: PlaySoundResource
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);
}
}
示例4: Seek_Stream_Closed
public void Seek_Stream_Closed ()
{
UnmanagedMemoryStream ums = new
UnmanagedMemoryStream(mem_byteptr, length);
ums.Close ();
ums.Seek (0, SeekOrigin.Begin);
}
示例5: Constructor1
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();
}
示例6: Seek_Begin_Overflow
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");
}
}
}
示例7: Seek_End_Overflow
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");
}
}
}
示例8: Seek_Origin_Invalid
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();
}
示例9: Seek_Offset_Invalid
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();
}
示例10: ReadByte_EndOfStream
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();
}
示例11: Seek
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");
}
示例12: Read_EndOfStream
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 ();
}
示例13: Read
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 ();
}
示例14: CanWrite
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 ();
}
示例15: TryWriteBamlResource
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;
}