+
+ // Array types:
+ customTypes.put("[]", new CustomSerializer() {
+ @Override
+ protected String toString(Object value) {
+ String type = value.getClass().getCanonicalName();
+ type = type.substring(0, type.length() - 2); // remove the []
+
+ StringBuilder builder = new StringBuilder();
+ builder.append(type).append("\n");
+ try {
+ for (int i = 0; true; i++) {
+ Object item = Array.get(value, i);
+ // encode it normally if direct value
+ if (!SerialUtils.encode(builder, item)) {
+ try {
+ // use ZIP: if not
+ builder.append(new Exporter().append(item)
+ .toString(true));
+ } catch (NotSerializableException e) {
+ throw new UnknownFormatConversionException(e
+ .getMessage());
+ }
+ }
+ builder.append("\n");
+ }
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Done.
+ }
+
+ return builder.toString();
+ }
+
+ @Override
+ protected String getType() {
+ return "[]";
+ }
+
+ @Override
+ protected Object fromString(String content) throws IOException {
+ String[] tab = content.split("\n");
+
+ try {
+ Object array = Array.newInstance(
+ SerialUtils.getClass(tab[0]), tab.length - 1);
+ for (int i = 1; i < tab.length; i++) {
+ Object value = new Importer().read(tab[i]).getValue();
+ Array.set(array, i - 1, value);
+ }
+
+ return array;
+ } catch (Exception e) {
+ if (e instanceof IOException) {
+ throw (IOException) e;
+ }
+ throw new IOException(e.getMessage());
+ }
+ }
+ });
+
+ // Images (this is currently the only supported image type by default)
+ customTypes.put("java.awt.image.BufferedImage", new CustomSerializer() {
+ @Override
+ protected String toString(Object value) {
+ try {
+ return ImageUtils.toBase64((BufferedImage) value);
+ } catch (IOException e) {
+ throw new UnknownFormatConversionException(e.getMessage());
+ }
+ }
+
+ @Override
+ protected String getType() {
+ return "java.awt.image.BufferedImage";
+ }
+
+ @Override
+ protected Object fromString(String content) {
+ try {
+ return ImageUtils.fromBase64(content);
+ } catch (IOException e) {
+ throw new UnknownFormatConversionException(e.getMessage());
+ }
+ }
+ });
+ }
+
+ /**
+ * Create an empty object of the given type.
+ *
+ * @param type
+ * the object type (its class name)
+ *
+ * @return the new object
+ *
+ * @throws ClassNotFoundException
+ * if the class cannot be found
+ * @throws NoSuchMethodException
+ * if the given class is not compatible with this code
+ */
+ public static Object createObject(String type)
+ throws ClassNotFoundException, NoSuchMethodException {
+
+ try {
+ Class<?> clazz = getClass(type);
+ String className = clazz.getName();
+ Object[] args = null;
+ Constructor<?> ctor = null;
+ if (className.contains("$")) {
+ Object javaParent = createObject(className.substring(0,
+ className.lastIndexOf('$')));
+ args = new Object[] { javaParent };
+ ctor = clazz.getDeclaredConstructor(new Class[] { javaParent
+ .getClass() });
+ } else {
+ args = new Object[] {};
+ ctor = clazz.getDeclaredConstructor();
+ }
+
+ ctor.setAccessible(true);
+ return ctor.newInstance(args);
+ } catch (ClassNotFoundException e) {
+ throw e;
+ } catch (NoSuchMethodException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new NoSuchMethodException("Cannot instantiate: " + type);
+ }