public static MemoryStream DataPtrToStream(DataStreamPtr ptr) { if (ptr.Size() != 0) { byte[] buffer = new byte[ptr.Size()]; unsafe { fixed(byte *bufferPtr = &buffer[0]) { ptr.Read(bufferPtr, (uint)buffer.Length); } } MemoryStream memoryStream = new MemoryStream(buffer); return(memoryStream); } return(null); }
//See here: http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=29287&p=98241#p98241 public static MemoryStream DataPtrToStream(DataStreamPtr dataPtr) { if (dataPtr.Size() != 0) { byte[] buffer = new byte[dataPtr.Size()]; unsafe { //Get the pointer to the first element of our buffer of bytes (in C++, can just use 'buffer') fixed(byte *bufferPtr = &buffer[0]) { //Read buffer.Length amount of data into bufferPtr dataPtr.Read(bufferPtr, (uint)buffer.Length); } } MemoryStream stream = new MemoryStream(buffer); return(stream); } return(null); }
/************************************************************************/ /* read a resource completely as a byte array */ /************************************************************************/ public byte[] ReadResource(string _groupName, string _fileName) { // if the file does not exist there is no way to load it if (!CheckResourceExists(_groupName, _fileName)) { return(null); } // open resource for reading DataStreamPtr streamPtr = ResourceGroupManager.Singleton.OpenResource(_fileName, _groupName); uint length = streamPtr.Size(); uint readLength = 0; // create buffer to load resource into byte[] buffer = new byte[(int)length]; if (length != 0) { unsafe { fixed(byte *bufferPtr = &buffer[0]) { // read resource into buffer readLength = streamPtr.Read(bufferPtr, length); } } } // if reading the resource failed, just get rid of the buffer if (readLength != length) { buffer = null; } // close the resource stream streamPtr.Close(); // return buffer of loaded resource or null if failed return(buffer); }