Stream< List< int>>和Stream< int>在Dart [英] What is the difference between Stream<List<int>> and Stream<int> in Dart

查看:446
本文介绍了Stream< List< int>>和Stream< int>在Dart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用Dart Streams包装我的头。特别是此示例命令行实用程序 cat 具有以下代码行:

I am trying to wrap my head around Dart Streams. In particular this example of the command line utility cat has the following lines of code:

 Stream<List<int>> stream = new File(path).openRead();

  // Transform the stream using a `StreamTransformer`. The transformers
  // used here convert the data to UTF8 and split string values into
  // individual lines.
  return stream
      .transform(UTF8.decoder)
      .transform(const LineSplitter())
      .listen((line) {
        if (showLineNumbers) {
          stdout.write('${lineNumber++} ');
        }
        stdout.writeln(line);
      }).asFuture().catchError((_) => _handleError(path));




  1. ; T> as Stream< List< int>> 让我有点困惑。为什么不声明为 Stream< int> 。 List<>类型如何使它不同。如果是列表,订阅者事件是否以某种方式缓冲?

  1. The declaration of the Stream<T> as Stream<List<int>> has me a bit confused. Why is it not declared as a Stream<int>. How does the List<> type make this different. Are the subscriber events buffered in some way if it is a List?

什么类型(如< T> )传递给第一个变换?是 int 还是列表< int>

What Type (as in <T>) is passed to the first transform? Is it an int or a List<int>?

传递给每个下一个转换的类型以及决定它们类型的类型。

What type is passed to each of the next transforms and what determines their type.

此示例在将转换结果传递给下一个转换之前读取整个文件吗?如果是这样,是否有一个例子某处如何流类似这个Node问题的非常大的文件在Node.js中解析巨大的日志文件 - 逐行阅读

Does this example read the entire file before passing the results of the transform to the next transform? If so, is there an example somewhere of how to Stream very large files similar to this Node question Parsing huge logfiles in Node.js - read in line-by-line


推荐答案


  1. 好问题。

  2. UTF8 是一个 Utf8Codec ,扩展了 Codec< String,List< int>> 。因此, UTF8.decoder 是一个 Converter< List< int>,String> List< int> li> LineSplitter 是一个 Converter< String,List< String>> 。因此,它需要 String 作为参数。 .transform(const LineSplitter())的结果流是发送每一行的 Stream< String>

  3. File.openRead 在将第一个字节写入流之前不会读取整个文件。因此,处理大型文件没有问题。

  1. Good question.
  2. UTF8 is a Utf8Codec that extends Codec<String, List<int>>. So UTF8.decoder is a Converter<List<int>, String> that takes List<int> as parameter.
  3. LineSplitter is a Converter<String, List<String>>. So it takes String as parameter. The resulting stream of .transform(const LineSplitter()) is a Stream<String> where each line is sent.
  4. File.openRead doesn't read the entire file before writing the first bytes to the stream. So there's no problem to deal with large files.

这篇关于Stream&lt; List&lt; int&gt;&gt;和Stream&lt; int&gt;在Dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