Skip to content

Commit 9757383

Browse files
committed
Remove complicated IList<ArraySegment<byte>> based API from BytePacker.
1 parent 318a461 commit 9757383

20 files changed

Lines changed: 834 additions & 5565 deletions

samples/Samples/Sample10_ByteArrayBased.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void SimpleBufferCase()
4747
Image = new byte[] { 1, 2, 3, 4 },
4848
Comment = "This is test object to be serialize/deserialize using MsgPack."
4949
};
50-
// Note that the packer automatically increse buffer list.
51-
using ( var bytePacker = Packer.Create( buffer, allowsBufferExpansion: false ) )
50+
// Note that the packer automatically increse buffer.
51+
using ( var bytePacker = Packer.Create( buffer ) )
5252
{
5353
serializer.PackTo( bytePacker, obj );
5454
// Note: You can get actual bytes with GetResultBytes(), but it causes array copy.
@@ -61,40 +61,6 @@ public void SimpleBufferCase()
6161
}
6262
}
6363
}
64-
65-
[Test]
66-
public void ComplexBufferListCase()
67-
{
68-
var bufferManager = new MyArrayBufferManager();
69-
70-
var context = new SerializationContext();
71-
var serializer = context.GetSerializer<PhotoEntry>();
72-
73-
var obj =
74-
new PhotoEntry
75-
{
76-
Id = 123,
77-
Title = "My photo",
78-
Date = DateTime.Now,
79-
Image = new byte[] { 1, 2, 3, 4 },
80-
Comment = "This is test object to be serialize/deserialize using MsgPack."
81-
};
82-
// Note that the packer automatically increse buffer list.
83-
using ( var bytePacker = Packer.Create( bufferManager.Buffers, startIndex: 0, startOffset: 0, allowsBufferExpansion: true ) )
84-
{
85-
serializer.PackTo( bytePacker, obj );
86-
// The ByteArrayPacker tracks buffer usage (used bytes, current index of the list and offset of the current array).
87-
// In general, you bookkeep CurrentBufferIndex and CurrentBufferOffset for next Packer.Create call's startIndex and startOffset respectively.
88-
Console.WriteLine( "Packed {0:#,0}bytes. Now buffer position is index:{1}, offset:{2}", bytePacker.BytesUsed, bytePacker.CurrentBufferIndex, bytePacker.CurrentBufferOffset );
89-
90-
using ( var byteUnpacker = Unpacker.Create( bufferManager.Buffers, startIndex: 0, startOffset: 0 ) )
91-
{
92-
var deserialized = serializer.UnpackFrom( byteUnpacker );
93-
// The ByteArrayUnpacker tracks buffer usage (used bytes, current index of the list and offset of the current array).
94-
Console.WriteLine( "Unpacked {0:#,0}bytes. Now buffer position is index:{1}, offset:{2}", byteUnpacker.BytesUsed, byteUnpacker.CurrentSourceIndex, byteUnpacker.CurrentSourceOffset );
95-
}
96-
}
97-
}
9864
}
9965

10066
public class MyArrayBufferManager

src/MsgPack.Silverlight.5/MsgPack.Silverlight.5.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,6 @@
254254
<Compile Include="..\MsgPack\MessageTypeException.cs">
255255
<Link>MessageTypeException.cs</Link>
256256
</Compile>
257-
<Compile Include="..\MsgPack\MultipleArrayBufferAllocator.cs">
258-
<Link>MultipleArrayBufferAllocator.cs</Link>
259-
</Compile>
260257
<Compile Include="..\MsgPack\Packer.cs">
261258
<Link>Packer.cs</Link>
262259
</Compile>

src/MsgPack.Silverlight.WindowsPhone/MsgPack.Silverlight.WindowsPhone.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,6 @@
293293
<Compile Include="..\MsgPack\MessageTypeException.cs">
294294
<Link>MessageTypeException.cs</Link>
295295
</Compile>
296-
<Compile Include="..\MsgPack\MultipleArrayBufferAllocator.cs">
297-
<Link>MultipleArrayBufferAllocator.cs</Link>
298-
</Compile>
299296
<Compile Include="..\MsgPack\Packer.cs">
300297
<Link>Packer.cs</Link>
301298
</Compile>

