Skip to content

Commit

Permalink
Better handling of new stream api for Memos
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtule committed Mar 27, 2017
1 parent a1aaefc commit bdb7b5b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
14 changes: 10 additions & 4 deletions DotNetDBF/DBFReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public DBFField[] GetSelectFields()

#if NET35

[Obsolete("Will need to open your own stream in later versions of .Net Framework")]
public DBFReader(string anIn)
{
try
Expand Down Expand Up @@ -152,6 +153,7 @@ public void Dispose()
#endregion

#if NET35
[Obsolete("Will need to open your own stream and use DataMemo property in later versions of .Net Framework")]
public string DataMemoLoc
{
get { return _dataMemoLoc; }
Expand All @@ -161,14 +163,14 @@ public string DataMemoLoc

public delegate Stream LazyStream();

private Stream loadedStream;
private Stream _loadedStream;
private LazyStream GetLazyStreamFromLocation()
{
#if NET35
if (_dataMemo == null && !String.IsNullOrEmpty(_dataMemoLoc))
{
return () => loadedStream ??
(loadedStream = File.Open(_dataMemoLoc, FileMode.Open, FileAccess.Read,
return () => _loadedStream ??
(_loadedStream = File.Open(_dataMemoLoc, FileMode.Open, FileAccess.Read,
FileShare.Read));
}else
#endif
Expand Down Expand Up @@ -206,9 +208,13 @@ public override String ToString()
public void Close()
{
#if NET35


_loadedStream?.Close();
_dataMemo?.Close();
_dataInputStream.Close();
#else

_dataMemo?.Dispose();
_dataInputStream.Dispose();
#endif

Expand Down
2 changes: 2 additions & 0 deletions DotNetDBF/DBFWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ public void Close()
raf.WriteByte(DBFFieldType.EndOfData);
#if NET35
raf.Close();
_dataMemo?.Close();
#else
raf.Dispose();
_dataMemo?.Dispose();
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions DotNetDBF/DotNetDBF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<TargetFrameworks>net35;netstandard1.3</TargetFrameworks>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>sn.snk</AssemblyOriginatorKeyFile>
<Version>5.0.0.0</Version>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<FileVersion>5.0.0.0</FileVersion>
<Version>5.0.2.0</Version>
<AssemblyVersion>5.0.2.0</AssemblyVersion>
<FileVersion>5.0.2.0</FileVersion>
<Company>Ekon Benefits</Company>
<Copyright>Copyright 2009-2017</Copyright>
<Description>This is a basic file parser for reading and writing xBase DBF files particularlly Clipper. Code originally derived from javadbf.</Description>
Expand Down
18 changes: 13 additions & 5 deletions DotNetDBF/MemoValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ internal MemoValue(long block, DBFBase aBase, string fileLoc, DBFReader.LazyStre
_base = aBase;
_fileLoc = fileLoc;
_fileStream = fileStream;
_lockName = $"DotNetDBF.Memo.read.{_fileLoc?? Guid.NewGuid().ToString() }.{_block}.";
if (String.IsNullOrEmpty(fileLoc))
{
_lockName = fileStream();
}
else
{
_lockName = $"DotNetDBF.Memo.read.{_fileLoc}";
}
}

private readonly DBFBase _base;
private readonly string _lockName;
private readonly object _lockName;
private long _block;
private readonly string _fileLoc;
private string _value;
Expand Down Expand Up @@ -95,10 +102,11 @@ public string Value
if (!_new && !_loaded)
{
var fileStream = _fileStream();
using (var reader = new BinaryReader(fileStream))

var reader = new BinaryReader(fileStream);

{
reader.BaseStream.Seek(_block * _base.BlockSize, SeekOrigin.Begin);
string tString;
var tStringBuilder = new StringBuilder();
int tIndex;
var tSoftReturn = _base.CharEncoding.GetString(new byte[] {0x8d, 0x0a});
Expand All @@ -107,7 +115,7 @@ public string Value
{
var tData = reader.ReadBytes(_base.BlockSize);

tString = _base.CharEncoding.GetString(tData);
var tString = _base.CharEncoding.GetString(tData);
tIndex = tString.IndexOf(MemoTerminator, StringComparison.Ordinal);
if (tIndex != -1)
tString = tString.Substring(0, tIndex);
Expand Down

0 comments on commit bdb7b5b

Please sign in to comment.