diff --git a/MakoIoT.Device.Services.FileStorage.nuspec b/MakoIoT.Device.Services.FileStorage.nuspec
index 27ed7fa..9f41733 100644
--- a/MakoIoT.Device.Services.FileStorage.nuspec
+++ b/MakoIoT.Device.Services.FileStorage.nuspec
@@ -20,7 +20,7 @@
-
+
diff --git a/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/MakoIoT.Device.Services.FileStorage.DeviceTest.nfproj b/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/MakoIoT.Device.Services.FileStorage.DeviceTest.nfproj
index a88636d..653bbc2 100644
--- a/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/MakoIoT.Device.Services.FileStorage.DeviceTest.nfproj
+++ b/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/MakoIoT.Device.Services.FileStorage.DeviceTest.nfproj
@@ -29,6 +29,7 @@
+
@@ -58,8 +59,8 @@
..\..\packages\nanoFramework.TestFramework.3.0.47\lib\nanoFramework.UnitTestLauncher.exe
-
- ..\..\packages\nanoFramework.System.IO.FileSystem.1.1.54\lib\System.IO.FileSystem.dll
+
+ ..\..\packages\nanoFramework.System.IO.FileSystem.1.1.66\lib\System.IO.FileSystem.dll
..\..\packages\nanoFramework.System.IO.Streams.1.1.59\lib\System.IO.Streams.dll
diff --git a/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/StreamExtensionTest.cs b/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/StreamExtensionTest.cs
new file mode 100644
index 0000000..6a38a01
--- /dev/null
+++ b/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/StreamExtensionTest.cs
@@ -0,0 +1,34 @@
+
+using System.IO;
+using System.Text;
+using MakoIoT.Device.Services.FileStorage.Extensions;
+using nanoFramework.TestFramework;
+
+namespace MakoIoT.Device.Services.FileStorage.DeviceTest
+{
+ [TestClass]
+ public class StreamExtensionTest
+ {
+ [TestMethod]
+ public void SafeGetLength_should_keep_stream_position()
+ {
+
+ File.WriteAllBytes("I:\\loremipsum.txt", Encoding.UTF8.GetBytes(Lorem));
+
+ using (var fs = new FileStream("I:\\loremipsum.txt", FileMode.Open))
+ {
+ var buffer = new byte[10];
+ //read 10 bytes
+ fs.Read(buffer, 0, 10);
+ //get position
+ Assert.AreEqual(10, fs.Position, "before getting stream length");
+ //get length
+ var length = fs.SafeGetLength();
+ //get position again
+ Assert.AreEqual(10, fs.Position, "after getting stream length");
+ }
+ }
+
+ private const string Lorem = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque lacinia pellentesque pharetra. Nulla a tortor in nunc facilisis cursus nec id felis.";
+ }
+}
diff --git a/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/packages.config b/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/packages.config
index 8eb09c5..c04d1c1 100644
--- a/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/packages.config
+++ b/TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/packages.config
@@ -5,7 +5,7 @@
-
+
diff --git a/src/MakoIoT.Device.Services.FileStorage/Extensions/StreamExtension.cs b/src/MakoIoT.Device.Services.FileStorage/Extensions/StreamExtension.cs
new file mode 100644
index 0000000..b957e0a
--- /dev/null
+++ b/src/MakoIoT.Device.Services.FileStorage/Extensions/StreamExtension.cs
@@ -0,0 +1,15 @@
+using System.IO;
+
+namespace MakoIoT.Device.Services.FileStorage.Extensions
+{
+ public static class StreamExtension
+ {
+ public static int SafeGetLength(this Stream stream)
+ {
+ var position = stream.Position;
+ var length = (int)stream.Length;
+ stream.Position = position;
+ return length;
+ }
+ }
+}
diff --git a/src/MakoIoT.Device.Services.FileStorage/MakoIoT.Device.Services.FileStorage.nfproj b/src/MakoIoT.Device.Services.FileStorage/MakoIoT.Device.Services.FileStorage.nfproj
index 5738e1a..f349923 100644
--- a/src/MakoIoT.Device.Services.FileStorage/MakoIoT.Device.Services.FileStorage.nfproj
+++ b/src/MakoIoT.Device.Services.FileStorage/MakoIoT.Device.Services.FileStorage.nfproj
@@ -20,6 +20,7 @@
+
@@ -46,8 +47,8 @@
..\..\packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll
-
- ..\..\packages\nanoFramework.System.IO.FileSystem.1.1.54\lib\System.IO.FileSystem.dll
+
+ ..\..\packages\nanoFramework.System.IO.FileSystem.1.1.66\lib\System.IO.FileSystem.dll
..\..\packages\nanoFramework.System.IO.Streams.1.1.59\lib\System.IO.Streams.dll
diff --git a/src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs b/src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs
index c3e84d6..f7a07fb 100644
--- a/src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs
+++ b/src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs
@@ -8,6 +8,7 @@
using System.Collections;
using System.IO;
using System.Text;
+using MakoIoT.Device.Services.FileStorage.Extensions;
namespace MakoIoT.Device.Services.FileStorage
{
@@ -16,8 +17,6 @@ namespace MakoIoT.Device.Services.FileStorage
///
public class MakoStreamReader : StreamReader
{
- private long _pos = 0;
-
private const int c_MaxReadLineLen = 0xFFFF;
private const int c_BufferSize = 512;
@@ -89,8 +88,6 @@ public MakoStreamReader(Stream stream) : base(stream)
BaseStream = stream;
_decoder = CurrentEncoding.GetDecoder();
_disposed = false;
-
- _pos = 0;
}
///
@@ -164,13 +161,13 @@ public override int Peek()
try
{
// retry read until response timeout expires
- while (BaseStream.Length > 0 && totRead < _buffer.Length)
+ while (BaseStream.SafeGetLength() > 0 && totRead < _buffer.Length)
{
int len = (int)(_buffer.Length - totRead);
- if (len > BaseStream.Length)
+ if (len > BaseStream.SafeGetLength())
{
- len = (int)BaseStream.Length;
+ len = (int)BaseStream.SafeGetLength();
}
len = BaseStream.Read(_buffer, totRead, len);
@@ -242,7 +239,7 @@ public override int Read()
int readCount = _buffer.Length;
// Put it to the maximum of available data and readCount
- readCount = readCount > (int)BaseStream.Length ? (int)BaseStream.Length : readCount;
+ readCount = readCount > (int)BaseStream.SafeGetLength() ? (int)BaseStream.SafeGetLength() : readCount;
if (readCount == 0)
{
@@ -433,7 +430,7 @@ public override string ReadToEnd()
private char[] ReadSeekableStream()
{
- char[] chars = new char[(int)BaseStream.Length];
+ char[] chars = new char[(int)BaseStream.SafeGetLength()];
_ = Read(chars, 0, chars.Length);
@@ -516,9 +513,7 @@ private int FillBufferAndReset(int count)
if (count > spaceLeft) count = spaceLeft;
- BaseStream.Position = _pos;
int read = BaseStream.Read(_buffer, _curBufLen, count);
- _pos += read;
if (read == 0) break;
diff --git a/src/MakoIoT.Device.Services.FileStorage/packages.config b/src/MakoIoT.Device.Services.FileStorage/packages.config
index ccdb467..713660f 100644
--- a/src/MakoIoT.Device.Services.FileStorage/packages.config
+++ b/src/MakoIoT.Device.Services.FileStorage/packages.config
@@ -5,7 +5,7 @@
-
+