src/MsgPack.Unity.Full/MsgPack.Unity.Full.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'CodeAnalysis|AnyCPU'">
3939
<OutputPath>bin\CodeAnalysis\</OutputPath>
4040
<DefineConstants>TRACE;UNITY_IPHONE;MSGPACK_UNITY_FULL;AOT;CODE_ANALYSIS</DefineConstants>
41-
<DocumentationFile>
42-
</DocumentationFile>
41+
<DocumentationFile></DocumentationFile>
4342
<Optimize>true</Optimize>
4443
<NoWarn>3001,3002</NoWarn>
4544
<DebugType>pdbonly</DebugType>
@@ -233,9 +232,6 @@
233232
<Compile Include="..\MsgPack\MPContract.cs">
234233
<Link>MPContract.cs</Link>
235234
</Compile>
236-
<Compile Include="..\MsgPack\MultipleArrayBufferAllocator.cs">
237-
<Link>MultipleArrayBufferAllocator.cs</Link>
238-
</Compile>
239235
<Compile Include="..\MsgPack\Packer.cs">
240236
<Link>Packer.cs</Link>
241237
</Compile>

src/MsgPack.Unity/MsgPack.Unity.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,6 @@
254254
<Compile Include="..\MsgPack\MPContract.cs">
255255
<Link>MPContract.cs</Link>
256256
</Compile>
257-
<Compile Include="..\MsgPack\MultipleArrayBufferAllocator.cs">
258-
<Link>MultipleArrayBufferAllocator.cs</Link>
259-
</Compile>
260257
<Compile Include="..\MsgPack\Packer.cs">
261258
<Link>Packer.cs</Link>
262259
</Compile>

src/MsgPack.Uwp/MsgPack.Uwp.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@
296296
<Compile Include="..\MsgPack\MessageTypeException.cs">
297297
<Link>MessageTypeException.cs</Link>
298298
</Compile>
299-
<Compile Include="..\MsgPack\MultipleArrayBufferAllocator.cs">
300-
<Link>MultipleArrayBufferAllocator.cs</Link>
301-
</Compile>
302299
<Compile Include="..\MsgPack\NetStandardCompatibility.cs">
303300
<Link>NetStandardCompatibility.cs</Link>
304301
</Compile>

src/MsgPack/ByteArrayPacker.cs

Lines changed: 15 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,16 @@ public abstract class ByteArrayPacker : Packer
3939
/// <value>
4040
/// The bytes used by this instance. The initial state is <c>0</c>.
4141
/// </value>
42-
public abstract long BytesUsed { get; }
42+
public abstract int BytesUsed { get; }
4343

4444
/// <summary>
45-
/// Gets the initial index of destination buffers.
45+
/// Gets the initial offset of the destination buffer.
4646
/// </summary>
4747
/// <value>
48-
/// The initial index of destination buffers.
49-
/// This value is greater than or equal to <c>0</c>,
50-
/// and less than the <see cref="ICollection{T}.Count"/> of the result of <see cref="GetFinalBuffers()"/>.
48+
/// The initial index of the destination buffer.
49+
/// This value is greater than or equal to <c>0</c> and less than the destination buffer's length.
5150
/// </value>
52-
public abstract int InitialBufferIndex { get; }
53-
54-
/// <summary>
55-
/// Gets the current index of destination buffers.
56-
/// </summary>
57-
/// <value>
58-
/// The current index of destination buffers.
59-
/// This value is greater than or equal to <c>0</c>,
60-
/// and less than the <see cref="ICollection{T}.Count"/> of the result of <see cref="GetFinalBuffers()"/>.
61-
/// </value>
62-
public abstract int CurrentBufferIndex { get; }
63-
64-
/// <summary>
65-
/// Gets the current offset of the current destination buffer.
66-
/// </summary>
67-
/// <value>
68-
/// The current offset of the current destination buffer.
69-
/// This value is greater than or equal to <see cref="ArraySegment{T}.Offset"/> of an item of <see cref="GetFinalBuffers()"/> in index <see cref="CurrentBufferIndex"/>
70-
/// and less than the length of the <see cref="ArraySegment{T}.Array"/> of it.
71-
/// </value>
72-
public abstract int CurrentBufferOffset { get; }
51+
public abstract int InitialBufferOffset { get; }
7352

