case 'base64':
return base64ToBytes(str).length
+ case 'ucs2':
+ case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
+ case 'utf16be':
+ case 'utf-16be':
+ return str.length * 2
+
default:
throw new Error('Unknown encoding')
}
return Buffer._charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length)
}
+function _utf16leWrite (buf, string, offset, length) {
+ var bytes, pos
+ return Buffer._charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length)
+}
+
+function _utf16leWrite (buf, string, offset, length) {
+ var bytes, pos
+ return Buffer._charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length)
+}
+
+function _utf16beWrite (buf, string, offset, length) {
+ var bytes, pos
+ return Buffer._charsWritten = blitBuffer(utf16beToBytes(string), buf, offset, length)
+}
+
function BufferWrite (string, offset, length, encoding) {
// Support both (string, offset, length, encoding)
// and the legacy (string, encoding, offset, length)
case 'base64':
return _base64Write(this, string, offset, length)
+ case 'ucs2':
+ case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
+ return _utf16leWrite(this, string, offset, length)
+
+ case 'utf16be':
+ case 'utf-16be':
+ return _utf16beWrite(this, string, offset, length)
+
default:
throw new Error('Unknown encoding')
}
case 'base64':
return _base64Slice(self, start, end)
+ case 'ucs2':
+ case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
+ return _utf16leSlice(self, start, end)
+
+ case 'utf16be':
+ case 'utf-16be':
+ return _utf16beSlice(self, start, end)
+
default:
throw new Error('Unknown encoding')
}
return out
}
+function _utf16leSlice (buf, start, end) {
+ var bytes = buf.slice(start, end)
+ var res = ''
+ for (var i = 0; i < bytes.length; i += 2) {
+ res += String.fromCharCode(bytes[i] + bytes[i+1] * 256)
+ }
+ return res
+}
+
+function _utf16beSlice (buf, start, end) {
+ var bytes = buf.slice(start, end)
+ var res = ''
+ for (var i = 0; i < bytes.length; i += 2) {
+ res += String.fromCharCode(bytes[i] * 256 + bytes[i+1])
+ }
+ return res
+}
+
// TODO: add test that modifying the new buffer slice will modify memory in the
// original buffer! Use code from:
// http://nodejs.org/api/buffer.html#buffer_buf_slice_start_end
return byteArray
}
+function utf16leToBytes (str) {
+ var c, hi, lo
+ var byteArray = []
+ for (var i = 0; i < str.length; i++) {
+ c = str.charCodeAt(i)
+ hi = c >> 8
+ lo = c % 256
+ byteArray.push(lo)
+ byteArray.push(hi)
+ }
+
+ return byteArray
+}
+
+function utf16beToBytes (str) {
+ var c, hi, lo
+ var byteArray = []
+ for (var i = 0; i < str.length; i++) {
+ c = str.charCodeAt(i)
+ hi = c >> 8
+ lo = c % 256
+ byteArray.push(hi)
+ byteArray.push(lo)
+ }
+
+ return byteArray
+}
+
function base64ToBytes (str) {
return base64.toByteArray(str)
}