Created
December 12, 2011 20:57
-
-
Save jhurliman/1469062 to your computer and use it in GitHub Desktop.
Stream from one file to another one line at a time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var input = fs.createReadStream('input.txt'); | |
var output = fs.createWriteStream('output.txt'); | |
var buffer = ''; | |
input.on('data', function(data) { | |
buffer += data; | |
var index = buffer.indexOf('\n'); | |
while (index > -1) { | |
var line = buffer.substr(0, index); | |
buffer = buffer.substr(index + 1); | |
parseLine(line); | |
index = buffer.indexOf('\n'); | |
} | |
// Show progress on the console | |
process.stdout.write('.'); | |
}); | |
input.on('end', function() { | |
console.log('Finished reading input'); | |
output.end(); // Terminate the stream | |
output.destroySoon(); // Close the file when the write queue is drained | |
}); | |
output.on('drain', function() { | |
// Output buffer is empty again, continue reading | |
input.resume(); | |
}); | |
function parseLine(line) { | |
// Apply a simple upper-case transformation. If the kernel buffer is full, | |
// stop reading from the input file until the output.drain event is called | |
if (!output.write(line.toUpperCase() + '\n', 'ascii')) | |
input.pause(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment