Skip to content

Commit

Permalink
workaround for nanoframework/Home#1529
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzman55 committed Dec 18, 2024
1 parent 657e056 commit e17fd82
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion MakoIoT.Device.Services.FileStorage.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency id="nanoFramework.CoreLibrary" version="1.15.5" />
<dependency id="nanoFramework.Runtime.Events" version="1.11.18" />
<dependency id="nanoFramework.System.Collections" version="1.5.45" />
<dependency id="nanoFramework.System.IO.FileSystem" version="1.1.54" />
<dependency id="nanoFramework.System.IO.FileSystem" version="1.1.66" />
<dependency id="nanoFramework.System.IO.Streams" version="1.1.59" />
<dependency id="nanoFramework.System.Text" version="1.2.54" />
<dependency id="nanoFramework.DependencyInjection" version="1.1.11" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<ItemGroup>
<Compile Include="DebugLog.cs" />
<Compile Include="FileStorageServiceTest.cs" />
<Compile Include="StreamExtensionTest.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MakoIoT.Device.Services.Interface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
Expand Down Expand Up @@ -58,8 +59,8 @@
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.TestFramework.3.0.47\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
</Reference>
<Reference Include="System.IO.FileSystem, Version=1.1.54.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.1.54\lib\System.IO.FileSystem.dll</HintPath>
<Reference Include="System.IO.FileSystem">
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.1.66\lib\System.IO.FileSystem.dll</HintPath>
</Reference>
<Reference Include="System.IO.Streams, Version=1.1.59.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.59\lib\System.IO.Streams.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="nanoFramework.DependencyInjection" version="1.1.11" targetFramework="netnano1.0" />
<package id="nanoFramework.Runtime.Events" version="1.11.18" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Collections" version="1.5.45" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.FileSystem" version="1.1.54" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.FileSystem" version="1.1.66" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.Streams" version="1.1.59" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Runtime" version="1.0.27" />
<package id="nanoFramework.System.Text" version="1.2.54" targetFramework="netnano1.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="Extensions\DeviceBuilderExtension.cs" />
<Compile Include="Extensions\StreamExtension.cs" />
<Compile Include="FileStorageService.cs" />
<Compile Include="Interface\IStreamStorageService.cs" />
<Compile Include="MakoStreamReader.cs" />
Expand All @@ -46,8 +47,8 @@
<Reference Include="nanoFramework.System.Text, Version=1.2.54.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll</HintPath>
</Reference>
<Reference Include="System.IO.FileSystem, Version=1.1.54.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.1.54\lib\System.IO.FileSystem.dll</HintPath>
<Reference Include="System.IO.FileSystem">
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.1.66\lib\System.IO.FileSystem.dll</HintPath>
</Reference>
<Reference Include="System.IO.Streams, Version=1.1.59.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.59\lib\System.IO.Streams.dll</HintPath>
Expand Down
17 changes: 6 additions & 11 deletions src/MakoIoT.Device.Services.FileStorage/MakoStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections;
using System.IO;
using System.Text;
using MakoIoT.Device.Services.FileStorage.Extensions;

namespace MakoIoT.Device.Services.FileStorage
{
Expand All @@ -16,8 +17,6 @@ namespace MakoIoT.Device.Services.FileStorage
/// </summary>
public class MakoStreamReader : StreamReader
{
private long _pos = 0;

private const int c_MaxReadLineLen = 0xFFFF;
private const int c_BufferSize = 512;

Expand Down Expand Up @@ -89,8 +88,6 @@ public MakoStreamReader(Stream stream) : base(stream)
BaseStream = stream;
_decoder = CurrentEncoding.GetDecoder();
_disposed = false;

_pos = 0;
}

/// <summary>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/MakoIoT.Device.Services.FileStorage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="nanoFramework.DependencyInjection" version="1.1.11" targetFramework="netnano1.0" />
<package id="nanoFramework.Runtime.Events" version="1.11.18" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Collections" version="1.5.45" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.FileSystem" version="1.1.54" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.FileSystem" version="1.1.66" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.Streams" version="1.1.59" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Runtime" version="1.0.27" />
<package id="nanoFramework.System.Text" version="1.2.54" targetFramework="netnano1.0" />
Expand Down

0 comments on commit e17fd82

Please sign in to comment.