Node.JS数据输入流
是否有一个输入流扩展,以便我可以像以前那样调用方法 比如说Node.JS数据输入流,node.js,module,stream,extend,Node.js,Module,Stream,Extend,是否有一个输入流扩展,以便我可以像以前那样调用方法 比如说 stdin.readData(function (err, buffer) { // err if an error event was created, buffer if this is just data, null to both if the end of the stream was reached. // Added bonuses would be other methods I am used to in Ja
stdin.readData(function (err, buffer) { // err if an error event was created, buffer if this is just data, null to both if the end of the stream was reached.
// Added bonuses would be other methods I am used to in Java
// - readLine
// - readFully
// - readStringUtf8
// - readInt, readDouble, readBoolean, etc.
})
后端将监听
数据
,结束
,和错误
事件,并自动缓冲它们,当我调用读取数据时,它们才可用。这项功能并不难实现。您所要做的就是掌握ReadableStream原型并实现.read
方法
未经测试的代码:
var ReadableStream = Object.getPrototypeOf(process.stdin);
ReadableStream.read = function(cb) {
this.on('data', function(buf) {
cb(null, buf);
});
this.on('error', function(err) {
cb(err, null);
});
this.on('end', function() {
cb(null, null);
});
this.on('close', function() {
cb(new Error("Stream closed"), null);
});
};
实际上,cb
每次调用read
只需调用一次。以上代码是不够的。我会写我自己的,只是想知道是否已经有一个可能有其他功能,如Readfull。@GeorgeBailey在npm上快速搜索“read”找到了。我个人不知道这个功能有什么可靠的代码基础,但应该有一些(即使它们没有文档)