Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Change implementations to return Uint8List rather than List<int> #123

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/file/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 5.0.8

* Return Uint8List rather than List<int>.

#### 5.0.7

* Dart 2 fixes for `RecordingProxyMixin` and `ReplayProxyMixin`.
Expand Down
1 change: 1 addition & 0 deletions packages/file/lib/src/backends/chroot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ library file.src.backends.chroot;

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:file/file.dart';
import 'package:file/src/common.dart' as common;
Expand Down
6 changes: 3 additions & 3 deletions packages/file/lib/src/backends/chroot/chroot_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
getDelegate(followLinks: true).openSync(mode: mode);

@override
Stream<List<int>> openRead([int start, int end]) =>
Stream<Uint8List> openRead([int start, int end]) =>
getDelegate(followLinks: true).openRead(start, end);

@override
Expand All @@ -262,11 +262,11 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
getDelegate(followLinks: true).openWrite(mode: mode, encoding: encoding);

@override
Future<List<int>> readAsBytes() =>
Future<Uint8List> readAsBytes() =>
getDelegate(followLinks: true).readAsBytes();

@override
List<int> readAsBytesSync() =>
Uint8List readAsBytesSync() =>
getDelegate(followLinks: true).readAsBytesSync();

@override
Expand Down
20 changes: 11 additions & 9 deletions packages/file/lib/src/backends/memory/memory_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math' show min;
import 'dart:typed_data';

