OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library front_end.memory_file_system; | 5 library front_end.memory_file_system; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 | 10 |
11 import 'file_system.dart'; | 11 import 'file_system.dart'; |
12 | 12 |
13 /// Concrete implementation of [FileSystem] which performs its operations on an | 13 /// Concrete implementation of [FileSystem] which performs its operations on an |
14 /// in-memory virtual file system. | 14 /// in-memory virtual file system. |
15 /// | 15 /// |
16 /// Not intended to be implemented or extended by clients. | 16 /// Not intended to be implemented or extended by clients. |
17 class MemoryFileSystem implements FileSystem { | 17 class MemoryFileSystem implements FileSystem { |
18 final Map<Uri, Uint8List> _files = {}; | 18 final Map<Uri, Uint8List> _files = {}; |
19 final Map<Uri, DateTime> _lastModified = {}; | |
20 | |
21 // Counter used to create mock last-modification timestamps. The memory | |
22 // file-system is mainly used for testing, so we use mock timestamps to avoid | |
23 // introducing non-determinism. | |
24 int _lastUpdate = 0; | |
25 | 19 |
26 /// The "current directory" in the in-memory virtual file system. | 20 /// The "current directory" in the in-memory virtual file system. |
27 /// | 21 /// |
28 /// This is used to convert relative URIs to absolute URIs. | 22 /// This is used to convert relative URIs to absolute URIs. |
29 /// | 23 /// |
30 /// Always ends in a trailing '/'. | 24 /// Always ends in a trailing '/'. |
31 Uri currentDirectory; | 25 Uri currentDirectory; |
32 | 26 |
33 MemoryFileSystem(Uri currentDirectory) | 27 MemoryFileSystem(Uri currentDirectory) |
34 : currentDirectory = _addTrailingSlash(currentDirectory); | 28 : currentDirectory = _addTrailingSlash(currentDirectory); |
(...skipping 28 matching lines...) Expand all Loading... |
63 @override | 57 @override |
64 bool operator ==(Object other) => | 58 bool operator ==(Object other) => |
65 other is MemoryFileSystemEntity && | 59 other is MemoryFileSystemEntity && |
66 other.uri == uri && | 60 other.uri == uri && |
67 identical(other._fileSystem, _fileSystem); | 61 identical(other._fileSystem, _fileSystem); |
68 | 62 |
69 @override | 63 @override |
70 Future<bool> exists() async => _fileSystem._files[uri] != null; | 64 Future<bool> exists() async => _fileSystem._files[uri] != null; |
71 | 65 |
72 @override | 66 @override |
73 Future<DateTime> lastModified() async { | |
74 var lastModified = _fileSystem._lastModified[uri]; | |
75 if (lastModified == null) { | |
76 throw new FileSystemException(uri, 'File $uri does not exist.'); | |
77 } | |
78 return lastModified; | |
79 } | |
80 | |
81 @override | |
82 Future<List<int>> readAsBytes() async { | 67 Future<List<int>> readAsBytes() async { |
83 List<int> contents = _fileSystem._files[uri]; | 68 List<int> contents = _fileSystem._files[uri]; |
84 if (contents == null) { | 69 if (contents == null) { |
85 throw new FileSystemException(uri, 'File $uri does not exist.'); | 70 throw new FileSystemException(uri, 'File $uri does not exist.'); |
86 } | 71 } |
87 return contents.toList(); | 72 return contents.toList(); |
88 } | 73 } |
89 | 74 |
90 @override | 75 @override |
91 Future<String> readAsString() async { | 76 Future<String> readAsString() async { |
(...skipping 21 matching lines...) Expand all Loading... |
113 /// overwritten. | 98 /// overwritten. |
114 void writeAsStringSync(String s) { | 99 void writeAsStringSync(String s) { |
115 // Note: the return type of UTF8.encode is List<int>, but in practice it | 100 // Note: the return type of UTF8.encode is List<int>, but in practice it |
116 // always returns Uint8List. We rely on that for efficiency, so that we | 101 // always returns Uint8List. We rely on that for efficiency, so that we |
117 // don't have to make an extra copy. | 102 // don't have to make an extra copy. |
118 _update(uri, UTF8.encode(s) as Uint8List); | 103 _update(uri, UTF8.encode(s) as Uint8List); |
119 } | 104 } |
120 | 105 |
121 void _update(Uri uri, Uint8List data) { | 106 void _update(Uri uri, Uint8List data) { |
122 _fileSystem._files[uri] = data; | 107 _fileSystem._files[uri] = data; |
123 _fileSystem._lastModified[uri] = | |
124 new DateTime.fromMicrosecondsSinceEpoch(++_fileSystem._lastUpdate); | |
125 } | 108 } |
126 } | 109 } |
OLD | NEW |