Add File.lengthSync to use it for FileByteStore eviction.

R=brianwilkerson@google.com, paulberry@google.com
BUG=

Review URL: https://codereview.chromium.org/2490023003 .
This commit is contained in:
Konstantin Shcheglov 2016-11-10 13:31:06 -08:00
parent d0ee9613d4
commit 891e709c5f
5 changed files with 53 additions and 1 deletions

View file

@ -20,9 +20,15 @@ abstract class File implements Resource {
*/
Stream<WatchEvent> get changes;
/**
* Synchronously get the length of the file.
* Throws a [FileSystemException] if the operation fails.
*/
int get lengthSync;
/**
* Return the last-modified stamp of the file.
* Throws [FileSystemException] if the file does not exist.
* Throws a [FileSystemException] if the file does not exist.
*/
int get modificationStamp;

View file

@ -305,6 +305,11 @@ class _MemoryDummyLink extends _MemoryResource implements File {
@override
bool get exists => false;
@override
int get lengthSync {
throw new FileSystemException(path, 'File could not be read');
}
@override
int get modificationStamp {
int stamp = _provider._pathToTimestamp[path];
@ -370,6 +375,11 @@ class _MemoryFile extends _MemoryResource implements File {
@override
bool get exists => _provider._pathToResource[path] is _MemoryFile;
@override
int get lengthSync {
return readAsBytesSync().length;
}
@override
int get modificationStamp {
int stamp = _provider._pathToTimestamp[path];

View file

@ -126,6 +126,15 @@ class _PhysicalFile extends _PhysicalResource implements File {
@override
Stream<WatchEvent> get changes => new FileWatcher(_entry.path).events;
@override
int get lengthSync {
try {
return _file.lengthSync();
} on io.FileSystemException catch (exception) {
throw new FileSystemException(exception.path, exception.message);
}
}
@override
int get modificationStamp {
try {

View file

@ -111,6 +111,20 @@ class FileTest {
expect(file.isOrContains(provider.convertPath('/foo/bar')), isFalse);
}
void test_lengthSync_doesNotExist() {
File file = provider.getResource(provider.convertPath('/test.txt'));
expect(() {
file.lengthSync;
}, throwsA(_isFileSystemException));
}
void test_lengthSync_exists() {
List<int> bytes = <int>[1, 2, 3, 4, 5];
File file =
provider.newFileWithBytes(provider.convertPath('/file.bin'), bytes);
expect(file.lengthSync, bytes.length);
}
void test_modificationStamp_doesNotExist() {
String path = provider.convertPath('/foo/bar/file.txt');
File file = provider.newFile(path, 'qwerty');

View file

@ -97,6 +97,19 @@ class FileTest extends _BaseTest {
expect(file.isOrContains('foo'), isFalse);
}
void test_lengthSync_doesNotExist() {
File file = PhysicalResourceProvider.INSTANCE.getResource(path);
expect(() {
file.lengthSync;
}, throwsA(_isFileSystemException));
}
void test_lengthSync_exists() {
List<int> bytes = <int>[1, 2, 3, 4, 5];
new io.File(path).writeAsBytesSync(bytes);
expect(file.lengthSync, bytes.length);
}
void test_modificationStamp_doesNotExist() {
File file = PhysicalResourceProvider.INSTANCE.getResource(path);
expect(() {