diff options
author | isaacs <i@izs.me> | 2012-10-11 15:53:11 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-10-11 16:46:18 -0700 |
commit | 061f2075cf81017cdb40de80533ba18746743c94 (patch) | |
tree | 35f6b2608572336927f93c1fb7c36298e22ebf0f /lib/string_decoder.js | |
parent | d7c45ea7d03de5c4a6c71b34780150fcd1e6314c (diff) | |
download | android-node-v8-061f2075cf81017cdb40de80533ba18746743c94.tar.gz android-node-v8-061f2075cf81017cdb40de80533ba18746743c94.tar.bz2 android-node-v8-061f2075cf81017cdb40de80533ba18746743c94.zip |
string_decoder: Add 'end' method, do base64 properly
Diffstat (limited to 'lib/string_decoder.js')
-rw-r--r-- | lib/string_decoder.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/string_decoder.js b/lib/string_decoder.js index 879e590647..31d4b24702 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -32,6 +32,11 @@ var StringDecoder = exports.StringDecoder = function(encoding) { this.surrogateSize = 2; this.detectIncompleteChar = utf16DetectIncompleteChar; break; + case 'base64': + // Base-64 stores 3 bytes in 4 chars, and pads the remainder. + this.surrogateSize = 3; + this.detectIncompleteChar = base64DetectIncompleteChar; + break; default: this.write = passThroughWrite; return; @@ -145,6 +150,21 @@ StringDecoder.prototype.detectIncompleteChar = function(buffer) { return i; }; +StringDecoder.prototype.end = function(buffer) { + var res = ''; + if (buffer && buffer.length) + res = this.write(buffer); + + if (this.charReceived) { + var cr = this.charReceived; + var buf = this.charBuffer; + var enc = this.encoding; + res += buf.slice(0, cr).toString(enc); + } + + return res; +}; + function passThroughWrite(buffer) { return buffer.toString(this.encoding); } @@ -154,3 +174,9 @@ function utf16DetectIncompleteChar(buffer) { this.charLength = incomplete ? 2 : 0; return incomplete; } + +function base64DetectIncompleteChar(buffer) { + var incomplete = this.charReceived = buffer.length % 3; + this.charLength = incomplete ? 3 : 0; + return incomplete; +} |