Exemplo n.º 1
0
 /// <summary>
 /// Seeks in an unmanaged stream.
 /// </summary>
 protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin)
 {
     return(io.seekProc(handle, offset, origin));
 }
        public void FreeImageStreamIO()
        {
            Random rand = new Random();

            byte[] bBuffer = new byte[256];
            IntPtr buffer  = Marshal.AllocHGlobal(256);

            MemoryStream stream = new MemoryStream();

            Assert.IsNotNull(stream);
            using (fi_handle handle = new fi_handle(stream))
            {
                FreeImageIO io = FreeImageAPI.IO.FreeImageStreamIO.io;
                Assert.IsNotNull(io.readProc);
                Assert.IsNotNull(io.writeProc);
                Assert.IsNotNull(io.seekProc);
                Assert.IsNotNull(io.tellProc);

                //
                // Procs
                //

                rand.NextBytes(bBuffer);

                stream.Write(bBuffer, 0, bBuffer.Length);
                Assert.That(io.tellProc(handle) == stream.Position);
                Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0);
                Assert.That(io.tellProc(handle) == 0);
                Assert.That(io.tellProc(handle) == stream.Position);

                // Read one block
                Assert.That(io.readProc(buffer, (uint)bBuffer.Length, 1, handle) == 1);
                for (int i = 0; i < bBuffer.Length; i++)
                {
                    Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]);
                }

                Assert.That(io.tellProc(handle) == stream.Position);
                Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0);
                Assert.That(io.tellProc(handle) == stream.Position);

                // Read 1 byte block
                Assert.That(io.readProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length);
                for (int i = 0; i < bBuffer.Length; i++)
                {
                    Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]);
                }

                Assert.That(io.tellProc(handle) == stream.Position);
                Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0);
                Assert.That(io.tellProc(handle) == stream.Position);

                rand.NextBytes(bBuffer);
                for (int i = 0; i < bBuffer.Length; i++)
                {
                    Marshal.WriteByte(buffer, i, bBuffer[i]);
                }

                // Write one block

                Assert.That(io.writeProc(buffer, (uint)bBuffer.Length, 1, handle) == 1);
                for (int i = 0; i < bBuffer.Length; i++)
                {
                    Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]);
                }

                Assert.That(io.tellProc(handle) == stream.Position);

                Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0);
                Assert.That(io.tellProc(handle) == 0);

                // write 1 byte block

                Assert.That(io.writeProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length);
                for (int i = 0; i < bBuffer.Length; i++)
                {
                    Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]);
                }

                Assert.That(io.tellProc(handle) == stream.Position);

                // Seek and tell

                Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0);
                Assert.That(io.tellProc(handle) == 0);

                Assert.That(io.seekProc(handle, 127, SeekOrigin.Current) == 0);
                Assert.That(io.tellProc(handle) == 127);

                Assert.That(io.seekProc(handle, 0, SeekOrigin.End) == 0);
                Assert.That(io.tellProc(handle) == 256);

                Marshal.FreeHGlobal(buffer);
                stream.Dispose();
            }
        }