2 * Copyright (c) 2017 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
22 using System.Threading.Tasks;
25 namespace LibTVRefCommonPortable.Utils
28 /// A class for file system control function.
33 /// A instance of file system port layer
35 private static IFileSystemAPIs fileSystemAPIs;
38 /// A instance of FileSystemUtils
40 private static readonly FileSystemUtils instance = new FileSystemUtils();
43 /// A property of FileSystemUtils instance
45 public static FileSystemUtils Instance
54 /// A unit test stub for FileSystemUtils
56 private class TestStub : IFileSystemAPIs
59 /// A directory path which should be used for app data storing.
61 public string AppDataStorage
65 return "test_app_data_path/";
70 /// A directory path which should be used for app resource storing.
72 public string AppResourceStorage
76 return "test_app_resource_path/";
81 /// A directory path which should be used for sharing between apps.
83 public string PlatformShareStorage
87 return "test_platform_share_path/";
92 /// A method closes the file.
94 /// <param name="stream">A file descriptor</param>
95 public void CloseFile(Stream stream)
100 /// A method flushing the stream to write remains.
102 /// <param name="stream">A file descriptor</param>
103 public void Flush(Stream stream)
108 /// A method checks if a file existence in the file system.
110 /// <param name="filePath">A file path</param>
111 /// <returns>An existence of the file</returns>
112 public bool IsFileExist(string filePath)
118 /// A method checks if file is read to use.
120 /// <param name="filePath">A file path</param>
121 /// <returns>A status of ready</returns>
122 public bool IsFileReady(string filePath)
128 /// A method opens a file on the given mode.
130 /// <param name="filePath">A file path</param>
131 /// <param name="mode">An opening mode</param>
132 /// <returns>A file descriptor</returns>
133 public Stream OpenFile(string filePath, UtilFileMode mode)
135 throw new NotImplementedException();
142 private FileSystemUtils()
144 fileSystemAPIs = new TestStub();
148 if (DependencyService.Get<IFileSystemAPIs>() != null)
150 fileSystemAPIs = DependencyService.Get<IFileSystemAPIs>();
153 catch (InvalidOperationException e)
155 DebuggingUtils.Err(e.Message);
160 /// A directory path which should be used for app data storing.
162 public string AppDataStorage
166 return fileSystemAPIs.AppDataStorage;
171 /// A directory path which should be used for app resource storing.
173 public string AppResourceStorage
177 return fileSystemAPIs.AppResourceStorage;
182 /// A directory path which should be used for sharing between apps.
184 public string PlatformShareStorage
188 return fileSystemAPIs.PlatformShareStorage;
193 /// A method opens a file on the given mode.
195 /// <param name="filePath">A file path</param>
196 /// <param name="mode">An opening mode</param>
197 /// <returns>A file descriptor</returns>
198 public Stream OpenFile(string filePath, UtilFileMode mode)
200 return fileSystemAPIs.OpenFile(filePath, mode);
204 /// A method flushing the stream to write remains.
206 /// <param name="stream">A file descriptor</param>
207 public void Flush(Stream stream)
209 fileSystemAPIs.Flush(stream);
213 /// A method closes the file.
215 /// <param name="stream">A file descriptor</param>
216 public void CloseFile(Stream stream)
218 fileSystemAPIs.CloseFile(stream);
222 /// A method checks if a file existence in the file system.
224 /// <param name="filePath">A file path</param>
225 /// <returns>An existence of the file</returns>
226 public bool IsFileExist(String filePath)
228 return fileSystemAPIs.IsFileExist(filePath);
232 /// A method checks if file is read to use.
234 /// <param name="filePath">A file path</param>
235 /// <returns>A status of ready</returns>
236 public bool IsFileReady(String filePath)
238 return fileSystemAPIs.IsFileReady(filePath);