1 | # Decode streams into strings The Right Way(tm)
|
2 |
|
3 | ```javascript
|
4 | var fs = require('fs')
|
5 | var zlib = require('zlib')
|
6 | var strs = require('stringstream')
|
7 |
|
8 | var utf8Stream = fs.createReadStream('massiveLogFile.gz')
|
9 | .pipe(zlib.createGunzip())
|
10 | .pipe(strs('utf8'))
|
11 | ```
|
12 |
|
13 | No need to deal with `setEncoding()` weirdness, just compose streams
|
14 | like they were supposed to be!
|
15 |
|
16 | Handles input and output encoding:
|
17 |
|
18 | ```javascript
|
19 | // Stream from utf8 to hex to base64... Why not, ay.
|
20 | var hex64Stream = fs.createReadStream('myFile')
|
21 | .pipe(strs('utf8', 'hex'))
|
22 | .pipe(strs('hex', 'base64'))
|
23 | ```
|
24 |
|
25 | Also deals with `base64` output correctly by aligning each emitted data
|
26 | chunk so that there are no dangling `=` characters:
|
27 |
|
28 | ```javascript
|
29 | var stream = fs.createReadStream('myFile').pipe(strs('base64'))
|
30 |
|
31 | var base64Str = ''
|
32 |
|
33 | stream.on('data', function(data) { base64Str += data })
|
34 | stream.on('end', function() {
|
35 | console.log('My base64 encoded file is: ' + base64Str) // Wouldn't work with setEncoding()
|
36 | console.log('Original file is: ' + new Buffer(base64Str, 'base64'))
|
37 | })
|
38 | ```
|