utf8 encode/decode in java
You might want to encode/decode strings in java but are unable to use conventional classes. This could be for example due to a restrictive environment like google web toolkit (gwt). In this case, you can use use these two methods:
private static String byte2str(byte[] utfarray) {
StringBuilder sb = new StringBuilder();
int i = 0;
int c=0, c2=0, c3 = 0;
while (i < utfarray.length) {
c = (0xFF & utfarray[i]);
if (c < 128) {
sb.append((char)c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = (0xFF & utfarray[i + 1]);
sb.append((char)(((c & 31) << 6) | (c2 & 63)));
i += 2;
} else {
c2 = (0xFF & utfarray[i + 1]);
c3 = (0xFF & utfarray[i + 2]);
sb.append((char)(((c & 15) << 12)
| ((c2 & 63) << 6) | (c3 & 63)));
i += 3;
}
}
return sb.toString();
}
private static byte[] str2byte(String string) {
IndependentByteStream bos = new IndependentByteStream(string.length());
for ( int n = 0; n < string.length(); n++) {
char c = string.charAt(n);
if (c < 128) {
bos.write((byte) c);
} else if ((c > 127) && (c < 2048)) {
bos.write((byte) ((c >> 6) | 192));
bos.write((byte) ((c & 63) | 128));
} else {
bos.write((byte) ((c >> 12) | 224));
bos.write((byte) (((c >> 6) & 63) | 128));
bos.write((byte) ((c & 63) | 128));
}
}
return bos.toByteArray();
}
Ubuntu: import CA certificate as trusted
Using mumble and want to import your own CA certificate as trusted?
sudo cp /tmp/myCoolOwnCaCert.pem /etc/ssl/certs/
Google-web-tookit (gwt): Deserialize JSON array
To deserialize a JSON array you can use an Overlay Type. However, those examples return objects instead of arrays. Here an example of how this is case is handled:
JSON:
[{"age": 42, "name": "arthur"}, {"age": 23, "name": "adam"}]
public class PersonRecord extends JavaScriptObject{
protected PersonRecord() {
}
public final native String getName() /*-{ return this.name; }-*/;
public final native int getAge() /*-{ return this.age; }-*/;
}
public class Deserialize {
public static final native JsArray<PersonRecord> deserialize(
String json) /*-{
return eval('(' + json + ')');
}-*/;
}
JavaScript HashSet: objects in associative arrays
As it turns out, associative arrays can’t be used as a cheap hashSET surrogate. Consider the following source:
var hashset = {};
var obj1 = new Object();
obj1['foo'] = 42;
hashset[obj] = true;
for(key in hashset)
alert(key.foo);
This won’t work for a simple reason: Associative arrays only support simple types as key. Obj1 will be casted to string as soon as you use it as an index. Thus key.foo is inaccessible because strings don’t have a property “foo”. I ended up with a quick and dirty solution which should require O(1)/O(log n). Depends on the javascript implementation of associative arrays.
var _hashSetSufix = 0;
function HashSet() {
this.data = {};
this.counter = 0;
this.objAttr = '_hashsetCounter_' + (_hashSetSufix++);
}
HashSet.prototype.add = function(obj) {
obj[this.objAttr] = this.counter++;
this.data[obj[this.objAttr]] = obj;
};
HashSet.prototype.contains = function(obj) {
return this.objAttr in obj && obj[this.objAttr] in this.data;
};
HashSet.prototype.remove = function(obj) {
delete this.data[obj[this.objAttr]];
delete obj[this.objAttr];
};
// test
(function() {
var hs = new HashSet();
var obj1 = new Object();
var obj2 = new Object();
hs.add(obj1);
if (!hs.contains(obj1) || hs.contains(obj2))
alert("Hashset: selftest fail");
hs.remove(obj1);
if (hs.contains(obj1) || hs.contains(obj2))
alert("Hashset: selftest fail2");
})();
Flash HTC Desire S HBOOT 2.00.0002
- Download ROM, place as .zip on sd card
- Unlock bootloader: www.htcdev.com/bootloader/unlock-instructions/
- Get in fastboot mode, boot cwm:
./fastboot boot /tmp/recovery-clockwork-5.0.2.0-saga.img
- Wipe
- Install zips
- Reboot
- If stuck at loading screen: flash boot.img by hand
./fastboot flash boot /tmp/boot.img
ArrayBuffer <-> Blob <-> BinaryString conversion
You can speed up your javascript encryption stuff by using ArrayBuffers. However, if you want to convert it back to a binary string for other API calls, it’s really slow. At least if you do it by hand using something like
for(var i=0; i< view.length; ++i)
str += String.fromCharCode(view[i]);
Of course you can somewhat improve it by using a stringbuilder approach, like
function convertFaster(view)
{
var array = new Array(view.length);
for(var i=0; i< view.length; ++i)
array[i] = String.fromCharCode(view[i]);
return array.join('');
}
However, there is a much faster way using the
FileReader API. Although its name implies only usage for files, it takes a Blob as an argument. Since every File is also a Blob, this makes it possible to read Files. But this also means, we can feed it with custom blobs. To make use of this we can convert any ArrayBuffer to a blob by using the brand new
BlobBuilder
var buffer = new ArrayBuffer(3)
var array = new Uint8Array(buffer);
var i=0;
array[i++] = 0x48;
array[i++] = 0x69;
array[i++] = 0x21;
var blobBuilder = new BlobBuilder();
blobBuilder.append(buffer);
buffer = null;
var blob = blobBuilder.getBlob();
var fileReader =new FileReader();
fileReader.onload = function (e) {
//conversion done
};
fileReader.readAsBinaryString(blob);
To convert a binary string to a blob we can again use the BlobBuilder. Append also accept DOMStrings as argument.
Last but not least a nice performance comparison (ArrayBuffer to BinaryString): click me.
Some results with 5000000 bytes on a Core2Duo 6300:
- Firefox 9.0.1: 884ms vs 89ms
- Chromium 14.0.835.202: 1886ms vs 101ms
Conclusion: Speedup between 10x and 20x :)
JavaScript: benchmark implications on sparse arrays
I tried to benchmark an encryption function by passing an empty array to it:
var data = new Array(10* 1024* 1024);
var start = (new Date).getTime();
rc4.secureEncrypt([1, 2, 3, 4, 5, 6,7,8,9,0], data);
var diff = (new Date).getTime() - start;
alert("spent "+diff+" ms on encryption");
It was slow, but I didn’t know why. I gradually debugged the code and discovered, that 90% of the time was spent in that line:
plantext[i] = encrypted;
Why should accessing a simple array take that long? It’s an O(1) operation! Well, it turns out that the array is a sparse array. Since most of it will never be accessed, it’s simply empty (empty != 0) and is created on first access.
Just filling the array caused the function to be executed 3 to 4 times faster. The encryption itself turned out to be pretty fast, it was just a bad benchmark testing input.
var data = new Array(10* 1024* 1024);
for(var i=0; i < data.length; ++i) data[i]=42;
var start = (new Date).getTime();
rc4.secureEncrypt([1, 2, 3, 4, 5, 6,7,8,9,0], data);
var diff = (new Date).getTime() - start;
alert("spent "+diff+" ms on encryption");
Filter useless wireless traffic
!(wlan.fc.type_subtype == 0x1d) && !(wlan.sa == $MY_ADDRESS) && !(wlan.fc.type_subtype == 0x08)
0x1d = beacon
0x08 = ack
by maik
stringToBytes JavaScript Performance
Searching for a simple function to convert javascript strings to byte arrays, I stumbled over this stackoverflow answer.
While it’s implementation is correct, it has a significant performance problem. For large strings, the internal byte array is recreated too often. It’s somewhat similar to using the += operator on strings in java instead of using a StringBuilder. A faster function is below.
function stringToBytesFaster ( str ) {
var ch, st, re = [], j=0;
for (var i = 0; i < str.length; i++ ) {
ch = str.charCodeAt(i);
if(ch < 127)
{
re[j++] = ch & 0xFF;
}
else
{
st = []; // clear stack
do {
st.push( ch & 0xFF ); // push byte to stack
ch = ch >> 8; // shift value down by 1 byte
}
while ( ch );
// add stack contents to result
// done because chars have "wrong" endianness
st = st.reverse();
for(var k=0;k<st.length; ++k)
re[j++] = st[k];
}
}
// return an array of bytes
return re;
}
To round up things, you can start a performance test here:
clickme
Example output on Firefox 8, core2duo and strlength = 10000:
Old: 2021ms
New: 4ms
Speedup factor:505.25