CefStreamReader.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 read data from a stream. The methods of this class may be
  11. /// called on any thread.
  12. /// </summary>
  13. public sealed unsafe partial class CefStreamReader
  14. {
  15. /// <summary>
  16. /// Create a new CefStreamReader object from a file.
  17. /// </summary>
  18. public static CefStreamReader 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 CefStreamReader.FromNative(
  25. cef_stream_reader_t.create_for_file(&n_fileName)
  26. );
  27. }
  28. }
  29. /// <summary>
  30. /// Create a new CefStreamReader object from data.
  31. /// </summary>
  32. public static CefStreamReader Create(void* data, long size)
  33. {
  34. return CefStreamReader.FromNative(
  35. cef_stream_reader_t.create_for_data(data, (UIntPtr)size)
  36. );
  37. }
  38. /// <summary>
  39. /// Create a new CefStreamReader object from a custom handler.
  40. /// </summary>
  41. public static CefStreamReader Create(CefReadHandler handler)
  42. {
  43. if (handler == null) throw new ArgumentNullException("handler");
  44. return CefStreamReader.FromNative(
  45. cef_stream_reader_t.create_for_handler(handler.ToNative())
  46. );
  47. }
  48. /// <summary>
  49. /// Read raw binary data.
  50. /// </summary>
  51. public int Read(byte[] buffer, int offset, int length)
  52. {
  53. if (offset < 0 || length < 0 || buffer.Length - offset < length) throw new ArgumentOutOfRangeException();
  54. fixed (byte* ptr = &buffer[offset])
  55. {
  56. return (int)cef_stream_reader_t.read(_self, ptr, (UIntPtr)1, (UIntPtr)length);
  57. }
  58. }
  59. /// <summary>
  60. /// Seek to the specified offset position. |whence| may be any one of
  61. /// SEEK_CUR, SEEK_END or SEEK_SET. Returns zero on success and non-zero on
  62. /// failure.
  63. /// </summary>
  64. public bool Seek(long offset, SeekOrigin whence)
  65. {
  66. return cef_stream_reader_t.seek(_self, offset, (int)whence) == 0;
  67. }
  68. /// <summary>
  69. /// Return the current offset position.
  70. /// </summary>
  71. public long Tell()
  72. {
  73. return cef_stream_reader_t.tell(_self);
  74. }
  75. /// <summary>
  76. /// Return non-zero if at end of file.
  77. /// </summary>
  78. public bool Eof()
  79. {
  80. return cef_stream_reader_t.eof(_self) != 0;
  81. }
  82. /// <summary>
  83. /// Returns true if this reader performs work like accessing the file system
  84. /// which may block. Used as a hint for determining the thread to access the
  85. /// reader from.
  86. /// </summary>
  87. public bool MayBlock()
  88. {
  89. return cef_stream_reader_t.may_block(_self) != 0;
  90. }
  91. }
  92. }