|
| 1 | +//Released under the MIT License. |
| 2 | +// |
| 3 | +//Copyright (c) 2018 Ntreev Soft co., Ltd. |
| 4 | +// |
| 5 | +//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 6 | +//documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 7 | +//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +//persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 11 | +//Software. |
| 12 | +// |
| 13 | +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 14 | +//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 15 | +//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.IO; |
| 21 | +using System.Linq; |
| 22 | +using System.Runtime.InteropServices; |
| 23 | +using System.Text; |
| 24 | + |
| 25 | +namespace Ntreev.Crema.Runtime.Serialization.Binary |
| 26 | +{ |
| 27 | + public static class BinaryWriterExtension |
| 28 | + { |
| 29 | + public static long GetPosition(this BinaryWriter writer) |
| 30 | + { |
| 31 | + return writer.Seek(0, SeekOrigin.Current); |
| 32 | + } |
| 33 | + |
| 34 | + public static void SetPosition(this BinaryWriter writer, long pos) |
| 35 | + { |
| 36 | + writer.Seek((int)pos, SeekOrigin.Begin); |
| 37 | + } |
| 38 | + |
| 39 | + public static void WriteResourceString(this BinaryWriter writer, KeyValuePair<int, string> value) |
| 40 | + { |
| 41 | + var bytes = Encoding.UTF8.GetBytes(value.Value); |
| 42 | + writer.WriteValue(value.Key); |
| 43 | + writer.WriteValue(bytes.Length); |
| 44 | + writer.Write(bytes, 0, bytes.Length); |
| 45 | + } |
| 46 | + |
| 47 | + public static void WriteResourceStrings(this BinaryWriter writer, KeyValuePair<int, string>[] strings) |
| 48 | + { |
| 49 | + writer.WriteValue(strings.Length); |
| 50 | + foreach (var item in strings) |
| 51 | + { |
| 52 | + writer.WriteResourceString(item); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static void WriteArray<T>(this BinaryWriter writer, T[] values) |
| 57 | + where T : struct |
| 58 | + { |
| 59 | + foreach (T value in values) |
| 60 | + { |
| 61 | + writer.WriteValue(value); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static void WriteValue<T>(this BinaryWriter writer, T value) |
| 66 | + where T : struct |
| 67 | + { |
| 68 | + var bytes = BinaryWriterExtension.GetBytes<T>(value); |
| 69 | + writer.Write(bytes, 0, bytes.Length); |
| 70 | + } |
| 71 | + |
| 72 | + public static byte[] GetBytes<TStruct>(TStruct data) |
| 73 | + where TStruct : struct |
| 74 | + { |
| 75 | + var structSize = Marshal.SizeOf(typeof(TStruct)); |
| 76 | + var buffer = new byte[structSize]; |
| 77 | + var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); |
| 78 | + Marshal.StructureToPtr(data, handle.AddrOfPinnedObject(), false); |
| 79 | + handle.Free(); |
| 80 | + return buffer; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments