DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
LocalFile.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 
17 using System;
18 using System.IO;
19 
20 namespace Deveel.Data.Store {
21  public sealed class LocalFile : IFile {
22  private System.IO.FileStream fileStream;
23 
24  public const int BufferSize = 1024 * 2;
25 
26  public LocalFile(string fileName, bool readOnly) {
27  if (String.IsNullOrEmpty(fileName))
28  throw new ArgumentNullException("fileName");
29 
30  var fileMode = readOnly ? FileMode.Open : FileMode.OpenOrCreate;
31  var fileAccess = readOnly ? FileAccess.Read : FileAccess.ReadWrite;
32  // TODO: using the enrypted option uses the user's encryption: should we use a different system?
33  var options = FileOptions.WriteThrough | FileOptions.Encrypted;
34  fileStream = new System.IO.FileStream(fileName, fileMode, fileAccess, FileShare.None, BufferSize, options);
35  }
36 
37  public void Dispose() {
38  Dispose(true);
39  GC.SuppressFinalize(this);
40  }
41 
42  private void Dispose(bool disposing) {
43  if (disposing) {
44  if (fileStream != null)
45  fileStream.Dispose();
46  }
47 
48  fileStream = null;
49  }
50 
51  public string FileName {get; private set; }
52 
53  public bool IsReadOnly { get; private set; }
54 
55  public long Position {
56  get { return fileStream.Position; }
57  }
58 
59  public long Length {
60  get { return fileStream.Length; }
61  }
62 
63  public bool Exists {
64  get { return File.Exists(FileName); }
65  }
66 
67  public long Seek(long offset, SeekOrigin origin) {
68  return fileStream.Seek(offset, origin);
69  }
70 
71  public void SetLength(long value) {
72  fileStream.SetLength(value);
73  }
74 
75  public int Read(byte[] buffer, int offset, int length) {
76  return fileStream.Read(buffer, offset, length);
77  }
78 
79  public void Write(byte[] buffer, int offset, int length) {
80  fileStream.Write(buffer, offset, length);
81  }
82 
83  public void Flush(bool writeThrough) {
84  fileStream.Flush();
85  }
86 
87  public void Close() {
88  fileStream.Close();
89  }
90 
91  public void Delete() {
92  try {
93  File.Delete(FileName);
94  } finally {
95  fileStream = null;
96  }
97 
98  }
99  }
100 }
void Dispose(bool disposing)
Definition: LocalFile.cs:42
LocalFile(string fileName, bool readOnly)
Definition: LocalFile.cs:26
long Seek(long offset, SeekOrigin origin)
Definition: LocalFile.cs:67
void Flush(bool writeThrough)
Definition: LocalFile.cs:83
int Read(byte[] buffer, int offset, int length)
Definition: LocalFile.cs:75
void Write(byte[] buffer, int offset, int length)
Definition: LocalFile.cs:79
System.IO.FileStream fileStream
Definition: LocalFile.cs:22
void SetLength(long value)
Definition: LocalFile.cs:71