From bdb7b5b82aad9b8dfdf6b9bfb8147528426a6dc9 Mon Sep 17 00:00:00 2001 From: jbtule Date: Mon, 27 Mar 2017 13:28:51 -0500 Subject: [PATCH] Better handling of new stream api for Memos --- DotNetDBF/DBFReader.cs | 14 ++++++++++---- DotNetDBF/DBFWriter.cs | 2 ++ DotNetDBF/DotNetDBF.csproj | 6 +++--- DotNetDBF/MemoValue.cs | 18 +++++++++++++----- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/DotNetDBF/DBFReader.cs b/DotNetDBF/DBFReader.cs index 75b3b20..d65707e 100644 --- a/DotNetDBF/DBFReader.cs +++ b/DotNetDBF/DBFReader.cs @@ -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 @@ -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; } @@ -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 @@ -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 diff --git a/DotNetDBF/DBFWriter.cs b/DotNetDBF/DBFWriter.cs index 2372c59..58f9519 100644 --- a/DotNetDBF/DBFWriter.cs +++ b/DotNetDBF/DBFWriter.cs @@ -367,8 +367,10 @@ public void Close() raf.WriteByte(DBFFieldType.EndOfData); #if NET35 raf.Close(); + _dataMemo?.Close(); #else raf.Dispose(); + _dataMemo?.Dispose(); #endif } diff --git a/DotNetDBF/DotNetDBF.csproj b/DotNetDBF/DotNetDBF.csproj index c79e46c..9b7e888 100644 --- a/DotNetDBF/DotNetDBF.csproj +++ b/DotNetDBF/DotNetDBF.csproj @@ -3,9 +3,9 @@ net35;netstandard1.3 True sn.snk - 5.0.0.0 - 5.0.0.0 - 5.0.0.0 + 5.0.2.0 + 5.0.2.0 + 5.0.2.0 Ekon Benefits Copyright 2009-2017 This is a basic file parser for reading and writing xBase DBF files particularlly Clipper. Code originally derived from javadbf. diff --git a/DotNetDBF/MemoValue.cs b/DotNetDBF/MemoValue.cs index d159cab..41e4ef9 100644 --- a/DotNetDBF/MemoValue.cs +++ b/DotNetDBF/MemoValue.cs @@ -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; @@ -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}); @@ -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);