Skip to content

Instantly share code, notes, and snippets.

@yuchi
Last active December 16, 2015 20:09
Show Gist options
  • Save yuchi/5490028 to your computer and use it in GitHub Desktop.
Save yuchi/5490028 to your computer and use it in GitHub Desktop.
Titanium SDK: a snippet to read a Stream into a string preserving UTF-8 characters
/**
* Titanium SDK - Stream UTF-8 reader
* ==================================
*
* a snippet to read a Stream into a string preserving UTF-8 characters
*
* Author: Pier Paolo Ramon
* Copyright 2013 SMC Treviso <http://www.smc.it>
*
*/
var chunkSize = 1024;
var memoize = (typeof _ !== 'undefined') && _.memoize || function(func, hasher) {
var has = Object.prototype.hasOwnProperty;
var memo = {};
hasher || (hasher = function (o) { return o; });
return function() {
var key = hasher.apply(this, arguments);
return has.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
};
};
};
var firstByteToSequenceSize = memoize(function (firstByte) {
firstByte = firstByte.toString(2);
if (firstByte.slice(0, -5) === '110') {
return 2;
}
else if (firstByte.slice(0, -4) === '1110') {
return 3;
}
else if (firstByte.slice(0, -3) === '11110') {
return 4;
}
else if (firstByte.slice(0, -2) === '111110') {
return 5;
}
else if (firstByte.slice(0, -1) === '1111110') {
return 6;
}
else {
return NaN;
}
});
var getContent = function (stream) {
var stringbuffer = [];
var chunkSize = 1024;
var buffer = Ti.createBuffer({
type: Ti.Codec.CHARSET_UTF8,
length: chunkSize // bytes
});
var bytesRead;
var number, spy, firstByte, sequence, toread;
var cutout;
var append;
var stringvalue;
var temp;
while ((bytesRead = stream.read(buffer)) >= 0) {
append = null;
cutout = 0;
if (bytesRead < chunkSize) {
cutout = chunkSize - bytesRead;
}
else {
spy = null;
firstByte = null;
toread = 0;
number = 1;
while ((spy = buffer[chunkSize - number]) >= 128) {
firstByte = spy;
number += 1;
if (spy >= 192) {
break;
}
}
number -= 1;
if (number > 0) {
sequence = firstByteToSequenceSize(firstByte);
toread = sequence - number;
if (toread < 0) {
throw new Error("Malformed UTF-8");
}
if (toread > 0) {
cutout = number;
temp = Ti.createBuffer({
type: Ti.Codec.CHARSET_UTF8,
length: sequence
});
temp.copy(buffer, 0, chunkSize - number, number);
stream.read(temp, number, toread);
append = Ti.Codec.decodeString({
source: temp,
type: Ti.Codec.CHARSET_UTF8
});
temp.release();
temp = null;
}
}
}
if (cutout > 0) {
buffer.fill(0, chunkSize - cutout, cutout);
}
stringvalue = Ti.Codec.decodeString({
source: buffer,
type: Ti.Codec.CHARSET_UTF8
});
if (cutout > 0) {
stringvalue = stringvalue.slice(0, -cutout);
}
if (append) {
stringbuffer.push(stringvalue, append);
}
else {
stringbuffer.push(stringvalue);
}
}
var content = stringbuffer.join('');
buffer.release();
stream.close();
return content;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment