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 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 @override | 60 @override |
61 int get hashCode => uri.hashCode; | 61 int get hashCode => uri.hashCode; |
62 | 62 |
63 @override | 63 @override |
64 bool operator ==(Object other) => | 64 bool operator ==(Object other) => |
65 other is MemoryFileSystemEntity && | 65 other is MemoryFileSystemEntity && |
66 other.uri == uri && | 66 other.uri == uri && |
67 identical(other._fileSystem, _fileSystem); | 67 identical(other._fileSystem, _fileSystem); |
68 | 68 |
69 @override | 69 @override |
70 Future<List<int>> readAsBytes() async { | |
71 List<int> contents = _fileSystem._files[uri]; | |
72 if (contents != null) { | |
73 return contents.toList(); | |
74 } | |
75 throw new Exception('File does not exist'); | |
76 } | |
77 | |
78 @override | |
79 Future<String> readAsString() async { | |
80 List<int> contents = await readAsBytes(); | |
81 return UTF8.decode(contents); | |
82 } | |
83 | |
84 @override | |
85 Future<bool> exists() async => _fileSystem._files[uri] != null; | 70 Future<bool> exists() async => _fileSystem._files[uri] != null; |
86 | 71 |
87 @override | 72 @override |
88 Future<DateTime> lastModified() async { | 73 Future<DateTime> lastModified() async { |
89 var lastModified = _fileSystem._lastModified[uri]; | 74 var lastModified = _fileSystem._lastModified[uri]; |
90 if (lastModified == null) { | 75 if (lastModified == null) { |
91 throw new Exception('File does not exist'); | 76 throw new FileSystemException(uri, 'File $uri does not exist.'); |
92 } | 77 } |
93 return lastModified; | 78 return lastModified; |
94 } | 79 } |
95 | 80 |
| 81 @override |
| 82 Future<List<int>> readAsBytes() async { |
| 83 List<int> contents = _fileSystem._files[uri]; |
| 84 if (contents == null) { |
| 85 throw new FileSystemException(uri, 'File $uri does not exist.'); |
| 86 } |
| 87 return contents.toList(); |
| 88 } |
| 89 |
| 90 @override |
| 91 Future<String> readAsString() async { |
| 92 List<int> bytes = await readAsBytes(); |
| 93 try { |
| 94 return UTF8.decode(bytes); |
| 95 } on FormatException catch (e) { |
| 96 throw new FileSystemException(uri, e.message); |
| 97 } |
| 98 } |
| 99 |
96 /// Writes the given raw bytes to this file system entity. | 100 /// Writes the given raw bytes to this file system entity. |
97 /// | 101 /// |
98 /// If no file exists, one is created. If a file exists already, it is | 102 /// If no file exists, one is created. If a file exists already, it is |
99 /// overwritten. | 103 /// overwritten. |
100 void writeAsBytesSync(List<int> bytes) { | 104 void writeAsBytesSync(List<int> bytes) { |
101 _update(uri, new Uint8List.fromList(bytes)); | 105 _update(uri, new Uint8List.fromList(bytes)); |
102 } | 106 } |
103 | 107 |
104 /// Writes the given string to this file system entity. | 108 /// Writes the given string to this file system entity. |
105 /// | 109 /// |
106 /// The string is encoded as UTF-8. | 110 /// The string is encoded as UTF-8. |
107 /// | 111 /// |
108 /// If no file exists, one is created. If a file exists already, it is | 112 /// If no file exists, one is created. If a file exists already, it is |
109 /// overwritten. | 113 /// overwritten. |
110 void writeAsStringSync(String s) { | 114 void writeAsStringSync(String s) { |
111 // Note: the return type of UTF8.encode is List<int>, but in practice it | 115 // Note: the return type of UTF8.encode is List<int>, but in practice it |
112 // always returns Uint8List. We rely on that for efficiency, so that we | 116 // always returns Uint8List. We rely on that for efficiency, so that we |
113 // don't have to make an extra copy. | 117 // don't have to make an extra copy. |
114 _update(uri, UTF8.encode(s) as Uint8List); | 118 _update(uri, UTF8.encode(s) as Uint8List); |
115 } | 119 } |
116 | 120 |
117 void _update(Uri uri, Uint8List data) { | 121 void _update(Uri uri, Uint8List data) { |
118 _fileSystem._files[uri] = data; | 122 _fileSystem._files[uri] = data; |
119 _fileSystem._lastModified[uri] = | 123 _fileSystem._lastModified[uri] = |
120 new DateTime.fromMicrosecondsSinceEpoch(++_fileSystem._lastUpdate); | 124 new DateTime.fromMicrosecondsSinceEpoch(++_fileSystem._lastUpdate); |
121 } | 125 } |
122 } | 126 } |
OLD | NEW |