CefBinaryValue.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. // TODO: use intptr-sized implementations ?
  9. /// <summary>
  10. /// Class representing a binary value. Can be used on any process and thread.
  11. /// </summary>
  12. public sealed unsafe partial class CefBinaryValue : ICefBinaryValue
  13. {
  14. /// <summary>
  15. /// Creates a new object that is not owned by any other object. The specified
  16. /// |data| will be copied.
  17. /// </summary>
  18. public static CefBinaryValue Create(byte[] data)
  19. {
  20. if (data == null) throw new ArgumentNullException("data");
  21. fixed (byte* data_ptr = data)
  22. {
  23. var value = cef_binary_value_t.create(data_ptr, (UIntPtr)data.LongLength);
  24. return CefBinaryValue.FromNative(value);
  25. }
  26. }
  27. /// <summary>
  28. /// Returns true if this object is valid. This object may become invalid if
  29. /// the underlying data is owned by another object (e.g. list or dictionary)
  30. /// and that other object is then modified or destroyed. Do not call any other
  31. /// methods if this method returns false.
  32. /// </summary>
  33. public bool IsValid
  34. {
  35. get { return cef_binary_value_t.is_valid(_self) != 0; }
  36. }
  37. /// <summary>
  38. /// Returns true if this object is currently owned by another object.
  39. /// </summary>
  40. public bool IsOwned
  41. {
  42. get { return cef_binary_value_t.is_owned(_self) != 0; }
  43. }
  44. /// <summary>
  45. /// Returns true if this object and |that| object have the same underlying
  46. /// data.
  47. /// </summary>
  48. public bool IsSame(ICefBinaryValue that)
  49. {
  50. return cef_binary_value_t.is_same(_self, ((CefBinaryValue)that).ToNative()) != 0;
  51. }
  52. /// <summary>
  53. /// Returns true if this object and |that| object have an equivalent underlying
  54. /// value but are not necessarily the same object.
  55. /// </summary>
  56. public bool IsEqual(ICefBinaryValue that)
  57. {
  58. return cef_binary_value_t.is_equal(_self, ((CefBinaryValue)that).ToNative()) != 0;
  59. }
  60. /// <summary>
  61. /// Returns a copy of this object. The data in this object will also be copied.
  62. /// </summary>
  63. public ICefBinaryValue Copy()
  64. {
  65. var value = cef_binary_value_t.copy(_self);
  66. return CefBinaryValue.FromNative(value);
  67. }
  68. /// <summary>
  69. /// Returns the data size.
  70. /// </summary>
  71. public long Size
  72. {
  73. get { return (long)cef_binary_value_t.get_size(_self); }
  74. }
  75. /// <summary>
  76. /// Read up to |buffer_size| number of bytes into |buffer|. Reading begins at
  77. /// the specified byte |data_offset|. Returns the number of bytes read.
  78. /// </summary>
  79. public long GetData(byte[] buffer, long bufferSize, long dataOffset)
  80. {
  81. if (buffer.LongLength < dataOffset + bufferSize) throw new ArgumentOutOfRangeException("dataOffset");
  82. fixed (byte* buffer_ptr = buffer)
  83. {
  84. return (long)cef_binary_value_t.get_data(_self, buffer_ptr, (UIntPtr)bufferSize, (UIntPtr)dataOffset);
  85. }
  86. // FIXME: CefBinaryValue.GetData - allow work with buffer / etc
  87. }
  88. public byte[] ToArray()
  89. {
  90. var value = new byte[Size];
  91. var readed = GetData(value, value.Length, 0);
  92. if (readed != value.Length) throw new InvalidOperationException();
  93. return value;
  94. }
  95. }
  96. }