avatar-js/patches/lib/string_decoder.js.patch

65 lines
1.9 KiB
Diff

--- ../node/lib/string_decoder.js 2013-04-30 22:00:43.433305623 -0700
+++ src/main/js/lib/string_decoder.js 2013-05-30 13:33:21.987526602 -0700
@@ -30,15 +30,13 @@
assertEncoding(encoding);
switch (this.encoding) {
case 'utf8':
- // CESU-8 represents each of Surrogate Pair by 3-bytes
- this.surrogateSize = 3;
- break;
case 'ucs2':
case 'utf16le':
- // UTF-16 represents each of Surrogate Pair by 2-bytes
- this.surrogateSize = 2;
- this.detectIncompleteChar = utf16DetectIncompleteChar;
- break;
+ var decoder = new Packages.com.oracle.avatar.js.buffer.StringDecoder(Buffer._javaEncoding(encoding));
+ Object.defineProperty(this, '_decoder', { writable: false, enumerable: false, value: decoder });
+ this.write = stringWrite;
+ this.end = stringEnd;
+ return;
case 'base64':
// Base-64 stores 3 bytes in 4 chars, and pads the remainder.
this.surrogateSize = 3;
@@ -54,6 +52,27 @@
this.charLength = 0;
};
+function stringWrite(buffer) {
+ // some old tests pass string.
+ if(!Buffer.isBuffer(buffer)) {
+ return buffer;
+ }
+ return this._decoder.write(buffer._impl);
+}
+
+function stringEnd(buffer) {
+ // some old tests pass string.
+ if(buffer && !Buffer.isBuffer(buffer)) {
+ return buffer;
+ }
+ var res = '';
+ if (buffer && buffer.length)
+ res = this.write(buffer);
+
+ res += this._decoder.end();
+
+ return res;
+}
StringDecoder.prototype.write = function(buffer) {
var charStr = '';
@@ -176,12 +195,6 @@
return buffer.toString(this.encoding);
}
-function utf16DetectIncompleteChar(buffer) {
- var incomplete = this.charReceived = buffer.length % 2;
- this.charLength = incomplete ? 2 : 0;
- return incomplete;
-}
-
function base64DetectIncompleteChar(buffer) {
var incomplete = this.charReceived = buffer.length % 3;
this.charLength = incomplete ? 3 : 0;