-
Notifications
You must be signed in to change notification settings - Fork 2
/
MemoryFile.cs
84 lines (64 loc) · 2.35 KB
/
MemoryFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AshMind.Gallery.Core.IO;
namespace AshMind.Gallery.Core.Tests.IO {
public class MemoryFile : IFile {
private MemoryStream stream = new MemoryStream();
public MemoryFile() {
this.Exists = true;
}
public MemoryFile(MemoryLocation location, string name, bool exists) {
this.Location = location;
this.Name = name;
this.Exists = exists;
}
public DateTimeOffset GetLastWriteTime() {
throw new NotImplementedException();
}
private void ReopenStream() {
var bytes = this.stream.ToArray();
this.stream = new MemoryStream();
this.stream.Write(bytes, 0, bytes.Length);
}
public Stream Read(FileLockMode lockMode) {
if (!this.stream.CanRead) // disposed
this.ReopenStream();
this.stream.Seek(0, SeekOrigin.Begin);
return this.stream;
}
public Stream Open(FileLockMode lockMode, FileOpenMode openMode) {
if (!this.stream.CanWrite) // disposed
this.ReopenStream();
if (openMode == FileOpenMode.Append) {
this.stream.Seek(0, SeekOrigin.End);
return this.stream;
}
if (openMode == FileOpenMode.ReadOrWrite) {
this.stream.Seek(0, SeekOrigin.Begin);
return this.stream;
}
if (openMode == FileOpenMode.Recreate) {
this.stream = new MemoryStream();
return this.stream;
}
throw new ArgumentOutOfRangeException("openMode");
}
public string Name { get; set; }
public string Path {
get { throw new NotImplementedException(); }
}
public bool Exists { get; set; }
public bool IsHidden() {
throw new NotImplementedException();
}
public void SetHidden(bool value) {
throw new NotImplementedException();
}
public byte[] GetBytes() {
return this.stream.ToArray();
}
public ILocation Location { get; set; }
}
}