Skip to content

File reader stream #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CSMatIO.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csmatio", "src\csmatio.csproj", "{D6EB17BF-9074-484F-9950-E06169A3C39D}"
EndProject
Expand Down
6 changes: 3 additions & 3 deletions src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("csmatio")]
[assembly: AssemblyCopyright("Copyright © 2007, David A. Zier")]
[assembly: AssemblyCopyright("Copyright © 2016, Mike Williams, Copyright © 2007, David A. Zier")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -29,5 +29,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.1")]
6 changes: 3 additions & 3 deletions src/CSMatIO.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<package >
<metadata>
<id>CSMatIO</id>
<version>1.0.0</version>
<authors>Sérgio aka AlienEnginner</authors>
<version>1.0.1</version>
<authors>Sérgio aka AlienEnginner, Mike Williams</authors>
<owners />
<licenseUrl>https://raw.githubusercontent.com/AlienEngineer/CSMatIO/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/AlienEngineer/CSMatIO/wiki</projectUrl>
Expand All @@ -12,7 +12,7 @@
<releaseNotes>

</releaseNotes>
<copyright>Copyright (c) 2015 AlienEngineer</copyright>
<copyright>Copyright (c) 2016 Mike Williams, Copyright (c) 2015 AlienEngineer</copyright>
<tags>Matlab csmatio</tags>
</metadata>
<files>
Expand Down
58 changes: 31 additions & 27 deletions src/io/MatFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace csmatio.io
/// Hashtable content = mfr.Content;
/// </code>
/// </example>
/// <author>Mike Williams (<a href="https://github.com/mswillia/">mswillia</a>)</author>
/// <author>David Zier (<a href="mailto:david.zier@gmail.com">david.zier@gmail.com</a>)</author>
public class MatFileReader
{
Expand All @@ -55,7 +56,6 @@ public class MatFileReader
/// </summary>
private MatFileFilter _filter;


/// <summary>
/// Creates instance of <c>MatFileReader</c> and reads MAT-file with then name
/// <c>fileName</c>.
Expand All @@ -81,22 +81,26 @@ public MatFileReader(string fileName) :
/// <param name="fileName">The name of the MAT-file to open</param>
/// <param name="filter"><c>MatFileFilter</c></param>
public MatFileReader(string fileName, MatFileFilter filter)
: this(new FileStream(fileName, FileMode.Open, FileAccess.Read), filter)
{
}

/// <summary>
/// Creates instance of <c>MatFileReader</c> and reads MAT-file from the
/// <c>FileStream</c>.
/// </summary>
/// <remarks>
/// Results are filtered by <c>MatFileFilter</c>. Arrays that do not meet
/// filter match condition will not be available in results.
/// </remarks>
/// <param name="dataIn">The stream the MAT-stream will read</param>
/// <param name="filter"><c>MatFileFilter</c></param>
public MatFileReader(Stream dataIn, MatFileFilter filter)
{
_filter = filter;
_data = new Dictionary<string, MLArray>();

// Try to open up the file as read-only
FileStream dataIn;
try
{
dataIn = new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
catch (FileNotFoundException)
{
throw new MatlabIOException("Could not open the file '" + fileName + "' for reading!");
}

// try and read in the file until completed
// try and read in the stream until completed
try
{
ReadHeader(dataIn);
Expand All @@ -112,14 +116,14 @@ public MatFileReader(string fileName, MatFileFilter filter)
}
catch (IOException e)
{
throw new MatlabIOException("Error in reading MAT-file '" + fileName + "':\n" + e.ToString());
throw new MatlabIOException("Error in reading MAT-stream:\n" + e.ToString());
}
finally
{
dataIn.Close();
}
}

/// <summary>
/// Gets MAT-file header.
/// </summary>
Expand Down Expand Up @@ -208,7 +212,7 @@ private Stream Inflate(Stream buf, int numOfBytes)
return inflatedStream;
#endif
#if NET40 || NET45
// skip CRC (at end) and zip format (0x789C at begin)
// skip CRC (at end) and zip format (0x789C at begin)
buf.Position += 2;
numOfBytes -= 6;

Expand Down Expand Up @@ -240,7 +244,7 @@ private Stream Inflate(Stream buf, int numOfBytes)
}
while (data != -1);
}
decompressedStream.Position = 0;
decompressedStream.Position = 0;
return decompressedStream;
#endif
}
Expand Down Expand Up @@ -412,14 +416,14 @@ private MLArray ReadMatrix(Stream buf, bool isRoot)
//read real
tag = new ISMatTag(buf);
tag.ReadToByteBuffer(((MLNumericArray<double>)mlArray).RealByteBuffer,
(IByteStorageSupport)mlArray);
(IByteStorageSupport)mlArray);

// read complex
if (mlArray.IsComplex)
{
tag = new ISMatTag(buf);
tag.ReadToByteBuffer(((MLNumericArray<double>)mlArray).ImaginaryByteBuffer,
(IByteStorageSupport)mlArray);
(IByteStorageSupport)mlArray);
}
break;
case MLArray.mxSINGLE_CLASS:
Expand Down Expand Up @@ -534,29 +538,29 @@ private MLArray ReadMatrix(Stream buf, bool isRoot)
//read real
tag = new ISMatTag(buf);
tag.ReadToByteBuffer(((MLNumericArray<ulong>)mlArray).RealByteBuffer,
(IByteStorageSupport)mlArray);
(IByteStorageSupport)mlArray);

// read complex
if (mlArray.IsComplex)
{
tag = new ISMatTag(buf);
tag.ReadToByteBuffer(((MLNumericArray<ulong>)mlArray).ImaginaryByteBuffer,
(IByteStorageSupport)mlArray);
tag.ReadToByteBuffer(((MLNumericArray<ulong>)mlArray).ImaginaryByteBuffer,
(IByteStorageSupport)mlArray);
}
break;
case MLArray.mxINT64_CLASS:
mlArray = new MLInt64(name, dims, type, attributes);
//read real
tag = new ISMatTag(buf);
tag.ReadToByteBuffer(((MLNumericArray<long>)mlArray).RealByteBuffer,
(IByteStorageSupport)mlArray);
(IByteStorageSupport)mlArray);

// read complex
if (mlArray.IsComplex)
{
tag = new ISMatTag(buf);
tag.ReadToByteBuffer(((MLNumericArray<long>)mlArray).ImaginaryByteBuffer,
(IByteStorageSupport)mlArray);
tag.ReadToByteBuffer(((MLNumericArray<long>)mlArray).ImaginaryByteBuffer,
(IByteStorageSupport)mlArray);
}
break;
case MLArray.mxCHAR_CLASS:
Expand Down Expand Up @@ -773,15 +777,15 @@ public ISMatTag(Stream buf) :
///// </summary>
//public int Size
//{
// get { return (int)Buf.BaseStream.Length; }
// get { return (int)Buf.BaseStream.Length; }
//}

/// <summary>
/// Read MAT-file tag to a byte buffer.
/// </summary>
/// <param name="buff"><c>ByteBuffer</c></param>
/// <param name="storage"><c>ByteStorageSupport</c></param>
public void ReadToByteBuffer( ByteBuffer buff, IByteStorageSupport storage )
public void ReadToByteBuffer(ByteBuffer buff, IByteStorageSupport storage)
{
MatFileInputStream mfis = new MatFileInputStream(Buf, _type);
int elements = _size / SizeOf();
Expand Down