forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacker.Factory.cs
More file actions
224 lines (208 loc) · 12.1 KB
/
Packer.Factory.cs
File metadata and controls
224 lines (208 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2017 FUJIWARA, Yusuke and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
using System;
using System.IO;
namespace MsgPack
{
partial class Packer
{
#region -- Stream --
/// <summary>
/// Create standard Safe <see cref="Packer"/> instance wrapping specified <see cref="Stream"/> with <see cref="DefaultCompatibilityOptions"/>.
/// </summary>
/// <param name="stream"><see cref="Stream"/> object. This stream will be closed when <see cref="Packer.Dispose(Boolean)"/> is called.</param>
/// <returns>Safe <see cref="Packer"/>. This will not be null.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <remarks>
/// You can specify any derived <see cref="Stream"/> class like FileStream, <see cref="MemoryStream"/>,
/// NetworkStream, UnmanagedMemoryStream, or so.
/// </remarks>
public static Packer Create( Stream stream )
{
return Create( stream, true );
}
/// <summary>
/// Create standard Safe <see cref="Packer"/> instance wrapping specified <see cref="Stream"/> with specified <see cref="PackerCompatibilityOptions"/>.
/// </summary>
/// <param name="stream"><see cref="Stream"/> object. This stream will be closed when <see cref="Packer.Dispose(Boolean)"/> is called.</param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <returns>Safe <see cref="Packer"/>. This will not be null.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <remarks>
/// You can specify any derived <see cref="Stream"/> class like FileStream, <see cref="MemoryStream"/>,
/// NetworkStream, UnmanagedMemoryStream, or so.
/// </remarks>
public static Packer Create( Stream stream, PackerCompatibilityOptions compatibilityOptions )
{
return Create( stream, compatibilityOptions, true );
}
/// <summary>
/// Create standard Safe <see cref="Packer"/> instance wrapping specified <see cref="Stream"/> with <see cref="DefaultCompatibilityOptions"/>.
/// </summary>
/// <param name="stream"><see cref="Stream"/> object.</param>
/// <param name="ownsStream">
/// <c>true</c> to close <paramref name="stream"/> when this instance is disposed;
/// <c>false</c>, otherwise.
/// </param>
/// <returns>Safe <see cref="Packer"/>. This will not be null.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <remarks>
/// You can specify any derived <see cref="Stream"/> class like FileStream, <see cref="MemoryStream"/>,
/// NetworkStream, UnmanagedMemoryStream, or so.
/// </remarks>
public static Packer Create( Stream stream, bool ownsStream )
{
return Create( stream, DefaultCompatibilityOptions, ownsStream );
}
/// <summary>
/// Create standard Safe <see cref="Packer"/> instance wrapping specified <see cref="Stream"/> with specified <see cref="PackerCompatibilityOptions"/>.
/// </summary>
/// <param name="stream"><see cref="Stream"/> object.</param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <param name="ownsStream">
/// <c>true</c> to close <paramref name="stream"/> when this instance is disposed;
/// <c>false</c>, otherwise.
/// </param>
/// <returns>Safe <see cref="Packer"/>. This will not be null.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <remarks>
/// You can specify any derived <see cref="Stream"/> class like FileStream, <see cref="MemoryStream"/>,
/// NetworkStream, UnmanagedMemoryStream, or so.
/// </remarks>
public static Packer Create( Stream stream, PackerCompatibilityOptions compatibilityOptions, bool ownsStream )
{
return new MessagePackStreamPacker( stream, ownsStream ? PackerUnpackerStreamOptions.SingletonOwnsStream : PackerUnpackerStreamOptions.None, compatibilityOptions );
}
/// <summary>
/// Create standard Safe <see cref="Packer"/> instance wrapping specified <see cref="Stream"/> with specified <see cref="PackerCompatibilityOptions"/>.
/// </summary>
/// <param name="stream"><see cref="Stream"/> object.</param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <param name="streamOptions"><see cref="PackerUnpackerStreamOptions"/> which specifies stream handling options.</param>
/// <returns>Safe <see cref="Packer"/>. This will not be null.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
/// <remarks>
/// You can specify any derived <see cref="Stream"/> class like FileStream, <see cref="MemoryStream"/>,
/// NetworkStream, UnmanagedMemoryStream, or so.
/// </remarks>
public static Packer Create( Stream stream, PackerCompatibilityOptions compatibilityOptions, PackerUnpackerStreamOptions streamOptions )
{
return new MessagePackStreamPacker( stream, streamOptions, compatibilityOptions );
}
#endregion -- Stream --
#region -- byte[] --
/// <summary>
/// Creates a new <see cref="ByteArrayPacker"/> from specified byte array with <see cref="DefaultCompatibilityOptions"/> allowing expansion.
/// </summary>
/// <param name="buffer">The source byte array.</param>
/// <returns><see cref="ByteArrayPacker"/> instance. This value will not be <c>null</c>.</returns>
public static ByteArrayPacker Create( byte[] buffer )
{
return Create( buffer, true, DefaultCompatibilityOptions );
}
/// <summary>
/// Creates a new <see cref="ByteArrayPacker"/> from specified byte array list with <see cref="DefaultCompatibilityOptions"/> allowing expansion.
/// </summary>
/// <param name="buffer">The source byte array.</param>
/// <param name="startOffset">The effective start offset of the <paramref name="buffer"/>.</param>
/// <returns><see cref="ByteArrayPacker"/> instance. This value will not be <c>null</c>.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="startOffset"/> is negative.
/// </exception>
/// <exception cref="ArgumentException">The array length of <paramref name="buffer"/> is too small.</exception>
public static ByteArrayPacker Create( byte[] buffer, int startOffset )
{
return Create( buffer, startOffset, true, DefaultCompatibilityOptions );
}
/// <summary>
/// Creates a new <see cref="ByteArrayPacker"/> from specified byte array list with compatibility options.
/// </summary>
/// <param name="buffer">The source byte array.</param>
/// <param name="allowsBufferExpansion">
/// If <c>true</c>, new buffer is allocated in product of the original size and golden ratio.
/// Otherwise, the buffer will never be replaced.
/// </param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <returns><see cref="ByteArrayPacker"/> instance. This value will not be <c>null</c>.</returns>
public static ByteArrayPacker Create( byte[] buffer, bool allowsBufferExpansion, PackerCompatibilityOptions compatibilityOptions )
{
return new MessagePackByteArrayPacker( buffer, 0, allowsBufferExpansion ? SingleArrayBufferAllocator.Default : FixedArrayBufferAllocator.Instance, compatibilityOptions );
}
/// <summary>
/// Creates a new <see cref="ByteArrayPacker"/> from specified byte array with compatibility options.
/// </summary>
/// <param name="buffer">The source byte array.</param>
/// <param name="startOffset">The effective start offset of the <paramref name="buffer"/>.</param>
/// <param name="allowsBufferExpansion">
/// If <c>true</c>, new buffer is allocated in product of the original size and golden ratio.
/// Otherwise, the buffer will never be replaced.
/// </param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <returns><see cref="ByteArrayPacker"/> instance. This value will not be <c>null</c>.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="startOffset"/> is negative.
/// </exception>
/// <exception cref="ArgumentException">The array length of <paramref name="buffer"/> is too small.</exception>
public static ByteArrayPacker Create( byte[] buffer, int startOffset, bool allowsBufferExpansion, PackerCompatibilityOptions compatibilityOptions )
{
return new MessagePackByteArrayPacker( buffer, startOffset, allowsBufferExpansion ? SingleArrayBufferAllocator.Default : FixedArrayBufferAllocator.Instance, compatibilityOptions );
}
/// <summary>
/// Creates a new <see cref="ByteArrayPacker"/> from specified byte array with compatibility options and custom allocator.
/// </summary>
/// <param name="buffer">The source byte array.</param>
/// <param name="allocator">
/// A delegate to allocate new byte array which has requested size at least.
/// The first argument is old buffer which contains written data and second argument is requested (required) size.
/// The delegate must return new byte array which has enough size for requested write and contains old buffer's content.
/// If the delegate returns <c>null</c>, the packer will consider it as allocation failure.
/// </param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <returns><see cref="ByteArrayPacker"/> instance. This value will not be <c>null</c>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="allocator"/> is <c>null</c>.</exception>
public static ByteArrayPacker Create( byte[] buffer, Func<byte[], int, byte[]> allocator, PackerCompatibilityOptions compatibilityOptions )
{
return Create( buffer, 0, allocator, compatibilityOptions );
}
/// <summary>
/// Creates a new <see cref="ByteArrayPacker"/> from specified byte array with compatibility options and custom allocator.
/// </summary>
/// <param name="buffer">The source byte array.</param>
/// <param name="startOffset">The effective start offset of the <paramref name="buffer"/>.</param>
/// <param name="allocator">
/// A delegate to allocate new byte array which has requested size at least.
/// The first argument is old buffer which contains written data and second argument is requested (required) size.
/// The delegate must return new byte array which has enough size for requested write and contains old buffer's content.
/// If the delegate returns <c>null</c>, the packer will consider it as allocation failure.
/// </param>
/// <param name="compatibilityOptions">A <see cref="PackerCompatibilityOptions"/> which specifies compatibility options.</param>
/// <returns><see cref="ByteArrayPacker"/> instance. This value will not be <c>null</c>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="allocator"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="startOffset"/> is negative.
/// </exception>
/// <exception cref="ArgumentException">The array length of <paramref name="buffer"/> is too small.</exception>
public static ByteArrayPacker Create( byte[] buffer, int startOffset, Func<byte[], int, byte[]> allocator, PackerCompatibilityOptions compatibilityOptions )
{
return new MessagePackByteArrayPacker( buffer, startOffset, new SingleArrayBufferAllocator( allocator ), compatibilityOptions );
}
#endregion -- byte[] --
}
}