public unsafe void Put(byte *data, uint length) { if (length <= SmallEnoughToBuffer) { if (bufferedLength + length > buffer.Length) { Flush(); } // TODO: call a native function to perform the copy for (uint i = 0; i < length; i++) { buffer[bufferedLength + i] = data[i]; } bufferedLength += length; } else { Flush(); UInt32 written; if (false == WindowsNativeMethods.WriteFile(fileHandle, data, length, out written, IntPtr.Zero)) { throw new IOException(String.Format("WriteFile({0} bytes) failed (error={1})", length, WindowsNativeMethods.GetLastError())); } if (written != length) { throw new IOException(String.Format("Only wrote {0} out of {1}", written, length)); } } }
public void Flush() { if (bufferedLength > 0) { UInt32 written; if (false == WindowsNativeMethods.WriteFile(fileHandle, buffer, bufferedLength, out written, IntPtr.Zero)) { throw new IOException(String.Format("WriteFile({0} bytes) failed (error={1})", bufferedLength, WindowsNativeMethods.GetLastError())); } if (written != bufferedLength) { throw new IOException(String.Format("Only wrote {0} out of {1}", written, bufferedLength)); } //Console.WriteLine("Flushed {0} bytes:", bufferedLength); //Console.WriteLine("{0}", System.Text.Encoding.UTF8.GetString(buffer, 0, (int)bufferedLength)); bufferedLength = 0; } }