diff --git a/source/Windows.Storage/FileIO.cs b/source/Windows.Storage/FileIO.cs index a9a011d..79a1a18 100644 --- a/source/Windows.Storage/FileIO.cs +++ b/source/Windows.Storage/FileIO.cs @@ -5,6 +5,7 @@ using System; using System.Runtime.CompilerServices; +using Windows.Storage.Streams; namespace Windows.Storage { @@ -33,10 +34,24 @@ public static class FileIO // } - // ReadBufferAsync(IStorageFile) - // { - - // } + /// + /// Reads the contents of the specified file and returns a buffer. + /// + /// The file to read. + /// + /// When this method completes, it returns an object (type ) that represents the contents of the file. + /// + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be ReadBufferAsync(IStorageFile). + /// + public static IBuffer ReadBuffer(IStorageFile file) + { + byte[] tempBuffer = null; + ReadBufferNative(file, ref tempBuffer); + ByteBuffer buffer = new ByteBuffer(tempBuffer); + return (IBuffer)buffer; + } // ReadLinesAsync(IStorageFile) // { @@ -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). /// - [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[]) - // { + /// + /// Writes data from a buffer to the specified file. + /// + /// The file that the buffer of data is written to. + /// The buffer that contains the data to write. + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be WriteBuffer(IStorageFile, IBuffer). + /// + public static void WriteBuffer(IStorageFile file, IBuffer buffer) + { + WriteBytes(file, ((ByteBuffer)buffer).Data); + } - // } + /// + /// Writes an array of bytes of data to the specified file. + /// + /// The file that the byte is written to. + /// The array of bytes to write. + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be WriteBytesAsync(IStorageFile, Byte[]). + /// + [System.Diagnostics.DebuggerStepThrough] + [MethodImpl(MethodImplOptions.InternalCall)] + public extern static void WriteBytes(IStorageFile file, Byte[] buffer); // WriteLinesAsync(IStorageFile, IIterable) // { @@ -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 + } } diff --git a/source/Windows.Storage/Windows.Storage.nfproj b/source/Windows.Storage/Windows.Storage.nfproj index 0abd0a0..1facd59 100644 --- a/source/Windows.Storage/Windows.Storage.nfproj +++ b/source/Windows.Storage/Windows.Storage.nfproj @@ -129,6 +129,7 @@ ..\packages\nanoFramework.Windows.Storage.Streams.1.0.5-preview-008\lib\Windows.Storage.Streams.dll True + True @@ -144,4 +145,4 @@ - + \ No newline at end of file diff --git a/source/Windows.Storage/Windows.Storage.previous.nugetreferenceswitcher b/source/Windows.Storage/Windows.Storage.previous.nugetreferenceswitcher new file mode 100644 index 0000000..4e2a7f7 --- /dev/null +++ b/source/Windows.Storage/Windows.Storage.previous.nugetreferenceswitcher @@ -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 diff --git a/tests/FileAccess/FileAccess.nfproj b/tests/FileAccess/FileAccess.nfproj index 9a7a229..edf42c2 100644 --- a/tests/FileAccess/FileAccess.nfproj +++ b/tests/FileAccess/FileAccess.nfproj @@ -20,6 +20,7 @@ + @@ -30,6 +31,11 @@ True True + + ..\..\source\packages\nanoFramework.Windows.Storage.Streams.1.0.5-preview-008\lib\Windows.Storage.Streams.dll + True + True + diff --git a/tests/FileAccess/FileAccess.previous.nugetreferenceswitcher b/tests/FileAccess/FileAccess.previous.nugetreferenceswitcher new file mode 100644 index 0000000..1feca74 --- /dev/null +++ b/tests/FileAccess/FileAccess.previous.nugetreferenceswitcher @@ -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 diff --git a/tests/FileAccess/Program.cs b/tests/FileAccess/Program.cs index d2b9a5c..4a49903 100644 --- a/tests/FileAccess/Program.cs +++ b/tests/FileAccess/Program.cs @@ -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); } } diff --git a/tests/FileAccess/Scenario3_WriteAndReadTextInAFile.cs b/tests/FileAccess/Scenario3_WriteAndReadTextInAFile.cs index d1566a8..3988bd0 100644 --- a/tests/FileAccess/Scenario3_WriteAndReadTextInAFile.cs +++ b/tests/FileAccess/Scenario3_WriteAndReadTextInAFile.cs @@ -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 @@ -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 { diff --git a/tests/FileAccess/Scenario4_WriteAndReadBytesInAFile.cs b/tests/FileAccess/Scenario4_WriteAndReadBytesInAFile.cs new file mode 100644 index 0000000..7b0a227 --- /dev/null +++ b/tests/FileAccess/Scenario4_WriteAndReadBytesInAFile.cs @@ -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(); + } + } + } + } +} diff --git a/tests/FileAccess/packages.config b/tests/FileAccess/packages.config index 40a6e73..8b43f8d 100644 --- a/tests/FileAccess/packages.config +++ b/tests/FileAccess/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file