package com.lekebilen.quasseldroid.qtcomm.serializers;

import java.io.IOException;

import com.lekebilen.quasseldroid.qtcomm.DataStreamVersion;
import com.lekebilen.quasseldroid.qtcomm.QDataInputStream;
import com.lekebilen.quasseldroid.qtcomm.QDataOutputStream;
import com.lekebilen.quasseldroid.qtcomm.QMetaTypeSerializer;

public class QByteArray implements QMetaTypeSerializer<String> {

	@Override
	public String unserialize(QDataInputStream stream, DataStreamVersion version)
	throws IOException {
		int len = (int)stream.readUInt(32);
		if(len == 0xFFFFFFFF) {
			return new String();
		}
		byte data[] = new byte[len];
		stream.readFully(data);

		return new String(data, "UTF-8");
	}

	@Override
	public void serialize(QDataOutputStream stream, String data,
			DataStreamVersion version) throws IOException {
		//OOPS: Requires a byte buffer with array for writing
		//FIXME: ^ Make it work without hasArray()
		if(data==null){
			stream.writeUInt(0xFFFFFFFF, 32);
		}else{
			stream.writeUInt(data.getBytes().length, 32);
			stream.write(data.getBytes("UTF-8"));
		}
	}
}

@@ -385,16 +385,70 @@
 ring%3E %7B%0A
+%09static int buflen = -1;%0A%09static byte %5B%5D buf = null;%0A%09
 %0A%09@Overr
@@ -613,18 +613,16 @@
 FFFFFFF)
- %7B
 %0A%09%09%09retu
@@ -628,56 +628,82 @@
 urn 
-new String()
+%22%22
 ;%0A%09%09
-%7D%0A%09%09byte data%5B%5D = new byte%5Blen%5D;
+%09%0A%09%09if (len %3E buflen) %7B%0A%09%09%09buf = new byte%5Blen%5D;%0A%09%09%09buflen = len;%0A%09%09%7D
 %0A%09%09s
@@ -718,20 +718,27 @@
 adFully(
-data
+buf, 0, len
 );%0A%0A%09%09re
@@ -753,20 +753,27 @@
  String(
-data
+buf, 0, len
 , %22UTF-8
@@ -1076,40 +1076,71 @@
 %0A%09%09%09
-stream.writeUInt(data.getBytes()
+byte %5B%5D wbuf = data.getBytes(%22UTF-8%22);%0A%09%09%09stream.writeUInt(wbuf
 .len
@@ -1165,38 +1165,20 @@
 m.write(
-data.getBytes(%22UTF-8%22)
+wbuf
 );%0A%09%09%7D%0A%09

optimize qbytearray serializer too