In my current project, I wanted to ‘fake out’ the filesystem for integration tests. In several places in my code, I would first write some data to a temporary file, then later, open the file again for background processing. Now my integration tests run fine using the real filesystem, but just to make sure my tests could run in parallel, I wanted to switch to a faked filesystem.
So, my first attempt used memory streams. But then I found a crucial difference between filestreams and memorystreams. If you dispose a filestream.. the actual file is still there! Not so much with a memorystream.
So here’s an in memory filestream. If you close / dispose it.. it will still be there. Note.. it will only be cleaned up by the garbage collector. So don’t hang on to them in definetly and don’t use this in production.
1: public class InMemoryFileStream : Stream
2: {
3: private MemoryStream _memoryStream = new MemoryStream();
4:
5:
6: public override void Flush()
7: {
8: _memoryStream.Flush();
9: }
10:
11: public override long Seek(long offset, SeekOrigin origin)
12: {
13: return _memoryStream.Seek(offset, origin);
14: }
15:
16: public override void SetLength(long value)
17: {
18: _memoryStream.SetLength(value);
19: }
20:
21: public override int Read(byte[] buffer, int offset, int count)
22: {
23: return _memoryStream.Read(buffer, offset, count);
24: }
25:
26: public override void Write(byte[] buffer, int offset, int count)
27: {
28: _memoryStream.Write(buffer, offset, count);
29: }
30:
31: public override bool CanRead
32: {
33: get { return _memoryStream.CanRead; }
34: }
35:
36: public override bool CanSeek
37: {
38: get { return _memoryStream.CanSeek; }
39: }
40:
41: public override bool CanWrite
42: {
43: get { return _memoryStream.CanWrite; }
44: }
45:
46: public override long Length
47: {
48: get { return _memoryStream.Length; }
49: }
50:
51: public override long Position
52: {
53: get { return _memoryStream.Position; }
54: set { _memoryStream.Position = value; }
55: }
56:
57: protected override void Dispose(bool disposing)
58: {
59: if (disposing)
60: {
61: _memoryStream.Position = 0;
62: base.Dispose(disposing);
63: }
64: }
65: }
Lớp học kế toán tổng hợp thực hành tại hải phòng
ReplyDeleteLớp học kế toán tổng hợp thực hành tại biên hòa đồng nai
Lớp học kế toán tổng hợp thực hành tại vinh nghệ an
Lớp học kế toán tổng hợp thực hành tại hải dương
Lớp học kế toán tổng hợp thực hành tại ninh bình
Lớp học kế toán tổng hợp thực hành tại hưng yên
Lớp học kế toán tổng hợp thực hành tại phú thọ
Lớp học kế toán tổng hợp thực hành tại hà nam
Lớp học kế toán tổng hợp thực hành tại vĩnh phúc
Lớp học kế toán tổng hợp thực hành tại bắc giang
Lớp học kế toán tổng hợp thực hành tại thái nguyên
Lớp học kế toán tổng hợp thực hành tại thái bình
Lớp học kế toán tổng hợp thực hành tại nam định
Lớp học kế toán tổng hợp thực hành tại thanh hóa
Lớp học kế toán tổng hợp thực hành tại tphcm
Lớp học kế toán tổng hợp thực hành tại bắc ninh
Lớp học kế toán tổng hợp thực hành tại hà đông
Lớp học kế toán tổng hợp thực hành tại long biên
Lớp học kế toán tổng hợp thực hành tại thanh xuân
Lớp học kế toán tổng hợp thực hành tại cầu giấy
khóa học kế toán toán dành cho giám đốc và nhà quản lý
dịch vụ thành lập doanh nghiệp công ty trọn gói
dịch vụ thành lập doanh nghiệp công ty tại hải phòng
dịch vụ tạm ngừng giải thể doanh nghiệp
dich vu ke toan thue
dich vu lam bao cao tai chinh
Nice post , thanks
ReplyDeleteredhat certification in chennai | rhce training in chennai | redhat training in chennai |VMware training in chennai