Skip to content

Commit

Permalink
Added support for byte and sbyte serialization.
Browse files Browse the repository at this point in the history
Byte and SByte were serialized using the BinaryFormatter (this was an oversight). Now they really take up only one byte. Byte values serialized previously will only be changed to the new mode after they are loaded then stored again.
  • Loading branch information
enyim committed Jul 13, 2011
1 parent 46b38d2 commit 0b7294f
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Enyim.Caching/Memcached/Transcoders/DefaultTranscoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Enyim.Caching.Memcached
/// </summary>
public class DefaultTranscoder : ITranscoder
{
private const ushort RawDataFlag = 0xfa52;
public const uint RawDataFlag = 0xfa52;
private static readonly ArraySegment<byte> NullArray = new ArraySegment<byte>(new byte[0]);

CacheItem ITranscoder.Serialize(object value)
Expand Down Expand Up @@ -50,6 +50,8 @@ protected virtual CacheItem Serialize(object value)
case TypeCode.DBNull: data = this.SerializeNull(); break;
case TypeCode.String: data = this.SerializeString((String)value); break;
case TypeCode.Boolean: data = this.SerializeBoolean((Boolean)value); break;
case TypeCode.SByte: data = this.SerializeSByte((SByte)value); break;
case TypeCode.Byte: data = this.SerializeByte((Byte)value); break;
case TypeCode.Int16: data = this.SerializeInt16((Int16)value); break;
case TypeCode.Int32: data = this.SerializeInt32((Int32)value); break;
case TypeCode.Int64: data = this.SerializeInt64((Int64)value); break;
Expand All @@ -63,7 +65,17 @@ protected virtual CacheItem Serialize(object value)
default: data = this.SerializeObject(value); break;
}

return new CacheItem((ushort)((ushort)code | 0x0100), data);
return new CacheItem(TypeCodeToFlag(code), data);
}

public static uint TypeCodeToFlag(TypeCode code)
{
return (uint)((int)code | 0x0100);
}

public static bool IsFlagHandled(uint flag)
{
return (flag & 0x100) == 0x100;
}

protected virtual object Deserialize(CacheItem item)
Expand All @@ -86,7 +98,7 @@ protected virtual object Deserialize(CacheItem item)
return retval;
}

var code = (TypeCode)(item.Flags & 0x00ff);
var code = (TypeCode)(item.Flags & 0xff);

var data = item.Data;

Expand Down Expand Up @@ -135,6 +147,16 @@ protected virtual ArraySegment<byte> SerializeString(string value)
return new ArraySegment<byte>(Encoding.UTF8.GetBytes((string)value));
}

protected virtual ArraySegment<byte> SerializeByte(byte value)
{
return new ArraySegment<byte>(new byte[] { value });
}

protected virtual ArraySegment<byte> SerializeSByte(sbyte value)
{
return new ArraySegment<byte>(new byte[] { (byte)value });
}

protected virtual ArraySegment<byte> SerializeBoolean(bool value)
{
return new ArraySegment<byte>(BitConverter.GetBytes(value));
Expand Down

0 comments on commit 0b7294f

Please sign in to comment.