Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Fixed #673
Browse files Browse the repository at this point in the history
  • Loading branch information
ar7z1 committed Jul 3, 2017
1 parent 80ae8b9 commit 9f37235
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/Accord.Core/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public static void Save<T>(this T obj, Stream stream, SerializerCompression comp
if (compression == SerializerCompression.GZip)
{
#if NET35 || NET40
using (var gzip = new GZipStream(stream, CompressionMode.Compress))
using (var gzip = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true))
#else
using (var gzip = new GZipStream(stream, CompressionLevel.Optimal))
using (var gzip = new GZipStream(stream, CompressionLevel.Optimal, leaveOpen: true))
#endif
new BinaryFormatter().Serialize(gzip, obj);
}
Expand Down Expand Up @@ -346,7 +346,7 @@ public static T Load<T>(Stream stream, BinaryFormatter formatter, SerializerComp
object obj;
if (compression == SerializerCompression.GZip)
{
using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
using (var gzip = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true))
obj = formatter.Deserialize(gzip);
}
else if (compression == SerializerCompression.None)
Expand Down
19 changes: 19 additions & 0 deletions Unit Tests/Accord.Tests.Core/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

using System;
using Accord.IO;
using Accord.Math;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;

namespace Accord.Tests
{
Expand Down Expand Up @@ -152,5 +154,22 @@ public void compression_test_override()
foreach (int k in e.Keys)
Assert.AreEqual(e[k], a[k]);
}

[Test]
public void compression_test_stream()
{
using (var stream = new MemoryStream())
{
var e = new byte[] {1, 2, 3};
Serializer.Save(e, stream, compression: SerializerCompression.GZip);

stream.Position = 0;

byte[] a;
Serializer.Load(stream, out a, compression: SerializerCompression.GZip);

CollectionAssert.AreEqual(e, a);
}
}
}
}

0 comments on commit 9f37235

Please sign in to comment.