import 'package:file/file.dart';
import 'package:file/src/common.dart' as common;
Expand Down Expand Up @@ -176,18 +177,18 @@ class MemoryFile extends MemoryFileSystemEntity implements File {
throw new UnimplementedError('TODO');

@override
Stream<List<int>> openRead([int start, int end]) {
Stream<Uint8List> openRead([int start, int end]) {
try {
FileNode node = resolvedBacking;
List<int> content = node.content;
Uint8List content = node.content;
if (start != null) {
content = end == null
? content.sublist(start)
: content.sublist(start, min(end, content.length));
}
return new Stream<List<int>>.fromIterable(<List<int>>[content]);
return new Stream<Uint8List>.fromIterable(<Uint8List>[content]);
} catch (e) {
return new Stream<List<int>>.fromFuture(new Future<List<int>>.error(e));
return new Stream<Uint8List>.fromFuture(new Future<Uint8List>.error(e));
}
}

Expand All @@ -204,10 +205,11 @@ class MemoryFile extends MemoryFileSystemEntity implements File {
}

@override
Future<List<int>> readAsBytes() async => readAsBytesSync();
Future<Uint8List> readAsBytes() async => readAsBytesSync();

@override
List<int> readAsBytesSync() => (resolvedBacking as FileNode).content;
Uint8List readAsBytesSync() =>
Uint8List.fromList((resolvedBacking as FileNode).content);

@override
Future<String> readAsString({Encoding encoding: utf8}) async =>
Expand Down Expand Up @@ -248,7 +250,7 @@ class MemoryFile extends MemoryFileSystemEntity implements File {
}
FileNode node = _resolvedBackingOrCreate;
_truncateIfNecessary(node, mode);
node.content.addAll(bytes);
node.write(bytes);
node.touch();
}

Expand Down Expand Up @@ -278,7 +280,7 @@ class MemoryFile extends MemoryFileSystemEntity implements File {

void _truncateIfNecessary(FileNode node, io.FileMode mode) {
if (mode == io.FileMode.write || mode == io.FileMode.writeOnly) {
node.content.clear();
node.clear();
}
}

Expand Down Expand Up @@ -402,7 +404,7 @@ class _FileSink implements io.IOSink {

void _addData(List<int> data) {
_pendingWrites = _pendingWrites.then((FileNode node) {
node.content.addAll(data);
node.write(data);
return node;
});
}
Expand Down
22 changes: 19 additions & 3 deletions packages/file/lib/src/backends/memory/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:typed_data';

import 'package:file/file.dart';
import 'package:file/src/io.dart' as io;

Expand Down Expand Up @@ -222,7 +224,8 @@ class RootNode extends DirectoryNode {
/// Class that represents the backing for an in-memory regular file.
class FileNode extends RealNode {
/// File contents in bytes.
List<int> content = <int>[];
Uint8List get content => _content;
Uint8List _content = Uint8List(0);

/// Constructs a new [FileNode] as a child of the specified [parent].
FileNode(DirectoryNode parent) : super(parent);
Expand All @@ -231,7 +234,20 @@ class FileNode extends RealNode {
io.FileSystemEntityType get type => io.FileSystemEntityType.file;

@override
int get size => content.length;
int get size => _content.length;

/// Appends the specified bytes to the end of this node's [content].
void write(List<int> bytes) {
Uint8List existing = _content;
_content = Uint8List(existing.length + bytes.length);
_content.setRange(0, existing.length, existing);
_content.setRange(existing.length, _content.length, bytes);
}

/// Clears the [content] of the node.
void clear() {
_content = Uint8List(0);
}

/// Copies data from [source] into this node. The [modified] and [changed]
/// fields will be reset as opposed to copied to indicate that this file
Expand All @@ -240,7 +256,7 @@ class FileNode extends RealNode {
modified = changed = new DateTime.now().millisecondsSinceEpoch;
accessed = source.accessed;
mode = source.mode;
content = new List<int>.from(source.content);
_content = Uint8List.fromList(source.content);
}
}

Expand Down
13 changes: 11 additions & 2 deletions packages/file/lib/src/backends/record_replay/codecs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io' show systemEncoding;
import 'dart:typed_data';

import 'package:file/file.dart';
import 'package:path/path.dart' as path;
Expand Down Expand Up @@ -455,6 +456,14 @@ class Listify<T> extends Converter<T, List<T>> {
List<T> convert(T input) => <T>[input];
}

class Uint8ListToPlainList extends Converter<Uint8List, List<int>> {
/// Creates a new [Uint8ListToPlainList]
const Uint8ListToPlainList();

@override
List<int> convert(Uint8List list) => List<int>.from(list);
}

/// Revives a [Directory] entity reference into a [ReplayDirectory].
class ReviveDirectory extends Converter<String, Directory> {
final ReplayFileSystemImpl _fileSystem;
Expand Down Expand Up @@ -571,15 +580,15 @@ class ToStream<T> extends Converter<List<T>, Stream<T>> {

/// Converts a blob reference (serialized as a [String] of the form
/// `!<filename>`) into a byte list.
class BlobToBytes extends Converter<String, List<int>> {
class BlobToBytes extends Converter<String, Uint8List> {
final ReplayFileSystemImpl _fileSystem;

/// Creates a new [BlobToBytes] that will use the specified file system's
/// recording to load the blob.
const BlobToBytes(this._fileSystem);

@override
List<int> convert(String input) {
Uint8List convert(String input) {
assert(input.startsWith('!'));
String basename = input.substring(1);
String dirname = _fileSystem.recording.path;
Expand Down
19 changes: 10 additions & 9 deletions packages/file/lib/src/backends/record_replay/recording_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:file/file.dart';
Expand Down Expand Up @@ -89,11 +90,11 @@ class RecordingFile extends RecordingFileSystemEntity<File> implements File {
RandomAccessFile _openSync({FileMode mode: FileMode.read}) =>
_wrapRandomAccessFile(delegate.openSync(mode: mode));

StreamReference<List<int>> _openRead([int start, int end]) {
return new _BlobStreamReference<List<int>>(
StreamReference<Uint8List> _openRead([int start, int end]) {
return new _BlobStreamReference<Uint8List>(
file: _newRecordingFile(),
stream: delegate.openRead(start, end),
writer: (File file, List<int> bytes) {
writer: (File file, Uint8List bytes) {
file.writeAsBytesSync(bytes, mode: FileMode.append, flush: true);
},
);
Expand All @@ -106,21 +107,21 @@ class RecordingFile extends RecordingFileSystemEntity<File> implements File {
);
}

FutureReference<List<int>> _readAsBytes() {
return new _BlobFutureReference<List<int>>(
FutureReference<Uint8List> _readAsBytes() {
return new _BlobFutureReference<Uint8List>(
file: _newRecordingFile(),
future: delegate.readAsBytes(),
writer: (File file, List<int> bytes) async {
writer: (File file, Uint8List bytes) async {
await file.writeAsBytes(bytes, flush: true);
},
);
}

ResultReference<List<int>> _readAsBytesSync() {
return new _BlobReference<List<int>>(
ResultReference<Uint8List> _readAsBytesSync() {
return new _BlobReference<Uint8List>(
file: _newRecordingFile(),
value: delegate.readAsBytesSync(),
writer: (File file, List<int> bytes) {
writer: (File file, Uint8List bytes) {
file.writeAsBytesSync(bytes, flush: true);
},
);
Expand Down
16 changes: 9 additions & 7 deletions packages/file/lib/src/backends/record_replay/replay_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:file/file.dart';

Expand All @@ -20,10 +21,11 @@ class ReplayFile extends ReplayFileSystemEntity implements File {
Converter<String, File> reviveFile = new ReviveFile(fileSystem);
Converter<String, Future<File>> reviveFileAsFuture =
reviveFile.fuse(const ToFuture<File>());
Converter<String, List<int>> blobToBytes = new BlobToBytes(fileSystem);
Converter<String, Future<List<int>>> blobToBytesFuture =
blobToBytes.fuse(const ToFuture<List<int>>());
Converter<String, String> blobToString = blobToBytes.fuse(utf8.decoder);
Converter<String, Uint8List> blobToBytes = new BlobToBytes(fileSystem);
Converter<String, Future<Uint8List>> blobToBytesFuture =
blobToBytes.fuse(const ToFuture<Uint8List>());
Converter<String, String> blobToString =
blobToBytes.fuse(const Uint8ListToPlainList()).fuse(utf8.decoder);
Converter<String, Future<String>> blobToStringFuture =
blobToString.fuse(const ToFuture<String>());
Converter<String, RandomAccessFile> reviveRandomAccessFile =
Expand All @@ -36,9 +38,9 @@ class ReplayFile extends ReplayFileSystemEntity implements File {
blobToString.fuse(lineSplitter);
Converter<String, Future<List<String>>> blobToLinesFuture =
blobToLines.fuse(const ToFuture<List<String>>());
Converter<String, Stream<List<int>>> blobToByteStream = blobToBytes
.fuse(const Listify<List<int>>())
.fuse(const ToStream<List<int>>());
Converter<String, Stream<Uint8List>> blobToByteStream = blobToBytes
.fuse(const Listify<Uint8List>())
.fuse(const ToStream<Uint8List>());
Converter<int, Future<DateTime>> reviveDateTime =
DateTimeCodec.deserialize.fuse(const ToFuture<DateTime>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:file/file.dart';

Expand All @@ -29,8 +30,8 @@ class ReplayRandomAccessFile extends Object
#closeSync: const Passthrough<Null>(),
#readByte: const ToFuture<int>(),
#readByteSync: const Passthrough<int>(),
#read: const ToFuture<List<int>>(),
#readSync: const Passthrough<List<int>>(),
#read: const ToFuture<Uint8List>(),
#readSync: const Passthrough<Uint8List>(),
#readInto: const ToFuture<int>(),
#readIntoSync: const Passthrough<int>(),
#writeByte: reviveRandomAccessFileAsFuture,
Expand Down
41 changes: 37 additions & 4 deletions packages/file/lib/src/forwarding/forwarding_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:file/src/io.dart' as io;
import 'package:file/file.dart';
Expand Down Expand Up @@ -71,8 +72,8 @@ abstract class ForwardingFile
delegate.openSync(mode: mode);

@override
Stream<List<int>> openRead([int start, int end]) =>
delegate.openRead(start, end);
Stream<Uint8List> openRead([int start, int end]) =>
delegate.openRead(start, end).transform(const _ToUint8List());

@override
IOSink openWrite({
Expand All @@ -82,10 +83,14 @@ abstract class ForwardingFile
delegate.openWrite(mode: mode, encoding: encoding);

@override
Future<List<int>> readAsBytes() => delegate.readAsBytes();
Future<Uint8List> readAsBytes() {
return delegate.readAsBytes().then<Uint8List>((List<int> bytes) {
return Uint8List.fromList(bytes);
});
}

@override
List<int> readAsBytesSync() => delegate.readAsBytesSync();
Uint8List readAsBytesSync() => Uint8List.fromList(delegate.readAsBytesSync());

@override
Future<String> readAsString({Encoding encoding: utf8}) =>
Expand Down Expand Up @@ -151,3 +156,31 @@ abstract class ForwardingFile
flush: flush,
);
}

class _ToUint8List extends Converter<List<int>, Uint8List> {
const _ToUint8List();

@override
Uint8List convert(List<int> input) => Uint8List.fromList(input);

@override
Sink<List<int>> startChunkedConversion(Sink<Uint8List> sink) {
return _Uint8ListConversionSink(sink);
}
}

class _Uint8ListConversionSink implements Sink<List<int>> {
const _Uint8ListConversionSink(this._target);

final Sink<Uint8List> _target;

@override
void add(List<int> data) {
_target.add(Uint8List.fromList(data));
}

@override
void close() {
_target.close();
}
}
2 changes: 1 addition & 1 deletion packages/file/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: file
version: 5.0.7
version: 5.0.8
authors:
- Matan Lurey <[email protected]>
- Yegor Jbanov <[email protected]>
Expand Down
Loading