package com.fsryan.forsuredb.api.adapter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;

public interface FSSerializer {
    /**
     * <p>
     *
     * </p>
     * @return true if your serializer serializes best to a byte array. Otherwise, return false
     */
    boolean storeAsBlob();
    String createStringDoc(Type type, Object val);
    byte[] createBlobDoc(Type type, Object val);
    <T> T fromStorage(Type typeOfT, byte[] objectBytes);
    <T> T fromStorage(Type typeOfT, String stringRepresentation);

    /**
     * <p>
     *     The default serializer. You should never rely upon this, but it may be able to
     *     serialize your java objects properly
     * </p>
     */
    FSSerializer JAVA_SERIALIZABLE_SERIALIZER = new FSSerializer() {
        @Override
        public boolean storeAsBlob() {
            return true;
        }

        @Override
        public String createStringDoc(Type type, Object val) {
            return new String(createBlobDoc(type, val));
        }

        @Override
        public byte[] createBlobDoc(Type type, Object val) {
            ObjectOutputStream out = null;
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            byte[] outBytes = null;
            try {
                out = new ObjectOutputStream(byteStream);
                out.writeObject(outBytes);
                outBytes = byteStream.toByteArray();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException ioe2) {}
                }
                try {
                    byteStream.close();
                } catch (IOException ioe2) {}
            }
            return outBytes == null ? new byte[0] : outBytes;
        }
    }
}

@@ -40,109 +40,8 @@
 r;%0A%0A
-import java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.ObjectOutputStream;%0A
 impo
@@ -67,16 +67,16 @@
 t.Type;%0A
+
 %0Apublic 
@@ -112,38 +112,8 @@
 /**%0A
-     * %3Cp%3E%0A     *%0A     * %3C/p%3E%0A
     
@@ -392,24 +392,24 @@
 jectBytes);%0A
+
     %3CT%3E T fr
@@ -465,1371 +465,7 @@
 on);
-%0A%0A    /**%0A     * %3Cp%3E%0A     *     The default serializer. You should never rely upon this, but it may be able to%0A     *     serialize your java objects properly%0A     * %3C/p%3E%0A     */%0A    FSSerializer JAVA_SERIALIZABLE_SERIALIZER = new FSSerializer() %7B%0A        @Override%0A        public boolean storeAsBlob() %7B%0A            return true;%0A        %7D%0A%0A        @Override%0A        public String createStringDoc(Type type, Object val) %7B%0A            return new String(createBlobDoc(type, val));%0A        %7D%0A%0A        @Override%0A        public byte%5B%5D createBlobDoc(Type type, Object val) %7B%0A            ObjectOutputStream out = null;%0A            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();%0A            byte%5B%5D outBytes = null;%0A            try %7B%0A                out = new ObjectOutputStream(byteStream);%0A                out.writeObject(outBytes);%0A                outBytes = byteStream.toByteArray();%0A            %7D catch (IOException ioe) %7B%0A                ioe.printStackTrace();%0A            %7D finally %7B%0A                if (out != null) %7B%0A                    try %7B%0A                        out.close();%0A                    %7D catch (IOException ioe2) %7B%7D%0A                %7D%0A                try %7B%0A                    byteStream.close();%0A                %7D catch (IOException ioe2) %7B%7D%0A            %7D%0A            return outBytes == null ? new byte%5B0%5D : outBytes;%0A        %7D%0A    %7D
 %0A%7D%0A

remove the java serializer default