binary file r/w
This commit is contained in:
parent
8c165edc0c
commit
c14de9c3e9
2 changed files with 38 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
import 'package:actors/actors.dart';
|
import 'package:actors/actors.dart';
|
||||||
|
|
||||||
sealed class FileMessage {
|
sealed class FileMessage {
|
||||||
|
|
@ -16,6 +17,17 @@ final class SaveFile extends FileMessage {
|
||||||
const SaveFile(this.path, this.content);
|
const SaveFile(this.path, this.content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final class LoadBinaryFile extends FileMessage {
|
||||||
|
final String path;
|
||||||
|
const LoadBinaryFile(this.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
final class SaveBinaryFile extends FileMessage {
|
||||||
|
final String path;
|
||||||
|
final Uint8List bytes;
|
||||||
|
const SaveBinaryFile(this.path, this.bytes);
|
||||||
|
}
|
||||||
|
|
||||||
sealed class FileResult {
|
sealed class FileResult {
|
||||||
const FileResult();
|
const FileResult();
|
||||||
}
|
}
|
||||||
|
|
@ -29,11 +41,20 @@ final class FileSaved extends FileResult {
|
||||||
const FileSaved();
|
const FileSaved();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final class BinaryFileLoaded extends FileResult {
|
||||||
|
final Uint8List bytes;
|
||||||
|
const BinaryFileLoaded(this.bytes);
|
||||||
|
}
|
||||||
|
|
||||||
class FileActor with Handler<FileMessage, FileResult> {
|
class FileActor with Handler<FileMessage, FileResult> {
|
||||||
@override
|
@override
|
||||||
Future<FileResult> handle(FileMessage message) => switch (message) {
|
Future<FileResult> handle(FileMessage message) => switch (message) {
|
||||||
LoadFile(:final path) => File(path).readAsString().then(FileLoaded.new),
|
LoadFile(:final path) => File(path).readAsString().then(FileLoaded.new),
|
||||||
SaveFile(:final path, :final content) =>
|
SaveFile(:final path, :final content) =>
|
||||||
File(path).writeAsString(content).then((_) => const FileSaved()),
|
File(path).writeAsString(content).then((_) => const FileSaved()),
|
||||||
|
LoadBinaryFile(:final path) =>
|
||||||
|
File(path).readAsBytes().then(BinaryFileLoaded.new),
|
||||||
|
SaveBinaryFile(:final path, :final bytes) =>
|
||||||
|
File(path).writeAsBytes(bytes).then((_) => const FileSaved()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
import 'package:actors/actors.dart';
|
import 'package:actors/actors.dart';
|
||||||
import 'package:swarm/swarm.dart';
|
import 'package:swarm/swarm.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
@ -38,5 +39,21 @@ void main() {
|
||||||
throwsA(isA<PathNotFoundException>()),
|
throwsA(isA<PathNotFoundException>()),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('saves binary file', () async {
|
||||||
|
final path = '${tempDir.path}/data.bin';
|
||||||
|
final bytes = Uint8List.fromList([0x00, 0xFF, 0x89, 0x50, 0x4E, 0x47]);
|
||||||
|
final result = await actor.send(SaveBinaryFile(path, bytes));
|
||||||
|
expect(result, isA<FileSaved>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('loads a saved binary file', () async {
|
||||||
|
final path = '${tempDir.path}/data.bin';
|
||||||
|
final bytes = Uint8List.fromList([0x00, 0xFF, 0x89, 0x50, 0x4E, 0x47]);
|
||||||
|
await actor.send(SaveBinaryFile(path, bytes));
|
||||||
|
final result = await actor.send(LoadBinaryFile(path));
|
||||||
|
expect(result, isA<BinaryFileLoaded>());
|
||||||
|
expect((result as BinaryFileLoaded).bytes, equals(bytes));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue