Skip to content

Commit

Permalink
Work on FileIO class (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Mar 6, 2019
1 parent a127507 commit 7bbe559
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 18 deletions.
77 changes: 63 additions & 14 deletions source/Windows.Storage/FileIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Runtime.CompilerServices;
using Windows.Storage.Streams;

namespace Windows.Storage
{
Expand Down Expand Up @@ -33,10 +34,24 @@ public static class FileIO

// }

// ReadBufferAsync(IStorageFile)
// {

// }
/// <summary>
/// Reads the contents of the specified file and returns a buffer.
/// </summary>
/// <param name="file">The file to read.</param>
/// <returns>
/// When this method completes, it returns an object (type <see cref="IBuffer"/>) that represents the contents of the file.
/// </returns>
///<remarks>
/// This method is exclusive of nanoFramework and it's not available in the UWP API.
/// The equivalent method would be ReadBufferAsync(IStorageFile).
///</remarks>
public static IBuffer ReadBuffer(IStorageFile file)
{
byte[] tempBuffer = null;
ReadBufferNative(file, ref tempBuffer);
ByteBuffer buffer = new ByteBuffer(tempBuffer);
return (IBuffer)buffer;
}

// ReadLinesAsync(IStorageFile)
// {
Expand All @@ -61,24 +76,46 @@ public static class FileIO
/// This method is exclusive of nanoFramework and it's not available in the UWP API.
/// The equivalent method would be ReadTextAsync(IStorageFile).
///</remarks>
[System.Diagnostics.DebuggerStepThrough]
[MethodImpl(MethodImplOptions.InternalCall)]
public extern static String ReadText(IStorageFile file);
public static String ReadText(IStorageFile file)
{
String text = null;

// ReadTextAsync(IStorageFile, UnicodeEncoding)
// {
ReadTextNative(file, ref text);

// }
return text;
}

// WriteBufferAsync(IStorageFile, IBuffer)
// ReadTextAsync(IStorageFile, UnicodeEncoding)
// {

// }

// WriteBytesAsync(IStorageFile, Byte[])
// {
/// <summary>
/// Writes data from a buffer to the specified file.
/// </summary>
/// <param name="file">The file that the buffer of data is written to.</param>
/// <param name="buffer">The buffer that contains the data to write.</param>
///<remarks>
/// This method is exclusive of nanoFramework and it's not available in the UWP API.
/// The equivalent method would be WriteBuffer(IStorageFile, IBuffer).
///</remarks>
public static void WriteBuffer(IStorageFile file, IBuffer buffer)
{
WriteBytes(file, ((ByteBuffer)buffer).Data);
}

// }
/// <summary>
/// Writes an array of bytes of data to the specified file.
/// </summary>
/// <param name="file">The file that the byte is written to.</param>
/// <param name="buffer">The array of bytes to write.</param>
///<remarks>
/// This method is exclusive of nanoFramework and it's not available in the UWP API.
/// The equivalent method would be WriteBytesAsync(IStorageFile, Byte[]).
///</remarks>
[System.Diagnostics.DebuggerStepThrough]
[MethodImpl(MethodImplOptions.InternalCall)]
public extern static void WriteBytes(IStorageFile file, Byte[] buffer);

// WriteLinesAsync(IStorageFile, IIterable<String>)
// {
Expand Down Expand Up @@ -112,5 +149,17 @@ public static class FileIO
// {

// }

#region Native calls

[System.Diagnostics.DebuggerStepThrough]
[MethodImpl(MethodImplOptions.InternalCall)]
private extern static void ReadBufferNative(IStorageFile file, ref byte[] buffer);
[System.Diagnostics.DebuggerStepThrough]

[MethodImpl(MethodImplOptions.InternalCall)]
public extern static void ReadTextNative(IStorageFile file, ref string text);
#endregion

}
}
3 changes: 2 additions & 1 deletion source/Windows.Storage/Windows.Storage.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Reference Include="Windows.Storage.Streams, Version=1.0.5.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.Windows.Storage.Streams.1.0.5-preview-008\lib\Windows.Storage.Streams.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
Expand All @@ -144,4 +145,4 @@
</PropertyGroup>
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.0.6-beta\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.0.6-beta\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Windows.Storage.Streams ../../../nflib-Windows.Storage.Streams/source/Windows.Storage.Streams/Windows.Storage.Streams.nfproj ../packages/nanoFramework.Windows.Storage.Streams.1.0.5-preview-006/lib/Windows.Storage.Streams.dll
6 changes: 6 additions & 0 deletions tests/FileAccess/FileAccess.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scenario4_WriteAndReadBytesInAFile.cs" />
<Compile Include="Scenario3_WriteAndReadTextInAFile.cs" />
<Compile Include="Scenario2_CreateAFileInRemovableStorage.cs" />
<Compile Include="Scenario1_CreateAFolderInRemovableStorage.cs" />
Expand All @@ -30,6 +31,11 @@
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="Windows.Storage.Streams, Version=1.0.5.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\source\packages\nanoFramework.Windows.Storage.Streams.1.0.5-preview-008\lib\Windows.Storage.Streams.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Windows.Storage.Streams ../../../nflib-Windows.Storage.Streams/source/Windows.Storage.Streams/Windows.Storage.Streams.nfproj ../../source/packages/nanoFramework.Windows.Storage.Streams.1.0.5-preview-006/lib/Windows.Storage.Streams.dll
3 changes: 3 additions & 0 deletions tests/FileAccess/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static void Main()
// write text and read to/from a file
Scenario3_WriteAndReadTextInAFile.Execute();

