From 90a11179bdab069dcb46d4090d2597e95a82b356 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Tue, 3 Sep 2013 11:18:14 +0200 Subject: [PATCH] change string to byte[] in File.cs and MainModel --- CampusAppWP8/CampusAppWP8/Model/MainModel.cs | 31 ++-- .../CampusAppWP8/Pages/Exams/Exams.xaml.cs | 3 + CampusAppWP8/CampusAppWP8/Utility/File.cs | 135 ++---------------- .../CampusAppWP8/Utility/HttpRequest.cs | 7 + 4 files changed, 36 insertions(+), 140 deletions(-) diff --git a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs index 8a549d87..a88d6a99 100644 --- a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs @@ -335,7 +335,7 @@ namespace CampusAppWP8 { if (this.file != null) { - string data = this.file.ReadFile(); + byte[] data = this.file.ReadFile(); if (data == null) { @@ -343,9 +343,9 @@ namespace CampusAppWP8 } else { - if (!data.Equals(string.Empty)) + if (data.Length > 0) { - this.DeserializeModel(Encoding.UTF8.GetBytes(data)); + this.DeserializeModel(data); } this.RunOnIOCallback(this.OnLoaded); @@ -494,7 +494,7 @@ namespace CampusAppWP8 if ((this.IsFile() == true) && (fileName.Equals(string.Empty) == false)) { - this.InitFile(CampusAppWP8.Utility.File.IOTypeRead.ReadSync, CampusAppWP8.Utility.File.IOTypeWrite.WriteAsync); + this.InitFile(); } if ((this.IsHttpApi() == true) @@ -507,16 +507,12 @@ namespace CampusAppWP8 /// /// Initializes the file object. /// - /// read io type (Default: sync) - /// write io type (Default: async) - private void InitFile( - CampusAppWP8.Utility.File.IOTypeRead readType = CampusAppWP8.Utility.File.IOTypeRead.ReadSync, - CampusAppWP8.Utility.File.IOTypeWrite writeType = CampusAppWP8.Utility.File.IOTypeWrite.WriteAsync) + private void InitFile() { if ((this.IsFile() == true) && (this.file == null)) { - this.file = new CampusAppWP8.Utility.File(this.fileName, readType, writeType); + this.file = new CampusAppWP8.Utility.File(this.fileName); } } @@ -537,7 +533,7 @@ namespace CampusAppWP8 /// /// sending object /// event args - private void OnLoadDataComplete(object sender, DownloadStringCompletedEventArgs e) + private void OnLoadDataComplete(object sender, OpenReadCompletedEventArgs e) { Exception downloadError = e.Error; if (downloadError != null) @@ -546,11 +542,16 @@ namespace CampusAppWP8 } else { - string downloadResult = e.Result; - - if (downloadResult != null && !downloadResult.Equals(string.Empty)) + byte[] data; + using (MemoryStream ms = new MemoryStream()) { - this.DeserializeModel(Encoding.UTF8.GetBytes(downloadResult)); + e.Result.CopyTo(ms); + data = ms.ToArray(); + } + + if (data != null && data.Length > 0) + { + this.DeserializeModel(data); } this.RunOnIOCallback(this.OnLoaded); diff --git a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs index 1fec754a..78c5c024 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Exams/Exams.xaml.cs @@ -14,6 +14,8 @@ namespace CampusAppWP8.Pages.Exams using CampusAppWP8.Utility; using CampusAppWP8.Utility.Lui.MessageBoxes; using Microsoft.Phone.Controls; + using System.Net; + using System.IO; /// Exams. /// Stubbfel, 02.09.2013. @@ -74,6 +76,7 @@ namespace CampusAppWP8.Pages.Exams { this.SetupExamList(); this.ProgressBar.Visibility = System.Windows.Visibility.Collapsed; + HttpRequest api = new HttpRequest(); } /// Sets up the exam list. diff --git a/CampusAppWP8/CampusAppWP8/Utility/File.cs b/CampusAppWP8/CampusAppWP8/Utility/File.cs index 189a7250..61c6e1a1 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/File.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/File.cs @@ -27,27 +27,15 @@ namespace CampusAppWP8.Utility /// private string filename = string.Empty; - /// - /// Read type. - /// - private IOTypeRead readType; - - /// - /// Write type. - /// - private IOTypeWrite writeType; - /// /// Initializes a new instance of the class. /// /// file name /// read type /// write type - public File(string filename, IOTypeRead read, IOTypeWrite write) + public File(string filename) { this.filename = filename; - this.readType = (read == IOTypeRead.INVALID) ? IOTypeRead.ReadAsync : read; - this.writeType = write; } /// @@ -55,80 +43,18 @@ namespace CampusAppWP8.Utility /// public delegate void WriteCallbackFunc(); - /// - /// IO read type ENUM. - /// - public enum IOTypeRead - { - /// - /// Invalid/unset state. - /// - INVALID = 0, - - /// - /// Sync read. - /// - ReadSync = 1, - - /// - /// Async read. - /// - ReadAsync = 2 - } - - /// - /// IO write type ENUM. - /// - public enum IOTypeWrite - { - /// - /// Invalid/unset state. - /// - INVALID = 0, - - /// - /// Sync write. - /// - WriteSync = 1, - - /// - /// Async write. - /// - WriteAsync = 2, - - /// - /// Read only, no writing. - /// - ReadOnly = 3 - } - /// /// Read data from file to a string. /// /// read type /// data string - public string ReadFile(IOTypeRead ioType = IOTypeRead.INVALID) + public byte[] ReadFile() { - string retValue = null; + byte[] retValue = null; if (this.Exist() == true) { - IOTypeRead tempType = ioType; - - if (tempType == IOTypeRead.INVALID) - { - tempType = this.readType; - } - - if (tempType == IOTypeRead.ReadAsync) - { - // retValue = this.ReadAsync(); - retValue = this.ReadSync(); - } - else if (tempType == IOTypeRead.ReadSync) - { - retValue = this.ReadSync(); - } + retValue = this.ReadSync(); } return retValue; @@ -141,27 +67,8 @@ namespace CampusAppWP8.Utility /// callback function, called after writing is done. /// callback function, called when writing failed. /// write type. - public void WriteFile(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback, IOTypeWrite ioType = IOTypeWrite.INVALID) + public void WriteFile(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback) { - IOTypeWrite tempType = ioType; - - if (tempType == IOTypeWrite.INVALID) - { - tempType = this.writeType; - } - - /* - if (tempType == IOTypeWrite.WriteAsync) - { - this.WriteAsync(data, onSavedCallback, onFailedCallback); - } - else if (tempType == IOTypeWrite.WriteSync) - { - // this.WriteSync(data); - this.WriteAsync(data, onSavedCallback, onFailedCallback); - } - */ - Thread th = new Thread(delegate() { this.WriteAsync(data, onSavedCallback, onFailedCallback); }); th.Start(); } @@ -189,44 +96,22 @@ namespace CampusAppWP8.Utility /// Read data synchronous from file. /// /// data string - private string ReadSync() + private byte[] ReadSync() { - string retValue = null; + byte[] retValue = null; using (Stream fileStream = File.LocalFolder.OpenStreamForReadAsync(this.filename).Result) { - using (StreamReader streamReader = new StreamReader(fileStream)) + using (MemoryStream ms = new MemoryStream()) { - retValue = streamReader.ReadToEnd(); + fileStream.CopyTo(ms); + retValue = ms.ToArray(); } } return retValue; } - /// - /// Read data asynchronous from file. - /// - /// data string - private string ReadAsync() - { - string retValue = string.Empty; - - return retValue; - } - - /// - /// Write data synchronous to file. - /// - /// data array - /// true, if succeeded - private bool WriteSync(byte[] data) - { - bool retValue = true; - - return retValue; - } - /// /// Write data asynchronous to file. /// diff --git a/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs b/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs index 810bd29b..87ce11dc 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs @@ -60,6 +60,13 @@ namespace CampusAppWP8.Utility client.DownloadStringAsync(url); } + public void HttpGet(Uri url, Action action) + { + WebClient client = new WebClient(); + client.OpenReadCompleted += new OpenReadCompletedEventHandler(action); + client.OpenReadAsync(url); + } + /// /// Method create the Url for the http-get-method ///