CefStreamWriter.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using Xilium.CefGlue.Interop;
  9. /// <summary>
  10. /// Class used to write data to a stream. The methods of this class may be called
  11. /// on any thread.
  12. /// </summary>
  13. public sealed unsafe partial class CefStreamWriter
  14. {
  15. /// <summary>
  16. /// Create a new CefStreamWriter object for a file.
  17. /// </summary>
  18. public static CefStreamWriter Create(string fileName)
  19. {
  20. if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");
  21. fixed (char* fileName_str = fileName)
  22. {
  23. var n_fileName = new cef_string_t(fileName_str, fileName != null ? fileName.Length : 0);
  24. return CefStreamWriter.FromNative(
  25. cef_stream_writer_t.create_for_file(&n_fileName)
  26. );
  27. }
  28. }
  29. /// <summary>
  30. /// Create a new CefStreamWriter object for a custom handler.
  31. /// </summary>
  32. public static CefStreamWriter Create(CefWriteHandler handler)
  33. {
  34. if (handler == null) throw new ArgumentNullException("handler");
  35. return CefStreamWriter.FromNative(
  36. cef_stream_writer_t.create_for_handler(handler.ToNative())
  37. );
  38. }
  39. /// <summary>
  40. /// Write raw binary data.
  41. /// </summary>
  42. public int Write(byte[] buffer, int offset, int length)
  43. {
  44. if (offset < 0 || length < 0 || buffer.Length - offset < length) throw new ArgumentOutOfRangeException();
  45. fixed (byte* ptr = &buffer[offset])
  46. {
  47. return (int)cef_stream_writer_t.write(_self, ptr, (UIntPtr)1, (UIntPtr)length);
  48. }
  49. }
  50. /// <summary>
  51. /// Seek to the specified offset position. |whence| may be any one of
  52. /// SEEK_CUR, SEEK_END or SEEK_SET. Returns zero on success and non-zero on
  53. /// failure.
  54. /// </summary>
  55. public bool Seek(long offset, SeekOrigin whence)
  56. {
  57. return cef_stream_writer_t.seek(_self, offset, (int)whence) == 0;
  58. }
  59. /// <summary>
  60. /// Return the current offset position.
  61. /// </summary>
  62. public long Tell()
  63. {
  64. return cef_stream_writer_t.tell(_self);
  65. }
  66. /// <summary>
  67. /// Flush the stream.
  68. /// </summary>
  69. public bool Flush()
  70. {
  71. return cef_stream_writer_t.flush(_self) == 0;
  72. }
  73. /// <summary>
  74. /// Returns true if this writer performs work like accessing the file system
  75. /// which may block. Used as a hint for determining the thread to access the
  76. /// writer from.
  77. /// </summary>
  78. public bool MayBlock()
  79. {
  80. return cef_stream_writer_t.may_block(_self) != 0;
  81. }
  82. }
  83. }