up
This commit is contained in:
270
Assets/BestHTTP/PlatformSupport/IO/Directory.cs
Normal file
270
Assets/BestHTTP/PlatformSupport/IO/Directory.cs
Normal file
@@ -0,0 +1,270 @@
|
||||
#if NETFX_CORE
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace BestHTTP.PlatformSupport.IO
|
||||
{
|
||||
public static class Directory
|
||||
{
|
||||
private static StorageFolder GetDirectoryForPath(string path)
|
||||
{
|
||||
IAsyncOperation<StorageFolder> folderFromPathAsync = StorageFolder.GetFolderFromPathAsync(path);
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFolder>(folderFromPathAsync).Wait();
|
||||
return folderFromPathAsync.GetResults();
|
||||
}
|
||||
|
||||
public static DirectoryInfo CreateDirectory(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentNullException("Path is empty");
|
||||
StorageFolder folder = (StorageFolder)null;
|
||||
path = path.Replace('/', '\\');
|
||||
string path1 = path;
|
||||
Stack<string> stack = new Stack<string>();
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
folder = Directory.GetDirectoryForPath(path1);
|
||||
break;
|
||||
}
|
||||
catch
|
||||
{
|
||||
int length = path1.LastIndexOf('\\');
|
||||
if (length < 0)
|
||||
{
|
||||
path1 = (string)null;
|
||||
}
|
||||
else
|
||||
{
|
||||
stack.Push(path1.Substring(length + 1));
|
||||
path1 = path1.Substring(0, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
while (path1 != null);
|
||||
if (path1 == null)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.CreateDirectory: Could not find any part of the path: " + path);
|
||||
throw new IOException("Could not find any part of the path: " + path);
|
||||
}
|
||||
try
|
||||
{
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
string desiredName = stack.Pop();
|
||||
if (string.IsNullOrWhiteSpace(desiredName) && stack.Count > 0)
|
||||
throw new ArgumentNullException("Empty directory name in the path");
|
||||
IAsyncOperation<StorageFolder> folderAsync = folder.CreateFolderAsync(desiredName);
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFolder>(folderAsync).Wait();
|
||||
folder = folderAsync.GetResults();
|
||||
}
|
||||
return new DirectoryInfo(path, folder);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.CreateDirectory: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.CreateDirectory: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Delete(string path)
|
||||
{
|
||||
Directory.Delete(path, false);
|
||||
}
|
||||
|
||||
public static void Delete(string path, bool recursive)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException("Path is empty");
|
||||
try
|
||||
{
|
||||
StorageFolder directoryForPath = Directory.GetDirectoryForPath(path);
|
||||
if (!recursive)
|
||||
{
|
||||
IAsyncOperation<IReadOnlyList<StorageFolder>> foldersAsync = directoryForPath.GetFoldersAsync();
|
||||
WindowsRuntimeSystemExtensions.AsTask<IReadOnlyList<StorageFolder>>(foldersAsync).Wait();
|
||||
if (Enumerable.Count<StorageFolder>((IEnumerable<StorageFolder>)foldersAsync.GetResults()) > 0)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.Delete: Directory not empty");
|
||||
throw new IOException("Directory not empty");
|
||||
}
|
||||
IAsyncOperation<IReadOnlyList<StorageFile>> filesAsync = directoryForPath.GetFilesAsync();
|
||||
WindowsRuntimeSystemExtensions.AsTask<IReadOnlyList<StorageFile>>(filesAsync).Wait();
|
||||
if (Enumerable.Count<StorageFile>((IEnumerable<StorageFile>)filesAsync.GetResults()) > 0)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.Delete: Directory not empty");
|
||||
throw new IOException("Directory not empty");
|
||||
}
|
||||
}
|
||||
WindowsRuntimeSystemExtensions.AsTask(directoryForPath.DeleteAsync()).Wait();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.Delete: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.Delete: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> EnumerateDirectories(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException("Path is empty");
|
||||
try
|
||||
{
|
||||
IAsyncOperation<IReadOnlyList<StorageFolder>> foldersAsync = Directory.GetDirectoryForPath(path).GetFoldersAsync();
|
||||
WindowsRuntimeSystemExtensions.AsTask<IReadOnlyList<StorageFolder>>(foldersAsync).Wait();
|
||||
IReadOnlyList<StorageFolder> results = foldersAsync.GetResults();
|
||||
List<string> list = new List<string>(Enumerable.Count<StorageFolder>((IEnumerable<StorageFolder>)results));
|
||||
foreach (StorageFolder storageFolder in (IEnumerable<StorageFolder>)results)
|
||||
list.Add(storageFolder.Path);
|
||||
return (IEnumerable<string>)list;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.EnumerateDirectories: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.EnumerateDirectories: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> EnumerateFiles(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException("Path is empty");
|
||||
try
|
||||
{
|
||||
IAsyncOperation<IReadOnlyList<StorageFile>> filesAsync = Directory.GetDirectoryForPath(path).GetFilesAsync();
|
||||
WindowsRuntimeSystemExtensions.AsTask<IReadOnlyList<StorageFile>>(filesAsync).Wait();
|
||||
IReadOnlyList<StorageFile> results = filesAsync.GetResults();
|
||||
List<string> list = new List<string>(Enumerable.Count<StorageFile>((IEnumerable<StorageFile>)results));
|
||||
foreach (StorageFile storageFile in (IEnumerable<StorageFile>)results)
|
||||
list.Add(storageFile.Path);
|
||||
return (IEnumerable<string>)list;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.EnumerateFiles: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.EnumerateFiles: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> EnumerateFileSystemEntries(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException("Path is empty");
|
||||
try
|
||||
{
|
||||
List<string> list = (List<string>)Directory.EnumerateDirectories(path);
|
||||
list.AddRange(Directory.EnumerateFiles(path));
|
||||
return (IEnumerable<string>)list;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.EnumerateFileSystemEntries: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.EnumerateFileSystemEntries: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Exists(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Directory.GetDirectoryForPath(path) != null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static DateTimeOffset GetFolderCreationTime(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException("Path is empty");
|
||||
try
|
||||
{
|
||||
return Directory.GetDirectoryForPath(path).DateCreated;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.GetFolderCreationTime: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("Directory.GetFolderCreationTime: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static System.DateTime GetCreationTime(string path)
|
||||
{
|
||||
return Directory.GetFolderCreationTime(path).DateTime;
|
||||
}
|
||||
|
||||
public static System.DateTime GetCreationTimeUtc(string path)
|
||||
{
|
||||
return Directory.GetFolderCreationTime(path).ToUniversalTime().DateTime;
|
||||
}
|
||||
|
||||
public static string[] GetDirectories(string path)
|
||||
{
|
||||
return Enumerable.ToArray<string>(Directory.EnumerateDirectories(path));
|
||||
}
|
||||
|
||||
public static string[] GetFiles(string path)
|
||||
{
|
||||
return Enumerable.ToArray<string>(Directory.EnumerateFiles(path));
|
||||
}
|
||||
|
||||
public static string[] GetFileSystemEntries(string path)
|
||||
{
|
||||
return Enumerable.ToArray<string>(Directory.EnumerateFileSystemEntries(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
11
Assets/BestHTTP/PlatformSupport/IO/Directory.cs.meta
Normal file
11
Assets/BestHTTP/PlatformSupport/IO/Directory.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 74f6f31c369d846db92707a1582237f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
491
Assets/BestHTTP/PlatformSupport/IO/File.cs
Normal file
491
Assets/BestHTTP/PlatformSupport/IO/File.cs
Normal file
@@ -0,0 +1,491 @@
|
||||
#if NETFX_CORE
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace BestHTTP.PlatformSupport.IO
|
||||
{
|
||||
public static class File
|
||||
{
|
||||
public static void AppendAllLines(string path, IEnumerable<string> contents)
|
||||
{
|
||||
File.AppendAllLines(path, contents, (Encoding)null);
|
||||
}
|
||||
|
||||
public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
IEnumerator<string> enumerator = contents.GetEnumerator();
|
||||
if (!enumerator.MoveNext())
|
||||
return;
|
||||
using (StreamWriter streamWriter = File.AppendText(path, encoding))
|
||||
{
|
||||
string current = enumerator.Current;
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
streamWriter.WriteLine(current);
|
||||
current = enumerator.Current;
|
||||
}
|
||||
streamWriter.Write(current);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendAllText(string path, string contents)
|
||||
{
|
||||
File.AppendAllText(path, contents, (Encoding)null);
|
||||
}
|
||||
|
||||
public static void AppendAllText(string path, string contents, Encoding encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StreamWriter streamWriter = File.AppendText(path, encoding))
|
||||
streamWriter.Write(contents);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static StreamWriter AppendText(string path, Encoding encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
IAsyncOperation<StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.OpenIfExists);
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileAsync).Wait();
|
||||
IAsyncOperation<IRandomAccessStream> source = fileAsync.GetResults().OpenAsync(FileAccessMode.ReadWrite);
|
||||
WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
|
||||
FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults();
|
||||
randomAccessStream.Seek(randomAccessStream.Size);
|
||||
return encoding == null ? new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream)) : new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream), encoding);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static Exception GetRethrowException(Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("File.GetRethrowException: " + ex.Message + "\n" + ex.StackTrace);
|
||||
if (ex.GetType() == typeof(IOException))
|
||||
return ex;
|
||||
else
|
||||
return (Exception)new IOException(ex.Message, ex);
|
||||
}
|
||||
|
||||
public static void Copy(string sourceFileName, string destFileName)
|
||||
{
|
||||
File.Copy(sourceFileName, destFileName, false);
|
||||
}
|
||||
|
||||
public static void Copy(string sourceFileName, string destFileName, bool overwrite)
|
||||
{
|
||||
try
|
||||
{
|
||||
StorageFile fileForPathOrUri = FileHelper.GetFileForPathOrURI(sourceFileName);
|
||||
if (overwrite)
|
||||
{
|
||||
StorageFile storageFile = (StorageFile)null;
|
||||
try
|
||||
{
|
||||
storageFile = FileHelper.GetFileForPathOrURI(destFileName);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
if (storageFile != null)
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri.CopyAndReplaceAsync((IStorageFile)storageFile)).Wait();
|
||||
return;
|
||||
}
|
||||
}
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileForPathOrUri.CopyAsync((IStorageFolder)FileHelper.GetFolderForPathOrURI(destFileName), Path.GetFileName(destFileName))).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static FileStream Create(string path)
|
||||
{
|
||||
return new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
|
||||
}
|
||||
|
||||
public static FileStream Create(string path, int bufferSize)
|
||||
{
|
||||
return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize);
|
||||
}
|
||||
|
||||
public static StreamWriter CreateText(string path)
|
||||
{
|
||||
return new StreamWriter((Stream)File.Create(path));
|
||||
}
|
||||
|
||||
public static void Delete(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (path.Trim() == "")
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(FileHelper.GetFileForPathOrURI(path).DeleteAsync()).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Exists(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return FileHelper.GetFileForPathOrURI(path) != null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static FileAttributes GetAttributes(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
return File.WinAttributesToSysAttributes(FileHelper.GetFileForPathOrURI(path).Attributes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new FileNotFoundException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
internal static FileAttributes WinAttributesToSysAttributes(Windows.Storage.FileAttributes atts)
|
||||
{
|
||||
FileAttributes fileAttributes = (FileAttributes)0;
|
||||
if ((Windows.Storage.FileAttributes.ReadOnly & atts) != Windows.Storage.FileAttributes.Normal)
|
||||
fileAttributes |= FileAttributes.ReadOnly;
|
||||
if ((Windows.Storage.FileAttributes.Directory & atts) != Windows.Storage.FileAttributes.Normal)
|
||||
fileAttributes |= FileAttributes.Directory;
|
||||
if ((Windows.Storage.FileAttributes.Archive & atts) != Windows.Storage.FileAttributes.Normal)
|
||||
fileAttributes |= FileAttributes.Archive;
|
||||
if ((Windows.Storage.FileAttributes.Temporary & atts) != Windows.Storage.FileAttributes.Normal)
|
||||
fileAttributes |= FileAttributes.Temporary;
|
||||
if (fileAttributes == (FileAttributes)0)
|
||||
fileAttributes = FileAttributes.Normal;
|
||||
return fileAttributes;
|
||||
}
|
||||
|
||||
public static System.DateTime GetCreationTime(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
return FileHelper.GetFileForPathOrURI(path).DateCreated.DateTime;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new FileNotFoundException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static System.DateTime GetCreationTimeUtc(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
return FileHelper.GetFileForPathOrURI(path).DateCreated.ToUniversalTime().DateTime;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new FileNotFoundException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Move(string sourceFileName, string destFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(FileHelper.GetFileForPathOrURI(sourceFileName).MoveAsync((IStorageFolder)FileHelper.GetFolderForPathOrURI(destFileName), Path.GetFileName(destFileName))).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static FileStream Open(string path, FileMode mode)
|
||||
{
|
||||
return new FileStream(path, mode);
|
||||
}
|
||||
|
||||
public static FileStream Open(string path, FileMode mode, FileAccess access)
|
||||
{
|
||||
return new FileStream(path, mode, access);
|
||||
}
|
||||
|
||||
public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
|
||||
{
|
||||
return new FileStream(path, mode, access, share);
|
||||
}
|
||||
|
||||
public static FileStream OpenRead(string path)
|
||||
{
|
||||
return File.Open(path, FileMode.Open, FileAccess.Read);
|
||||
}
|
||||
|
||||
public static StreamReader OpenText(string path)
|
||||
{
|
||||
return new StreamReader((Stream)File.OpenRead(path));
|
||||
}
|
||||
|
||||
public static FileStream OpenWrite(string path)
|
||||
{
|
||||
return File.Open(path, FileMode.Create, FileAccess.Write);
|
||||
}
|
||||
|
||||
public static byte[] ReadAllBytes(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
return FileHelper.ReadEntireFile(FileHelper.GetFileForPathOrURI(path));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] ReadAllLines(string path)
|
||||
{
|
||||
return Enumerable.ToArray<string>(File.ReadLines(path));
|
||||
}
|
||||
|
||||
public static string[] ReadAllLines(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadAllLines(path);
|
||||
}
|
||||
|
||||
public static string ReadAllText(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
IAsyncOperation<IRandomAccessStream> source = FileHelper.GetFileForPathOrURI(path).OpenAsync(FileAccessMode.Read);
|
||||
WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
|
||||
using (FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults())
|
||||
return new StreamReader(WindowsRuntimeStreamExtensions.AsStreamForRead((IInputStream)randomAccessStream)).ReadToEnd();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static string ReadAllText(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadAllText(path);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> ReadLines(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
IAsyncOperation<IRandomAccessStream> source = FileHelper.GetFileForPathOrURI(path).OpenAsync(FileAccessMode.Read);
|
||||
WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
|
||||
using (FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults())
|
||||
{
|
||||
StreamReader streamReader = new StreamReader(WindowsRuntimeStreamExtensions.AsStreamForRead((IInputStream)randomAccessStream));
|
||||
List<string> list = new List<string>();
|
||||
while (true)
|
||||
{
|
||||
string str = streamReader.ReadLine();
|
||||
if (str != null)
|
||||
list.Add(str);
|
||||
else
|
||||
break;
|
||||
}
|
||||
return (IEnumerable<string>)list;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> ReadLines(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadLines(path);
|
||||
}
|
||||
|
||||
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
|
||||
{
|
||||
if (destinationFileName == null)
|
||||
throw new ArgumentNullException();
|
||||
if (!string.IsNullOrWhiteSpace(sourceFileName) && !string.IsNullOrWhiteSpace(destinationFileName))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(destinationBackupFileName))
|
||||
{
|
||||
try
|
||||
{
|
||||
StorageFile fileForPathOrUri1 = FileHelper.GetFileForPathOrURI(sourceFileName);
|
||||
string fileName1 = Path.GetFileName(destinationFileName);
|
||||
StorageFile fileForPathOrUri2;
|
||||
if (fileName1.ToLower() == destinationFileName.ToLower())
|
||||
{
|
||||
if (Path.GetFileName(sourceFileName).ToLower() == fileName1.ToLower())
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("File.Replace: Source and destination is the same file");
|
||||
throw new IOException("Source and destination is the same file");
|
||||
}
|
||||
fileForPathOrUri2 = FileHelper.GetFileForPathOrURI(Path.Combine(Path.GetDirectoryName(fileForPathOrUri1.Path), fileName1));
|
||||
}
|
||||
else
|
||||
{
|
||||
fileForPathOrUri2 = FileHelper.GetFileForPathOrURI(destinationFileName);
|
||||
if (fileForPathOrUri1.Equals((object)fileForPathOrUri2))
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("File.Replace: Source and destination is the same file");
|
||||
throw new IOException("Source and destination is the same file");
|
||||
}
|
||||
}
|
||||
string fileName2 = Path.GetFileName(destinationBackupFileName);
|
||||
if (fileName2.ToLower() == destinationBackupFileName.ToLower())
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri2.RenameAsync(destinationBackupFileName)).Wait();
|
||||
}
|
||||
else
|
||||
{
|
||||
StorageFolder folderForPathOrUri = FileHelper.GetFolderForPathOrURI(destinationBackupFileName);
|
||||
WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri2.MoveAsync((IStorageFolder)folderForPathOrUri, fileName2)).Wait();
|
||||
}
|
||||
if (fileName1.ToLower() == destinationFileName.ToLower())
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri1.RenameAsync(destinationFileName)).Wait();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
StorageFolder folderForPathOrUri = FileHelper.GetFolderForPathOrURI(destinationFileName);
|
||||
WindowsRuntimeSystemExtensions.AsTask(fileForPathOrUri1.MoveAsync((IStorageFolder)folderForPathOrUri, fileName1)).Wait();
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new ArgumentException();
|
||||
}
|
||||
|
||||
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
|
||||
{
|
||||
File.Replace(sourceFileName, destinationFileName, destinationBackupFileName);
|
||||
}
|
||||
|
||||
private static StorageFile CreateOrReplaceFile(string path)
|
||||
{
|
||||
IAsyncOperation<StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.ReplaceExisting);
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileAsync).Wait();
|
||||
return fileAsync.GetResults();
|
||||
}
|
||||
|
||||
public static void WriteAllBytes(string path, byte[] bytes)
|
||||
{
|
||||
if (path == null || bytes == null || bytes.Length == 0)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(FileIO.WriteBytesAsync((IStorageFile)File.CreateOrReplaceFile(path), bytes)).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteAllLines(string path, IEnumerable<string> contents)
|
||||
{
|
||||
if (path == null || contents == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(FileIO.WriteLinesAsync((IStorageFile)File.CreateOrReplaceFile(path), contents)).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteAllLines(string path, string[] contents)
|
||||
{
|
||||
IEnumerable<string> contents1 = (IEnumerable<string>)contents;
|
||||
File.WriteAllLines(path, contents1);
|
||||
}
|
||||
|
||||
public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
|
||||
{
|
||||
File.WriteAllLines(path, contents);
|
||||
}
|
||||
|
||||
public static void WriteAllLines(string path, string[] contents, Encoding encoding)
|
||||
{
|
||||
File.WriteAllLines(path, contents);
|
||||
}
|
||||
|
||||
public static void WriteAllText(string path, string contents)
|
||||
{
|
||||
if (path == null || string.IsNullOrEmpty(contents))
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(FileIO.WriteTextAsync((IStorageFile)File.CreateOrReplaceFile(path), contents)).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw File.GetRethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteAllText(string path, string contents, Encoding encoding)
|
||||
{
|
||||
File.WriteAllText(path, contents);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
11
Assets/BestHTTP/PlatformSupport/IO/File.cs.meta
Normal file
11
Assets/BestHTTP/PlatformSupport/IO/File.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57d397f0564a244aaa7be36a21e9b300
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
69
Assets/BestHTTP/PlatformSupport/IO/FileEnums.cs
Normal file
69
Assets/BestHTTP/PlatformSupport/IO/FileEnums.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
#if NETFX_CORE
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BestHTTP.PlatformSupport.IO
|
||||
{
|
||||
[Flags]
|
||||
public enum FileAccess
|
||||
{
|
||||
Read = 1,
|
||||
Write = 2,
|
||||
ReadWrite = Write | Read,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum FileAttributes
|
||||
{
|
||||
Archive = 32,
|
||||
Compressed = 2048,
|
||||
Device = 64,
|
||||
Directory = 16,
|
||||
Encrypted = 16384,
|
||||
Hidden = 2,
|
||||
Normal = 128,
|
||||
NotContentIndexed = 8192,
|
||||
Offline = 4096,
|
||||
ReadOnly = 1,
|
||||
ReparsePoint = 1024,
|
||||
SparseFile = 512,
|
||||
System = 4,
|
||||
Temporary = 256,
|
||||
}
|
||||
|
||||
public enum FileMode
|
||||
{
|
||||
CreateNew = 1,
|
||||
Create = 2,
|
||||
Open = 3,
|
||||
OpenOrCreate = 4,
|
||||
Truncate = 5,
|
||||
Append = 6,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum FileOptions
|
||||
{
|
||||
None = 0,
|
||||
Encrypted = 16384,
|
||||
DeleteOnClose = 67108864,
|
||||
SequentialScan = 134217728,
|
||||
RandomAccess = 268435456,
|
||||
Asynchronous = 1073741824,
|
||||
WriteThrough = -2147483648,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum FileShare
|
||||
{
|
||||
None = 0,
|
||||
Read = 1,
|
||||
Write = 2,
|
||||
ReadWrite = Write | Read,
|
||||
Delete = 4,
|
||||
Inheritable = 16,
|
||||
}
|
||||
}
|
||||
#endif
|
||||
11
Assets/BestHTTP/PlatformSupport/IO/FileEnums.cs.meta
Normal file
11
Assets/BestHTTP/PlatformSupport/IO/FileEnums.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 37e07c62de7114e0698c1a747fd51442
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
140
Assets/BestHTTP/PlatformSupport/IO/FileHelper.cs
Normal file
140
Assets/BestHTTP/PlatformSupport/IO/FileHelper.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
#if NETFX_CORE
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace BestHTTP.PlatformSupport.IO
|
||||
{
|
||||
public static class FileHelper
|
||||
{
|
||||
internal const string LOCAL_FOLDER = "ms-appdata:///local/";
|
||||
internal const string ROAMING_FOLDER = "ms-appdata:///roaming/";
|
||||
internal const string TEMP_FOLDER = "ms-appdata:///temp/";
|
||||
internal const string STORE_FOLDER = "isostore:/";
|
||||
|
||||
public static Stream OpenFileForReading(string uri)
|
||||
{
|
||||
return FileHelper.OpenFileForReading(FileHelper.GetFileForPathOrURI(uri));
|
||||
}
|
||||
|
||||
public static Stream OpenFileForReading(System.Uri uri)
|
||||
{
|
||||
Task<StorageFile> task = WindowsRuntimeSystemExtensions.AsTask<StorageFile>(StorageFile.GetFileFromApplicationUriAsync(uri));
|
||||
task.Wait();
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
throw new Exception("Filed to open file " + uri.ToString());
|
||||
else
|
||||
return FileHelper.OpenFileForReading(task.Result);
|
||||
}
|
||||
|
||||
public static Stream OpenFileForWriting(string uri)
|
||||
{
|
||||
string fileName = Path.GetFileName(uri);
|
||||
Task<StorageFile> task1 = WindowsRuntimeSystemExtensions.AsTask<StorageFile>(FileHelper.GetFolderForPathOrURI(uri).CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting));
|
||||
task1.Wait();
|
||||
if (task1.Status != TaskStatus.RanToCompletion)
|
||||
throw new Exception("Failed to open the file");
|
||||
Task<IRandomAccessStream> task2 = WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(task1.Result.OpenAsync(FileAccessMode.ReadWrite));
|
||||
task2.Wait();
|
||||
if (task2.Status != TaskStatus.RanToCompletion)
|
||||
throw new Exception("Failed to open the file");
|
||||
else
|
||||
return WindowsRuntimeStreamExtensions.AsStreamForWrite((IOutputStream)task2.Result);
|
||||
}
|
||||
|
||||
internal static StorageFolder GetFolderForURI(string uri)
|
||||
{
|
||||
uri = uri.ToLower();
|
||||
StorageFolder storageFolder1;
|
||||
if (uri.StartsWith("ms-appdata:///local/"))
|
||||
{
|
||||
storageFolder1 = ApplicationData.Current.LocalFolder;
|
||||
uri = uri.Replace("ms-appdata:///local/", "");
|
||||
}
|
||||
else if (uri.StartsWith("ms-appdata:///roaming/"))
|
||||
{
|
||||
storageFolder1 = ApplicationData.Current.RoamingFolder;
|
||||
uri = uri.Replace("ms-appdata:///roaming/", "");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!uri.StartsWith("ms-appdata:///temp/"))
|
||||
throw new Exception("Unsupported URI: " + uri);
|
||||
storageFolder1 = ApplicationData.Current.TemporaryFolder;
|
||||
uri = uri.Replace("ms-appdata:///temp/", "");
|
||||
}
|
||||
string[] strArray = uri.Split(new char[1]
|
||||
{
|
||||
'/'
|
||||
});
|
||||
for (int index = 0; index < strArray.Length - 1; ++index)
|
||||
{
|
||||
Task<IReadOnlyList<StorageFolder>> task = WindowsRuntimeSystemExtensions.AsTask<IReadOnlyList<StorageFolder>>(storageFolder1.CreateFolderQuery().GetFoldersAsync());
|
||||
task.Wait();
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
throw new Exception("Failed to find folder: " + strArray[index]);
|
||||
IReadOnlyList<StorageFolder> result = task.Result;
|
||||
bool flag = false;
|
||||
foreach (StorageFolder storageFolder2 in (IEnumerable<StorageFolder>)result)
|
||||
{
|
||||
if (storageFolder2.Name == strArray[index])
|
||||
{
|
||||
storageFolder1 = storageFolder2;
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
throw new Exception("Folder not found: " + strArray[index]);
|
||||
}
|
||||
return storageFolder1;
|
||||
}
|
||||
|
||||
internal static StorageFolder GetFolderForPathOrURI(string path)
|
||||
{
|
||||
if (System.Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute))
|
||||
return FileHelper.GetFolderForURI(path);
|
||||
IAsyncOperation<StorageFolder> folderFromPathAsync = StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(path));
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFolder>(folderFromPathAsync).Wait();
|
||||
return folderFromPathAsync.GetResults();
|
||||
}
|
||||
|
||||
internal static StorageFile GetFileForPathOrURI(string path)
|
||||
{
|
||||
IAsyncOperation<StorageFile> source = !System.Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute) ? StorageFile.GetFileFromPathAsync(path) : StorageFile.GetFileFromApplicationUriAsync(new System.Uri(path));
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFile>(source).Wait();
|
||||
return source.GetResults();
|
||||
}
|
||||
|
||||
internal static Stream OpenFileForReading(StorageFile file)
|
||||
{
|
||||
Task<IRandomAccessStream> task = WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(file.OpenAsync(FileAccessMode.Read));
|
||||
task.Wait();
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
throw new Exception("Failed to open file!");
|
||||
else
|
||||
return WindowsRuntimeStreamExtensions.AsStreamForRead((IInputStream)task.Result);
|
||||
}
|
||||
|
||||
internal static byte[] ReadEntireFile(StorageFile file)
|
||||
{
|
||||
Task<IBuffer> task = WindowsRuntimeSystemExtensions.AsTask<IBuffer>(FileIO.ReadBufferAsync((IStorageFile)file));
|
||||
task.Wait();
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
throw new Exception("Failed to read file");
|
||||
IBuffer result = task.Result;
|
||||
DataReader dataReader = DataReader.FromBuffer(result);
|
||||
byte[] numArray = new byte[result.Length];
|
||||
dataReader.ReadBytes(numArray);
|
||||
return numArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
11
Assets/BestHTTP/PlatformSupport/IO/FileHelper.cs.meta
Normal file
11
Assets/BestHTTP/PlatformSupport/IO/FileHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8eba5212cad144a2ba80168674807963
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
338
Assets/BestHTTP/PlatformSupport/IO/FileStream.cs
Normal file
338
Assets/BestHTTP/PlatformSupport/IO/FileStream.cs
Normal file
@@ -0,0 +1,338 @@
|
||||
#if NETFX_CORE
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace BestHTTP.PlatformSupport.IO
|
||||
{
|
||||
public class FileStream : Stream
|
||||
{
|
||||
private int readTimeout = -1;
|
||||
private int writeTimeout = 1000;
|
||||
internal const int DefaultBufferSize = 8192;
|
||||
private FileRandomAccessStream backend;
|
||||
private FileOptions fileOptions;
|
||||
private string name;
|
||||
|
||||
public override bool CanRead { get { return this.backend.CanRead; } }
|
||||
public override bool CanSeek { get { return true; } }
|
||||
public override bool CanWrite { get { return this.backend.CanWrite; } }
|
||||
public virtual bool IsAsync { get { return (this.fileOptions & FileOptions.Asynchronous) > FileOptions.None; } }
|
||||
public override long Length { get { return (long)this.backend.Size; } }
|
||||
public override int ReadTimeout { get { return this.readTimeout; } set { this.readTimeout = value; } }
|
||||
public override int WriteTimeout { get { return this.writeTimeout; } set { this.writeTimeout = value; } }
|
||||
|
||||
public string Name { get { return this.name; } }
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { return (long)this.backend.Position; }
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
this.backend.Seek((ulong)value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FileStream(string file, FileMode mode)
|
||||
: this(file, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.Read, 8192, FileOptions.None)
|
||||
{
|
||||
}
|
||||
|
||||
public FileStream(string file, FileMode mode, FileAccess access)
|
||||
: this(file, mode, access, FileShare.Read, 8192, FileOptions.None)
|
||||
{
|
||||
}
|
||||
|
||||
public FileStream(string file, FileMode mode, FileAccess access, FileShare share)
|
||||
: this(file, mode, access, share, 8192, FileOptions.None)
|
||||
{
|
||||
}
|
||||
|
||||
public FileStream(string file, FileMode mode, FileAccess access, FileShare share, int bufferSize)
|
||||
: this(file, mode, access, share, bufferSize, FileOptions.None)
|
||||
{
|
||||
}
|
||||
|
||||
public FileStream(string file, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync)
|
||||
: this(file, mode, access, share, bufferSize, useAsync ? FileOptions.Asynchronous : FileOptions.None)
|
||||
{
|
||||
}
|
||||
|
||||
public FileStream(string file, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.fileOptions = options;
|
||||
this.name = file;
|
||||
this.CheckAccess(mode, access);
|
||||
StorageFile storageFile;
|
||||
switch (mode)
|
||||
{
|
||||
case FileMode.CreateNew:
|
||||
case FileMode.Create:
|
||||
case FileMode.OpenOrCreate:
|
||||
case FileMode.Append:
|
||||
storageFile = FileStream.CreateFile(file, mode, access);
|
||||
break;
|
||||
case FileMode.Open:
|
||||
case FileMode.Truncate:
|
||||
storageFile = FileStream.OpenFile(file, mode, access);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Unknown file mode");
|
||||
}
|
||||
IAsyncOperation<IRandomAccessStream> source = storageFile.OpenAsync(FileStream.GetAccessMode(access));
|
||||
WindowsRuntimeSystemExtensions.AsTask<IRandomAccessStream>(source).Wait();
|
||||
this.backend = (FileRandomAccessStream)source.GetResults();
|
||||
if (mode == FileMode.Truncate)
|
||||
{
|
||||
this.backend.Size = 0UL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mode != FileMode.Append)
|
||||
return;
|
||||
this.backend.Seek(this.backend.Size);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckAccess(FileMode mode, FileAccess access)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case FileMode.CreateNew:
|
||||
break;
|
||||
case FileMode.Create:
|
||||
break;
|
||||
case FileMode.Open:
|
||||
break;
|
||||
case FileMode.OpenOrCreate:
|
||||
break;
|
||||
case FileMode.Truncate:
|
||||
break;
|
||||
case FileMode.Append:
|
||||
if (access == FileAccess.Write)
|
||||
break;
|
||||
else
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("FileStream.CheckAccess: Bad access mode for Append");
|
||||
throw new IOException("Bad access mode for Append");
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException("Unknown file mode");
|
||||
}
|
||||
}
|
||||
|
||||
private static StorageFile OpenFile(string file, FileMode mode, FileAccess access)
|
||||
{
|
||||
return FileHelper.GetFileForPathOrURI(file);
|
||||
}
|
||||
|
||||
private static StorageFile CreateFile(string file, FileMode mode, FileAccess access)
|
||||
{
|
||||
IAsyncOperation<StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(file).CreateFileAsync(Path.GetFileName(file), FileStream.GetCollisionOption(mode, access));
|
||||
WindowsRuntimeSystemExtensions.AsTask<StorageFile>(fileAsync).Wait();
|
||||
if (fileAsync.Status != AsyncStatus.Completed)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("FileStream.CheckAccess: Failed to create file " + file);
|
||||
throw new IOException("Failed to create file " + file);
|
||||
}
|
||||
else
|
||||
return fileAsync.GetResults();
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask<bool>(this.backend.FlushAsync()).Wait();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Flush(bool flushToDisc)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
try
|
||||
{
|
||||
Windows.Storage.Streams.Buffer buffer1 = new Windows.Storage.Streams.Buffer((uint)count);
|
||||
WindowsRuntimeSystemExtensions.AsTask<IBuffer, uint>(this.backend.ReadAsync((IBuffer)buffer1, (uint)count, InputStreamOptions.ReadAhead)).Wait(this.readTimeout);
|
||||
int length = (int)buffer1.Length;
|
||||
DataReader dataReader = DataReader.FromBuffer((IBuffer)buffer1);
|
||||
bool flag = offset == 0 && buffer.Length == count && length == count;
|
||||
byte[] numArray = flag ? buffer : new byte[length];
|
||||
dataReader.ReadBytes(numArray);
|
||||
if (!flag)
|
||||
Array.Copy((Array)numArray, 0, (Array)buffer, offset, numArray.Length);
|
||||
return length;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
if (offset > (long)this.backend.Size)
|
||||
{
|
||||
offset = (long)this.backend.Size;
|
||||
break;
|
||||
}
|
||||
else
|
||||
break;
|
||||
case SeekOrigin.Current:
|
||||
if ((long)this.backend.Position + offset > (long)this.backend.Size)
|
||||
{
|
||||
offset = (long)this.backend.Position + offset;
|
||||
break;
|
||||
}
|
||||
else
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
if (offset >= 0L)
|
||||
{
|
||||
offset = (long)this.backend.Size;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset += (long)this.backend.Size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (offset < 0L)
|
||||
offset = 0L;
|
||||
this.backend.Seek((ulong)offset);
|
||||
return offset;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.backend.Size = (ulong)value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
try
|
||||
{
|
||||
Windows.Storage.Streams.Buffer buffer1 = new Windows.Storage.Streams.Buffer((uint)count);
|
||||
byte[] numArray;
|
||||
if (offset == 0 && count == buffer.Length)
|
||||
{
|
||||
numArray = buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
numArray = new byte[count];
|
||||
Array.Copy((Array)buffer, offset, (Array)numArray, 0, count);
|
||||
}
|
||||
DataWriter dataWriter = new DataWriter();
|
||||
dataWriter.WriteBytes(numArray);
|
||||
WindowsRuntimeSystemExtensions.AsTask<uint, uint>(this.backend.WriteAsync(dataWriter.DetachBuffer())).Wait(this.writeTimeout);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw FileStream.RethrowException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
this.backend.Dispose();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
private static Exception RethrowException(Exception e)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("FileStream.RethrowException: " + e.Message + "\n" + e.StackTrace);
|
||||
if (e.GetType() == typeof(IOException))
|
||||
return e;
|
||||
else
|
||||
return (Exception)new IOException(e.Message, e);
|
||||
}
|
||||
|
||||
private static CreationCollisionOption GetCollisionOption(FileMode mode, FileAccess access)
|
||||
{
|
||||
CreationCollisionOption creationCollisionOption = CreationCollisionOption.GenerateUniqueName;
|
||||
switch (mode)
|
||||
{
|
||||
case FileMode.CreateNew:
|
||||
creationCollisionOption = CreationCollisionOption.FailIfExists;
|
||||
break;
|
||||
case FileMode.Create:
|
||||
case FileMode.Truncate:
|
||||
creationCollisionOption = CreationCollisionOption.ReplaceExisting;
|
||||
break;
|
||||
case FileMode.Open:
|
||||
case FileMode.OpenOrCreate:
|
||||
case FileMode.Append:
|
||||
creationCollisionOption = CreationCollisionOption.OpenIfExists;
|
||||
break;
|
||||
}
|
||||
return creationCollisionOption;
|
||||
}
|
||||
|
||||
private static FileAccessMode GetAccessMode(FileAccess access)
|
||||
{
|
||||
switch (access)
|
||||
{
|
||||
case FileAccess.Read:
|
||||
return FileAccessMode.Read;
|
||||
default:
|
||||
return FileAccessMode.ReadWrite;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
11
Assets/BestHTTP/PlatformSupport/IO/FileStream.cs.meta
Normal file
11
Assets/BestHTTP/PlatformSupport/IO/FileStream.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4119d4304fce48a89dfa6d57fb3d68a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
217
Assets/BestHTTP/PlatformSupport/IO/Infos.cs
Normal file
217
Assets/BestHTTP/PlatformSupport/IO/Infos.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
#if NETFX_CORE
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace BestHTTP.PlatformSupport.IO
|
||||
{
|
||||
public abstract class FileSystemInfo
|
||||
{
|
||||
public FileAttributes Attributes
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.GetAttributes();
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime CreationTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.GetCreationTime().DateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime CreationTimeUtc
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.GetCreationTime().ToUniversalTime().DateTime;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract bool Exists { get; }
|
||||
|
||||
public string Extention
|
||||
{
|
||||
get
|
||||
{
|
||||
return Path.GetExtension(this.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract string FullName { get; }
|
||||
|
||||
public abstract string Name { get; }
|
||||
|
||||
internal abstract FileAttributes GetAttributes();
|
||||
|
||||
internal abstract DateTimeOffset GetCreationTime();
|
||||
|
||||
public abstract void Delete();
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
this.RefreshInternal();
|
||||
}
|
||||
|
||||
internal abstract void RefreshInternal();
|
||||
}
|
||||
|
||||
public sealed class DirectoryInfo : FileSystemInfo
|
||||
{
|
||||
private string path;
|
||||
private StorageFolder folder;
|
||||
|
||||
public override bool Exists
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
this.RefreshInternal();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string FullName
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.folder.Path;
|
||||
}
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.folder.Name;
|
||||
}
|
||||
}
|
||||
|
||||
public DirectoryInfo(string path)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException();
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
throw new ArgumentException();
|
||||
try
|
||||
{
|
||||
this.path = path;
|
||||
this.folder = FileHelper.GetFolderForPathOrURI(path);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
internal DirectoryInfo(string path, StorageFolder folder)
|
||||
{
|
||||
this.path = path;
|
||||
this.folder = folder;
|
||||
}
|
||||
|
||||
internal override FileAttributes GetAttributes()
|
||||
{
|
||||
try
|
||||
{
|
||||
return File.WinAttributesToSysAttributes(this.folder.Attributes);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetAttributes: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetAttributes: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
internal override DateTimeOffset GetCreationTime()
|
||||
{
|
||||
try
|
||||
{
|
||||
return this.folder.DateCreated;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetCreationTime: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.GetCreationTime: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Delete()
|
||||
{
|
||||
try
|
||||
{
|
||||
WindowsRuntimeSystemExtensions.AsTask(this.folder.DeleteAsync()).Wait();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.Delete: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.Delete: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
internal override void RefreshInternal()
|
||||
{
|
||||
try
|
||||
{
|
||||
this.folder = FileHelper.GetFolderForPathOrURI(this.path);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.RefreshInternal: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine("DirectoryInfo.RefreshInternal: " + ex.Message + "\n" + ex.StackTrace);
|
||||
throw new IOException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.path;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.path.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
11
Assets/BestHTTP/PlatformSupport/IO/Infos.cs.meta
Normal file
11
Assets/BestHTTP/PlatformSupport/IO/Infos.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 68c9ba41295844c5ea47bc094fbed3e5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user