change string to byte[] in File.cs and MainModel

This commit is contained in:
stubbfel
2013-09-03 11:18:14 +02:00
parent 557f861774
commit 90a11179bd
4 changed files with 36 additions and 140 deletions

View File

@@ -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
/// <summary>
/// Initializes the file object.
/// </summary>
/// <param name="readType">read io type (Default: sync)</param>
/// <param name="writeType">write io type (Default: async)</param>
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
/// </summary>
/// <param name="sender">sending object</param>
/// <param name="e">event args</param>
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);

View File

@@ -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;
/// <summary>Exams.</summary>
/// <remarks>Stubbfel, 02.09.2013.</remarks>
@@ -74,6 +76,7 @@ namespace CampusAppWP8.Pages.Exams
{
this.SetupExamList();
this.ProgressBar.Visibility = System.Windows.Visibility.Collapsed;
HttpRequest api = new HttpRequest();
}
/// <summary>Sets up the exam list.</summary>

View File

@@ -27,27 +27,15 @@ namespace CampusAppWP8.Utility
/// </summary>
private string filename = string.Empty;
/// <summary>
/// Read type.
/// </summary>
private IOTypeRead readType;
/// <summary>
/// Write type.
/// </summary>
private IOTypeWrite writeType;
/// <summary>
/// Initializes a new instance of the <see cref="File" /> class.
/// </summary>
/// <param name="filename">file name</param>
/// <param name="read">read type</param>
/// <param name="write">write type</param>
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;
}
/// <summary>
@@ -55,80 +43,18 @@ namespace CampusAppWP8.Utility
/// </summary>
public delegate void WriteCallbackFunc();
/// <summary>
/// IO read type ENUM.
/// </summary>
public enum IOTypeRead
{
/// <summary>
/// Invalid/unset state.
/// </summary>
INVALID = 0,
/// <summary>
/// Sync read.
/// </summary>
ReadSync = 1,
/// <summary>
/// Async read.
/// </summary>
ReadAsync = 2
}
/// <summary>
/// IO write type ENUM.
/// </summary>
public enum IOTypeWrite
{
/// <summary>
/// Invalid/unset state.
/// </summary>
INVALID = 0,
/// <summary>
/// Sync write.
/// </summary>
WriteSync = 1,
/// <summary>
/// Async write.
/// </summary>
WriteAsync = 2,
/// <summary>
/// Read only, no writing.
/// </summary>
ReadOnly = 3
}
/// <summary>
/// Read data from file to a string.
/// </summary>
/// <param name="ioType">read type</param>
/// <returns>data string</returns>
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
/// <param name="onSavedCallback">callback function, called after writing is done.</param>
/// <param name="onFailedCallback">callback function, called when writing failed.</param>
/// <param name="ioType">write type.</param>
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.
/// </summary>
/// <returns>data string</returns>
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;
}
/// <summary>
/// Read data asynchronous from file.
/// </summary>
/// <returns>data string</returns>
private string ReadAsync()
{
string retValue = string.Empty;
return retValue;
}
/// <summary>
/// Write data synchronous to file.
/// </summary>
/// <param name="data">data array</param>
/// <returns>true, if succeeded</returns>
private bool WriteSync(byte[] data)
{
bool retValue = true;
return retValue;
}
/// <summary>
/// Write data asynchronous to file.
/// </summary>

View File

@@ -60,6 +60,13 @@ namespace CampusAppWP8.Utility
client.DownloadStringAsync(url);
}
public void HttpGet(Uri url, Action<object, OpenReadCompletedEventArgs> action)
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(action);
client.OpenReadAsync(url);
}
/// <summary>
/// Method create the Url for the http-get-method
/// </summary>