// write bytes and read to/from a file
Scenario4_WriteAndReadBytesInAFile.Execute();

Thread.Sleep(Timeout.Infinite);
}
}
Expand Down
16 changes: 13 additions & 3 deletions tests/FileAccess/Scenario3_WriteAndReadTextInAFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Scenario3_WriteAndReadTextInAFile
{
public static void Execute()
{
string textFromFile = null;

// Get the logical root folder for all removable storage devices
// in nanoFramework the drive letters are fixed, being:
// D: SD Card
Expand Down Expand Up @@ -43,14 +45,22 @@ public static void Execute()
try
{
// read text from the file
var textFromFile = FileIO.ReadText(myFile);
textFromFile = FileIO.ReadText(myFile);
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: write operation on file failed.");
Console.WriteLine($"ERROR: read operation on file failed.");
}


// compare
if(textContent.GetHashCode() == textFromFile.GetHashCode())
{
Console.WriteLine($"OK: read text matches written text.");
}
else
{
Console.WriteLine($"ERROR: read text does not match written text.");
}
}
else
{
Expand Down
69 changes: 69 additions & 0 deletions tests/FileAccess/Scenario4_WriteAndReadBytesInAFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Copyright (c) 2019 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

using System;
using Windows.Storage;
using Windows.Storage.Streams;

namespace FileAccess
{
public class Scenario4_WriteAndReadBytesInAFile
{
public static void Execute()
{
string textFromFile = null;

// Get the logical root folder for all removable storage devices
// in nanoFramework the drive letters are fixed, being:
// D: SD Card
// E: USB Mass Storage Device
StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;

// list all removable storage devices
var removableDevices = externalDevices.GetFolders();

if (removableDevices.Length > 0)
{
// create a file
var myFile = removableDevices[0].CreateFile("data-file-with-content.bin", CreationCollisionOption.ReplaceExisting);

Console.WriteLine($"OK: Successfully created file: {myFile.Path}");

string userContent = "this is a string to be saved as binary data";

IBuffer writeBuffer = GetBufferFromString(userContent);
FileIO.WriteBuffer(myFile, writeBuffer);

Console.WriteLine($"The following { writeBuffer.Length } bytes of text were written to '{myFile.Name}':\r\n{ userContent }");

IBuffer readBuffer = FileIO.ReadBuffer(myFile);
using (DataReader dataReader = DataReader.FromBuffer(readBuffer))
{
string fileContent = dataReader.ReadString(readBuffer.Length);

Console.WriteLine($"The following {readBuffer.Length} bytes of text were read from '{myFile.Name}':\r\n{ fileContent }");
}
}
else
{
// there is no removable device present
Console.WriteLine($"ERROR: Can't create file. There is no removable device present.");
}
}

private static IBuffer GetBufferFromString(String str)
{
using (InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream())
{
using (DataWriter dataWriter = new DataWriter(memoryStream))
{
dataWriter.WriteString(str);

return dataWriter.DetachBuffer();
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/FileAccess/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.1.1" targetFramework="netnanoframework10" />
<package id="nanoFramework.Windows.Storage.Streams" version="1.0.5-preview-008" targetFramework="netnanoframework10" />
</packages>

0 comments on commit 7bbe559

Please sign in to comment.