#region Copyright 2011-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* 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
using System.IO;
namespace CSharpTest.Net.Serialization
{
///
/// Provides numeric serializers for packed int/long values.
///
public class VariantNumberSerializer :
ISerializer,
ISerializer,
ISerializer,
ISerializer
{
/// Gets a singleton of the VariantNumberSerializer
public static readonly VariantNumberSerializer Instance = new VariantNumberSerializer();
/// Gets a typed version of the VariantNumberSerializer
public static readonly ISerializer Int32 = Instance;
/// Gets a typed version of the VariantNumberSerializer
public static readonly ISerializer UInt32 = Instance;
/// Gets a typed version of the VariantNumberSerializer
public static readonly ISerializer Int64 = Instance;
/// Gets a typed version of the VariantNumberSerializer
public static readonly ISerializer UInt64 = Instance;
#region ISerializer Members
void ISerializer.WriteTo(int value, Stream stream)
{
((ISerializer)this).WriteTo(unchecked((uint)value), stream);
}
int ISerializer.ReadFrom(Stream stream)
{
return unchecked((int)((ISerializer)this).ReadFrom(stream));
}
#endregion
#region ISerializer Members
void ISerializer.WriteTo(uint value, Stream stream)
{
unchecked
{
while (value > 0x7F)
{
stream.WriteByte((byte)(value | 0x80));
value >>= 7;
}
stream.WriteByte((byte)value);
}
}
uint ISerializer.ReadFrom(Stream stream)
{
const uint mask = 0x7f;
int last;
uint value = 0;
int shift = 0;
do
{
last = stream.ReadByte();
Check.Assert(last != -1);
value = (value & ~(mask << shift)) + ((uint)last << shift);
shift += 7;
} while ((last & 0x080) != 0);
return value;
}
#endregion
#region ISerializer Members
void ISerializer.WriteTo(long value, Stream stream)
{
((ISerializer)this).WriteTo(unchecked((ulong)value), stream);
}
long ISerializer.ReadFrom(Stream stream)
{
return unchecked((long)((ISerializer)this).ReadFrom(stream));
}
#endregion
#region ISerializer Members
/// Writes the object to the stream
void ISerializer.WriteTo(ulong value, Stream stream)
{
unchecked
{
while (value > 0x7F)
{
stream.WriteByte((byte)(value | 0x80));
value >>= 7;
}
stream.WriteByte((byte)value);
}
}
/// Reads the object from a stream
ulong ISerializer.ReadFrom(Stream stream)
{
const ulong mask = 0x7f;
int last;
ulong value = 0;
int shift = 0;
do
{
last = stream.ReadByte();
Check.Assert(last != -1);
value = (value & ~(mask << shift)) + ((ulong)last << shift);
shift += 7;
} while ((last & 0x080) != 0);
return value;
}
#endregion
}
}