forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInt32Serializer.cs
More file actions
44 lines (37 loc) · 1.39 KB
/
Copy pathInt32Serializer.cs
File metadata and controls
44 lines (37 loc) · 1.39 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
// -----------------------------------------------------------------------
// <copyright file="Int32Serializer.cs" company="Asynkron HB">
// Copyright (C) 2015-2017 Asynkron HB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.IO;
using Wire.Internal;
namespace Wire.ValueSerializers
{
public class Int32Serializer : SessionAwareByteArrayRequiringValueSerializer<int>
{
public const byte Manifest = 8;
public const int Size = sizeof(int);
public static readonly Int32Serializer Instance = new Int32Serializer();
public Int32Serializer()
: base(Manifest, () => WriteValueImpl, () => ReadValueImpl)
{
}
public override int PreallocatedByteBufferSize => Size;
public static void WriteValueImpl(Stream stream, int i, byte[] bytes)
{
NoAllocBitConverter.GetBytes(i, bytes);
stream.Write(bytes, 0, Size);
}
public static void WriteValueImpl(Stream stream, int i, SerializerSession session)
{
var bytes = session.GetBuffer(Size);
WriteValueImpl(stream, i, bytes);
}
public static int ReadValueImpl(Stream stream, byte[] bytes)
{
stream.Read(bytes, 0, Size);
return BitConverter.ToInt32(bytes, 0);
}
}
}