MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
BinaryFile.cs
1// Copyright (c) 2018-2020 SignalPop LLC, and contributors. All rights reserved.
2// License: Apache 2.0
3// License: https://github.com/MyCaffe/MyCaffe/blob/master/LICENSE
4// Modified from Original Source: https://github.com/MyCaffe/MyCaffe/blob/master/MyCaffe.data/MnistDataLoader.cs
5using System;
6using System.Collections.Generic;
7using System.IO;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11
15namespace MyCaffe.data
16{
20 public class BinaryFile : IDisposable
21 {
22 private FileStream _file;
23 private BinaryReader _reader;
24 private bool _disposed = false;
25
30 public BinaryFile(string strFile)
31 {
32 _file = File.Open(strFile, FileMode.Open, FileAccess.Read, FileShare.Read);
33 _reader = new BinaryReader(_file);
34 }
35
41 {
42 Dispose(false);
43 }
44
45 #region Disposable
46
51 protected virtual void Dispose(bool disposing)
52 {
53 if (_disposed)
54 return;
55
56 // dispose managed state (managed objects).
57 if (disposing)
58 {
59 _reader.Close();
60 _disposed = true;
61 }
62 }
63
67 public void Dispose()
68 {
69 Dispose(true);
70 // Suppress finalization.
71 GC.SuppressFinalize(this);
72 }
73
74 #endregion
75
80 public BinaryReader Reader
81 {
82 get { return _reader; }
83 }
84
89 public UInt32 ReadUInt32()
90 {
91 UInt32 nVal = _reader.ReadUInt32();
92
93 return swap_endian(nVal);
94 }
95
101 public byte[] ReadBytes(int nCount)
102 {
103 return _reader.ReadBytes(nCount);
104 }
105
106 private UInt32 swap_endian(UInt32 nVal)
107 {
108 nVal = ((nVal << 8) & 0xFF00FF00) | ((nVal >> 8) & 0x00FF00FF);
109 return (nVal << 16) | (nVal >> 16);
110 }
111 }
112}
The BinaryFile class is used to manage binary files used by the MNIST dataset creator.
Definition: BinaryFile.cs:21
BinaryFile(string strFile)
The constructor.
Definition: BinaryFile.cs:30
BinaryReader Reader
Returns the binary reader used.
Definition: BinaryFile.cs:81
UInt32 ReadUInt32()
Reads in a UINT32 and performs an endian swap.
Definition: BinaryFile.cs:89
byte[] ReadBytes(int nCount)
Reads bytes from the file.
Definition: BinaryFile.cs:101
void Dispose()
Release all resources.
Definition: BinaryFile.cs:67
virtual void Dispose(bool disposing)
Dispose if disposing, or ignore if already disposed.
Definition: BinaryFile.cs:51
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
Definition: BinaryFile.cs:16