-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
workaround for nanoframework/Home#1529
- Loading branch information
Showing
8 changed files
with
64 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
TestsHardware/MakoIoT.Device.Services.FileStorage.DeviceTest/StreamExtensionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/MakoIoT.Device.Services.FileStorage/Extensions/StreamExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters