Add options to control compression when writing
This commit is contained in:
parent
37f488919b
commit
5116f06c08
@ -376,13 +376,17 @@ namespace MatFileHandler.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MatCompareWithTestData(string factoryName, string testName, IMatFile actual)
|
private void CompareTestDataWithWritingOptions(
|
||||||
|
IMatFile expected,
|
||||||
|
IMatFile actual,
|
||||||
|
MatFileWriterOptions? maybeOptions)
|
||||||
{
|
{
|
||||||
var expected = GetMatTestData(factoryName)[testName];
|
|
||||||
byte[] buffer;
|
byte[] buffer;
|
||||||
using (var stream = new MemoryStream())
|
using (var stream = new MemoryStream())
|
||||||
{
|
{
|
||||||
var writer = new MatFileWriter(stream);
|
var writer = maybeOptions is MatFileWriterOptions options
|
||||||
|
? new MatFileWriter(stream, options)
|
||||||
|
: new MatFileWriter(stream);
|
||||||
writer.Write(actual);
|
writer.Write(actual);
|
||||||
buffer = stream.ToArray();
|
buffer = stream.ToArray();
|
||||||
}
|
}
|
||||||
@ -394,6 +398,20 @@ namespace MatFileHandler.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void MatCompareWithTestData(string factoryName, string testName, IMatFile actual)
|
||||||
|
{
|
||||||
|
var expected = GetMatTestData(factoryName)[testName];
|
||||||
|
CompareTestDataWithWritingOptions(expected, actual, null);
|
||||||
|
CompareTestDataWithWritingOptions(
|
||||||
|
expected,
|
||||||
|
actual,
|
||||||
|
new MatFileWriterOptions { UseCompression = CompressionUsage.Always });
|
||||||
|
CompareTestDataWithWritingOptions(
|
||||||
|
expected,
|
||||||
|
actual,
|
||||||
|
new MatFileWriterOptions { UseCompression = CompressionUsage.Never });
|
||||||
|
}
|
||||||
|
|
||||||
private ComplexOf<T>[] CreateComplexLimits<T>(T[] limits)
|
private ComplexOf<T>[] CreateComplexLimits<T>(T[] limits)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
|
20
MatFileHandler/CompressionUsage.cs
Normal file
20
MatFileHandler/CompressionUsage.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright 2017 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Describes compression usage strategy for writing files.
|
||||||
|
/// </summary>
|
||||||
|
public enum CompressionUsage
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Never use compression.
|
||||||
|
/// </summary>
|
||||||
|
Never,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Always use compression.
|
||||||
|
/// </summary>
|
||||||
|
Always,
|
||||||
|
}
|
||||||
|
}
|
@ -15,13 +15,27 @@ namespace MatFileHandler
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class MatFileWriter
|
public class MatFileWriter
|
||||||
{
|
{
|
||||||
|
private readonly MatFileWriterOptions _options;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="MatFileWriter"/> class with a stream.
|
/// Initializes a new instance of the <see cref="MatFileWriter"/> class with a stream and default options.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stream">Output stream.</param>
|
/// <param name="stream">Output stream.</param>
|
||||||
public MatFileWriter(Stream stream)
|
public MatFileWriter(Stream stream)
|
||||||
{
|
{
|
||||||
Stream = stream;
|
Stream = stream;
|
||||||
|
_options = MatFileWriterOptions.Default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MatFileWriter"/> class with a stream.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Output stream.</param>
|
||||||
|
/// <param name="options">Options to use for file writing.</param>
|
||||||
|
public MatFileWriter(Stream stream, MatFileWriterOptions options)
|
||||||
|
{
|
||||||
|
Stream = stream;
|
||||||
|
_options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Stream Stream { get; }
|
private Stream Stream { get; }
|
||||||
@ -38,7 +52,17 @@ namespace MatFileHandler
|
|||||||
WriteHeader(writer, header);
|
WriteHeader(writer, header);
|
||||||
foreach (var variable in file.Variables)
|
foreach (var variable in file.Variables)
|
||||||
{
|
{
|
||||||
WriteCompressedVariable(writer, variable);
|
switch (_options.UseCompression)
|
||||||
|
{
|
||||||
|
case CompressionUsage.Always:
|
||||||
|
WriteCompressedVariable(writer, variable);
|
||||||
|
break;
|
||||||
|
case CompressionUsage.Never:
|
||||||
|
WriteVariable(writer, variable);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
MatFileHandler/MatFileWriterOptions.cs
Normal file
23
MatFileHandler/MatFileWriterOptions.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright 2017 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Options for writing .mat files.
|
||||||
|
/// </summary>
|
||||||
|
public struct MatFileWriterOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets default options.
|
||||||
|
/// </summary>
|
||||||
|
public static MatFileWriterOptions Default => new MatFileWriterOptions
|
||||||
|
{
|
||||||
|
UseCompression = CompressionUsage.Always,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether to compress all variables when writing the file.
|
||||||
|
/// </summary>
|
||||||
|
public CompressionUsage UseCompression { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user