Skip to content

Commit

Permalink
Extract TypeCode to BinarySubtype.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 19, 2010
1 parent e457311 commit f1c3511
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
1 change: 1 addition & 0 deletions source/MongoDB.Tests/MongoDB.Tests.csproj
Expand Up @@ -142,6 +142,7 @@
<None Include="Test-Data\tests.charreads.txt" />
<None Include="Test-Data\tests.reads.txt" />
<None Include="Test-Data\tests.smallreads.txt" />
<Compile Include="UnitTests\TestMongoRegex.cs" />
<Compile Include="UnitTests\TestOid.cs" />
<Compile Include="UnitTests\TestOidGenerator.cs" />
<Compile Include="UnitTests\Bson\TestBsonWriter.cs" />
Expand Down
12 changes: 6 additions & 6 deletions source/MongoDB.Tests/UnitTests/TestBinary.cs
Expand Up @@ -10,30 +10,30 @@ public class TestBinary
public void CanCreateBinary(){
var binary = new Binary();
Assert.IsNull(binary.Bytes);
Assert.AreEqual(Binary.TypeCode.Unknown, binary.Subtype);
Assert.AreEqual(BinarySubtype.Unknown, binary.Subtype);
}

[Test]
public void CanCreateBinaryFromNull(){
var binary = new Binary(null);
Assert.IsNull(binary.Bytes);
Assert.AreEqual(Binary.TypeCode.General, binary.Subtype);
Assert.AreEqual(BinarySubtype.General, binary.Subtype);
}

[Test]
public void CanCreateBinaryFromBytes(){
var bytes = new byte[] { 10 };
var binary = new Binary(bytes);
Assert.AreEqual(bytes,binary.Bytes);
Assert.AreEqual(Binary.TypeCode.General, binary.Subtype);
Assert.AreEqual(BinarySubtype.General, binary.Subtype);
}

[Test]
public void CanCreateBinaryFromBytesAndSubtype(){
var bytes = new byte[] {10};
var binary = new Binary(bytes,Binary.TypeCode.UserDefined);
var binary = new Binary(bytes,BinarySubtype.UserDefined);
Assert.AreEqual(bytes, binary.Bytes);
Assert.AreEqual(Binary.TypeCode.UserDefined, binary.Subtype);
Assert.AreEqual(BinarySubtype.UserDefined, binary.Subtype);
}

[Test]
Expand All @@ -55,7 +55,7 @@ public class TestBinary

[Test]
public void CanBeCloned(){
var binarySource = new Binary(new byte[] {10, 20}, Binary.TypeCode.UserDefined);
var binarySource = new Binary(new byte[] {10, 20}, BinarySubtype.UserDefined);
var binaryDest = binarySource.Clone() as Binary;
Assert.IsNotNull(binaryDest);
Assert.AreEqual(binarySource.Bytes,binaryDest.Bytes);
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB.Tests/UnitTests/Util/TestJsonUtils.cs
Expand Up @@ -134,7 +134,7 @@ public class TestJsonUtils
[Test]
public void TestSerializeDocWithBinary(){
var doc = new Document(){{"b", new Binary(){Bytes = new byte[]{0,1,2,3,4},
Subtype = Binary.TypeCode.General}}};
Subtype = BinarySubtype.General}}};
Assert.AreEqual(@"{ ""b"": { ""$binary"": ""AAECAwQ="", ""$type"" : 2 } }",
JsonFormatter.Serialize(doc));
}
Expand Down
25 changes: 3 additions & 22 deletions source/MongoDB/Binary.cs
Expand Up @@ -8,25 +8,6 @@ namespace MongoDB
/// </summary>
public class Binary : IEquatable<Binary>, ICloneable, IEnumerable<byte>
{
/// <summary>
/// </summary>
public enum TypeCode : byte
{
/// <summary>
/// </summary>
Unknown = 0,
/// <summary>
/// </summary>
General = 2,
// Uuid = 3 is now replaced by Guid
/// <summary>
/// </summary>
Md5 = 5,
/// <summary>
/// </summary>
UserDefined = 80
}

/// <summary>
/// Initializes a new instance of the <see cref = "Binary" /> class.
/// </summary>
Expand All @@ -41,15 +22,15 @@ public Binary()
public Binary(byte[] bytes)
{
Bytes = bytes;
Subtype = TypeCode.General;
Subtype = BinarySubtype.General;
}

/// <summary>
/// Initializes a new instance of the <see cref = "Binary" /> class.
/// </summary>
/// <param name = "bytes">The bytes.</param>
/// <param name = "subtype">The subtype.</param>
public Binary(byte[] bytes, TypeCode subtype)
public Binary(byte[] bytes, BinarySubtype subtype)
{
Bytes = bytes;
Subtype = subtype;
Expand All @@ -65,7 +46,7 @@ public Binary(byte[] bytes, TypeCode subtype)
/// Gets or sets the subtype.
/// </summary>
/// <value>The subtype.</value>
public TypeCode Subtype { get; set; }
public BinarySubtype Subtype { get; set; }

/// <summary>
/// Creates a new object that is a copy of the current instance.
Expand Down
21 changes: 21 additions & 0 deletions source/MongoDB/BinarySubtype.cs
@@ -0,0 +1,21 @@
namespace MongoDB
{
/// <summary>
/// </summary>
public enum BinarySubtype : byte
{
/// <summary>
/// </summary>
Unknown = 0,
/// <summary>
/// </summary>
General = 2,
// Uuid = 3 is now replaced by Guid
/// <summary>
/// </summary>
Md5 = 5,
/// <summary>
/// </summary>
UserDefined = 80
}
}
4 changes: 2 additions & 2 deletions source/MongoDB/Bson/BsonReader.cs
Expand Up @@ -334,7 +334,7 @@ public BsonReader(Stream stream, BsonReaderSettings settings)
Position += 4;
var subtype = _reader.ReadByte();
Position ++;
if(subtype == (byte)Binary.TypeCode.General){
if(subtype == (byte)BinarySubtype.General){
size = _reader.ReadInt32();
Position += 4;
}
Expand All @@ -348,7 +348,7 @@ public BsonReader(Stream stream, BsonReaderSettings settings)

return new Binary{
Bytes = bytes,
Subtype = (Binary.TypeCode)subtype
Subtype = (BinarySubtype)subtype
};
}

Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB/Bson/BsonWriter.cs
Expand Up @@ -143,7 +143,7 @@ private void Write(Oid id)
/// <param name = "binary">The binary.</param>
private void Write(Binary binary)
{
if(binary.Subtype == Binary.TypeCode.General)
if(binary.Subtype == BinarySubtype.General)
{
_writer.Write(binary.Bytes.Length + 4);
_writer.Write((byte)binary.Subtype);
Expand Down Expand Up @@ -421,7 +421,7 @@ public int CalculateSize(Binary binary)
{
var size = 4; //size int
size += 1; //subtype
if(binary.Subtype == Binary.TypeCode.General)
if(binary.Subtype == BinarySubtype.General)
size += 4; //embedded size int
size += binary.Bytes.Length;
return size;
Expand Down

0 comments on commit f1c3511

Please sign in to comment.