7453
/// <summary>
7554
/// Initializes a new instance of the <see cref="ByteArrayPacker"/> class.
@@ -84,79 +63,25 @@ protected ByteArrayPacker( PackerCompatibilityOptions compatibilityOptions )
8463
: base( compatibilityOptions ) { }
8564

8665
/// <summary>
87-
/// Gets the final effective (written) bytes as single array.
66+
/// Gets the final effective (written) bytes as single array segment.
8867
/// </summary>
89-
/// <returns>The final buffers as single array. Its size will be <see cref="BytesUsed"/>.</returns>
90-
/// <exception cref="InvalidOperationException">Buffer is too large for single array.</exception>
68+
/// <returns>The final buffers as single <see cref="ArraySegment{T}"/>. Its size will be <see cref="BytesUsed"/>.</returns>
9169
/// <remarks>
92-
/// If the packer was allowed re-allocation, you can get final byte array from this method.
93-
/// Otherwise, the returned buffer contains only last successful operation.
94-
/// Note that there are no guarantee about the returned array is copied from the original buffer.
70+
/// The result segment contains the array returned from <see cref="GetFinalBuffer()"/>, and reflects <see cref="InitialBufferOffset"/> and <see cref="BytesUsed"/>.
9571
/// </remarks>
96-
public byte[] GetResultBytes()
72+
public ArraySegment<byte> GetResultBytes()
9773
{
98-
var bytesUsed = this.BytesUsed;
99-
if ( bytesUsed == 0 )
100-
{
101-
return Binary.Empty;
102-
}
103-
104-
var list = this.GetFinalBuffers();
105-
switch ( list.Count )
106-
{
107-
case 0:
108-
{
109-
return Binary.Empty;
110-
}
111-
case 1:
112-
{
113-
var segment = list[ 0 ];
114-
if ( segment.Offset == 0 && segment.Count == segment.Array.Length && segment.Count == bytesUsed )
115-
{
116-
return segment.Array;
117-
}
118-
else
119-
{
120-
var result = new byte[ bytesUsed ];
121-
Buffer.BlockCopy( segment.Array, segment.Offset, result, 0, unchecked( ( int )bytesUsed) );
122-
return result;
123-
}
124-
}
125-
default:
126-
{
127-
if ( bytesUsed > Int32.MaxValue )
128-
{
129-
throw new InvalidOperationException( "Buffer is too large for single array." );
130-
}
131-
132-
var result = new byte[ bytesUsed ];
133-
var offset = 0;
134-
foreach ( var segment in list.Skip( this.InitialBufferIndex ) )
135-
{
136-
var copying = Math.Min( segment.Count, result.Length - offset );
137-
Buffer.BlockCopy( segment.Array, segment.Offset, result, offset, copying );
138-
offset += copying;
139-
140-
if( offset == result.Length )
141-
{
142-
break;
143-
}
144-
}
145-
146-
return result;
147-
}
148-
}
74+
return new ArraySegment<byte>( this.GetFinalBuffer(), this.InitialBufferOffset, this.BytesUsed );
14975
}
15076

15177
/// <summary>
152-
/// Gets the final buffers as list of byte array segments.
78+
/// Gets the final buffer which may be reallocated.
15379
/// </summary>
154-
/// <returns>The final buffers as list of byte array segments.</returns>
80+
/// <returns>The final buffer which may be reallocated.</returns>
15581
/// <remarks>
156-
/// If the packer was allowed re-allocation, you can get adjusted byte array segments list from this method.
157-
/// Otherwise, the returned buffer should be same as list passed in the constructor.
158-
/// The result buffers may contain extra bytes.
82+
/// If the packer was allowed re-allocation, you can get new byte array from this method.
83+
/// Otherwise, the returned buffer should be same as the array passed in the constructor.
15984
/// </remarks>
160-
public abstract IList<ArraySegment<byte>> GetFinalBuffers();
85+
public abstract byte[] GetFinalBuffer();
16186
}
16287
}

0 commit comments

Comments
 (0)