Skip to content

Commit 79eb583

Browse files
authored
Change implementations to return Uint8List rather than List<int> (flutter#123)
This is in preparation for dart-lang/sdk#36900
1 parent 7a2153e commit 79eb583

15 files changed

+132
-67
lines changed

packages/file/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#### 5.0.8
2+
3+
* Return Uint8List rather than List<int>.
4+
15
#### 5.0.7
26

37
* Dart 2 fixes for `RecordingProxyMixin` and `ReplayProxyMixin`.

packages/file/lib/src/backends/chroot.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library file.src.backends.chroot;
66

77
import 'dart:async';
88
import 'dart:convert';
9+
import 'dart:typed_data';
910

1011
import 'package:file/file.dart';
1112
import 'package:file/src/common.dart' as common;

packages/file/lib/src/backends/chroot/chroot_file.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class _ChrootFile extends _ChrootFileSystemEntity<File, io.File>
251251
getDelegate(followLinks: true).openSync(mode: mode);
252252

253253
@override
254-
Stream<List<int>> openRead([int start, int end]) =>
254+
Stream<Uint8List> openRead([int start, int end]) =>
255255
getDelegate(followLinks: true).openRead(start, end);
256256

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

264264
@override
265-
Future<List<int>> readAsBytes() =>
265+
Future<Uint8List> readAsBytes() =>
266266
getDelegate(followLinks: true).readAsBytes();
267267

268268
@override
269-
List<int> readAsBytesSync() =>
269+
Uint8List readAsBytesSync() =>
270270
getDelegate(followLinks: true).readAsBytesSync();
271271

272272
@override

packages/file/lib/src/backends/memory/memory_file.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:convert';
77
import 'dart:math' show min;
8+
import 'dart:typed_data';
89

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

178179
@override
179-
Stream<List<int>> openRead([int start, int end]) {
180+
Stream<Uint8List> openRead([int start, int end]) {
180181
try {
181182
FileNode node = resolvedBacking;
182-
List<int> content = node.content;
183+
Uint8List content = node.content;
183184
if (start != null) {
184185
content = end == null
185186
? content.sublist(start)
186187
: content.sublist(start, min(end, content.length));
187188
}
188-
return new Stream<List<int>>.fromIterable(<List<int>>[content]);
189+
return new Stream<Uint8List>.fromIterable(<Uint8List>[content]);
189190
} catch (e) {
190-
return new Stream<List<int>>.fromFuture(new Future<List<int>>.error(e));
191+
return new Stream<Uint8List>.fromFuture(new Future<Uint8List>.error(e));
191192
}
192193
}
193194

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

206207
@override
207-
Future<List<int>> readAsBytes() async => readAsBytesSync();
208+
Future<Uint8List> readAsBytes() async => readAsBytesSync();
208209

209210
@override
210-
List<int> readAsBytesSync() => (resolvedBacking as FileNode).content;
211+
Uint8List readAsBytesSync() =>
212+
Uint8List.fromList((resolvedBacking as FileNode).content);
211213

212214
@override
213215
Future<String> readAsString({Encoding encoding: utf8}) async =>
@@ -248,7 +250,7 @@ class MemoryFile extends MemoryFileSystemEntity implements File {
248250
}
249251
FileNode node = _resolvedBackingOrCreate;
250252
_truncateIfNecessary(node, mode);
251-
node.content.addAll(bytes);
253+
node.write(bytes);
252254
node.touch();
253255
}
254256

@@ -278,7 +280,7 @@ class MemoryFile extends MemoryFileSystemEntity implements File {
278280

279281
void _truncateIfNecessary(FileNode node, io.FileMode mode) {
280282
if (mode == io.FileMode.write || mode == io.FileMode.writeOnly) {
281-
node.content.clear();
283+
node.clear();
282284
}
283285
}
284286

@@ -402,7 +404,7 @@ class _FileSink implements io.IOSink {
402404

403405
void _addData(List<int> data) {
404406
_pendingWrites = _pendingWrites.then((FileNode node) {
405-
node.content.addAll(data);
407+
node.write(data);
406408
return node;
407409
});
408410
}

packages/file/lib/src/backends/memory/node.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:typed_data';
6+
57
import 'package:file/file.dart';
68
import 'package:file/src/io.dart' as io;
79

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

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

233236
@override
234-
int get size => content.length;
237+
int get size => _content.length;
238+
239+
/// Appends the specified bytes to the end of this node's [content].
240+
void write(List<int> bytes) {
241+
Uint8List existing = _content;
242+
_content = Uint8List(existing.length + bytes.length);
243+
_content.setRange(0, existing.length, existing);
244+
_content.setRange(existing.length, _content.length, bytes);
245+
}
246+
247+
/// Clears the [content] of the node.
248+
void clear() {
249+
_content = Uint8List(0);
250+
}
235251

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

packages/file/lib/src/backends/record_replay/codecs.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io' show systemEncoding;
8+
import 'dart:typed_data';
89

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

459+
class Uint8ListToPlainList extends Converter<Uint8List, List<int>> {
460+
/// Creates a new [Uint8ListToPlainList]
461+
const Uint8ListToPlainList();
462+
463+
@override
464+
List<int> convert(Uint8List list) => List<int>.from(list);
465+
}
466+
458467
/// Revives a [Directory] entity reference into a [ReplayDirectory].
459468
class ReviveDirectory extends Converter<String, Directory> {
460469
final ReplayFileSystemImpl _fileSystem;
@@ -571,15 +580,15 @@ class ToStream<T> extends Converter<List<T>, Stream<T>> {
571580

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

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

581590
@override
582-
List<int> convert(String input) {
591+
Uint8List convert(String input) {
583592
assert(input.startsWith('!'));
584593
String basename = input.substring(1);
585594
String dirname = _fileSystem.recording.path;

packages/file/lib/src/backends/record_replay/recording_file.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:convert';
7+
import 'dart:typed_data';
78

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

92-
StreamReference<List<int>> _openRead([int start, int end]) {
93-
return new _BlobStreamReference<List<int>>(
93+
StreamReference<Uint8List> _openRead([int start, int end]) {
94+
return new _BlobStreamReference<Uint8List>(
9495
file: _newRecordingFile(),
9596
stream: delegate.openRead(start, end),
96-
writer: (File file, List<int> bytes) {
97+
writer: (File file, Uint8List bytes) {
9798
file.writeAsBytesSync(bytes, mode: FileMode.append, flush: true);
9899
},
99100
);
@@ -106,21 +107,21 @@ class RecordingFile extends RecordingFileSystemEntity<File> implements File {
106107
);
107108
}
108109

109-
FutureReference<List<int>> _readAsBytes() {
110-
return new _BlobFutureReference<List<int>>(
110+
FutureReference<Uint8List> _readAsBytes() {
111+
return new _BlobFutureReference<Uint8List>(
111112
file: _newRecordingFile(),
112113
future: delegate.readAsBytes(),
113-
writer: (File file, List<int> bytes) async {
114+
writer: (File file, Uint8List bytes) async {
114115
await file.writeAsBytes(bytes, flush: true);
115116
},
116117
);
117118
}
118119

119-
ResultReference<List<int>> _readAsBytesSync() {
120-
return new _BlobReference<List<int>>(
120+
ResultReference<Uint8List> _readAsBytesSync() {
121+
return new _BlobReference<Uint8List>(
121122
file: _newRecordingFile(),
122123
value: delegate.readAsBytesSync(),
123-
writer: (File file, List<int> bytes) {
124+
writer: (File file, Uint8List bytes) {
124125
file.writeAsBytesSync(bytes, flush: true);
125126
},
126127
);

packages/file/lib/src/backends/record_replay/replay_file.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:convert';
7+
import 'dart:typed_data';
78

89
import 'package:file/file.dart';
910

@@ -20,10 +21,11 @@ class ReplayFile extends ReplayFileSystemEntity implements File {
2021
Converter<String, File> reviveFile = new ReviveFile(fileSystem);
2122
Converter<String, Future<File>> reviveFileAsFuture =
2223
reviveFile.fuse(const ToFuture<File>());
23-
Converter<String, List<int>> blobToBytes = new BlobToBytes(fileSystem);
24-
Converter<String, Future<List<int>>> blobToBytesFuture =
25-
blobToBytes.fuse(const ToFuture<List<int>>());
26-
Converter<String, String> blobToString = blobToBytes.fuse(utf8.decoder);
24+
Converter<String, Uint8List> blobToBytes = new BlobToBytes(fileSystem);
25+
Converter<String, Future<Uint8List>> blobToBytesFuture =
26+
blobToBytes.fuse(const ToFuture<Uint8List>());
27+
Converter<String, String> blobToString =
28+
blobToBytes.fuse(const Uint8ListToPlainList()).fuse(utf8.decoder);
2729
Converter<String, Future<String>> blobToStringFuture =
2830
blobToString.fuse(const ToFuture<String>());
2931
Converter<String, RandomAccessFile> reviveRandomAccessFile =
@@ -36,9 +38,9 @@ class ReplayFile extends ReplayFileSystemEntity implements File {
3638
blobToString.fuse(lineSplitter);
3739
Converter<String, Future<List<String>>> blobToLinesFuture =
3840
blobToLines.fuse(const ToFuture<List<String>>());
39-
Converter<String, Stream<List<int>>> blobToByteStream = blobToBytes
40-
.fuse(const Listify<List<int>>())
41-
.fuse(const ToStream<List<int>>());
41+
Converter<String, Stream<Uint8List>> blobToByteStream = blobToBytes
42+
.fuse(const Listify<Uint8List>())
43+
.fuse(const ToStream<Uint8List>());
4244
Converter<int, Future<DateTime>> reviveDateTime =
4345
DateTimeCodec.deserialize.fuse(const ToFuture<DateTime>());
4446

packages/file/lib/src/backends/record_replay/replay_random_access_file.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:convert';
7+
import 'dart:typed_data';
78

89
import 'package:file/file.dart';
910

@@ -29,8 +30,8 @@ class ReplayRandomAccessFile extends Object
2930
#closeSync: const Passthrough<Null>(),
3031
#readByte: const ToFuture<int>(),
3132
#readByteSync: const Passthrough<int>(),
32-
#read: const ToFuture<List<int>>(),
33-
#readSync: const Passthrough<List<int>>(),
33+
#read: const ToFuture<Uint8List>(),
34+
#readSync: const Passthrough<Uint8List>(),
3435
#readInto: const ToFuture<int>(),
3536
#readIntoSync: const Passthrough<int>(),
3637
#writeByte: reviveRandomAccessFileAsFuture,

packages/file/lib/src/forwarding/forwarding_file.dart

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:convert';
7+
import 'dart:typed_data';
78

89
import 'package:file/src/io.dart' as io;
910
import 'package:file/file.dart';
@@ -71,8 +72,8 @@ abstract class ForwardingFile
7172
delegate.openSync(mode: mode);
7273

7374
@override
74-
Stream<List<int>> openRead([int start, int end]) =>
75-
delegate.openRead(start, end);
75+
Stream<Uint8List> openRead([int start, int end]) =>
76+
delegate.openRead(start, end).transform(const _ToUint8List());
7677

7778
@override
7879
IOSink openWrite({
@@ -82,10 +83,14 @@ abstract class ForwardingFile
8283
delegate.openWrite(mode: mode, encoding: encoding);
8384

8485
@override
85-
Future<List<int>> readAsBytes() => delegate.readAsBytes();
86+
Future<Uint8List> readAsBytes() {
87+
return delegate.readAsBytes().then<Uint8List>((List<int> bytes) {
88+
return Uint8List.fromList(bytes);
89+
});
90+
}
8691

8792
@override
88-
List<int> readAsBytesSync() => delegate.readAsBytesSync();
93+
Uint8List readAsBytesSync() => Uint8List.fromList(delegate.readAsBytesSync());
8994

9095
@override
9196
Future<String> readAsString({Encoding encoding: utf8}) =>
@@ -151,3 +156,31 @@ abstract class ForwardingFile
151156
flush: flush,
152157
);
153158
}
159+
160+
class _ToUint8List extends Converter<List<int>, Uint8List> {
161+
const _ToUint8List();
162+
163+
@override
164+
Uint8List convert(List<int> input) => Uint8List.fromList(input);
165+
166+
@override
167+
Sink<List<int>> startChunkedConversion(Sink<Uint8List> sink) {
168+
return _Uint8ListConversionSink(sink);
169+
}
170+
}
171+
172+
class _Uint8ListConversionSink implements Sink<List<int>> {
173+
const _Uint8ListConversionSink(this._target);
174+
175+
final Sink<Uint8List> _target;
176+
177+
@override
178+
void add(List<int> data) {
179+
_target.add(Uint8List.fromList(data));
180+
}
181+
182+
@override
183+
void close() {
184+
_target.close();
185+
}
186+
}

packages/file/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: file
2-
version: 5.0.7
2+
version: 5.0.8
33
authors:
44
- Matan Lurey <[email protected]>
55
- Yegor Jbanov <[email protected]>

0 commit comments

Comments
 (0)