/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mgatelabs.bytemapper.support.tags;

import com.mgatelabs.bytemapper.support.fields.types.AbstractBaseType;
import com.mgatelabs.bytemapper.support.instances.FieldSelfInstance;
import com.mgatelabs.bytemapper.support.instances.IdentityInstance;
import com.mgatelabs.bytemapper.support.interfaces.TagInterface;
import com.mgatelabs.bytemapper.support.io.streams.LimitedInputStream;
import com.mgatelabs.bytemapper.util.BMStreamUtils;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

/**
 *
 * @author MiniMegaton
 */
public class SimpleTagInstance implements TagInterface {

    private final Class reference;
    private AbstractBaseType type;
    private FieldSelfInstance instance;
    private final IdentityInstance identity;

    public SimpleTagInstance(Class reference, AbstractBaseType type, IdentityInstance identity) {
        this.reference = reference;
        this.type = type;
        this.identity = identity;
        instance = new FieldSelfInstance(this.type);
    }
    
    public AbstractBaseType getType() {
        return type;
    }

    public void setType(AbstractBaseType type) {
        this.type = type;
    }

    public FieldSelfInstance getInstance() {
        return instance;
    }

    public void setInstance(FieldSelfInstance instance) {
        this.instance = instance;
    }
    
    @Override
    public IdentityInstance getIdentity() {
        return identity;
    }
    
    @Override
    public Object readContent(LimitedInputStream lir, boolean isNullable) throws Exception {
        final int contentSize; 
        if (isNullable) {
            contentSize = BMStreamUtils.readNullableSize(lir);
            if (contentSize == -1) {
                return null;
            }
        } else {
            contentSize = BMStreamUtils.readSize(lir);
        }

        type.read(lir.spawn(contentSize), instance, instance);
        
        return instance.getObjectValue(instance);
    }

    @Override
    public void writeContent(OutputStream os, Object target, boolean isNullable) throws Exception {        
        if (target == null && isNullable) {
            BMStreamUtils.writeNullableSize(os);
        } else {
            // Update the instances value
            instance.setObjectValue(target, target);
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
            
            type.write(baos, instance, target);
            if (isNullable) {
                BMStreamUtils.writeNullableSize(os, baos.size(), false);
            } else {
                BMStreamUtils.writeSize(os, baos.size());
            }
            os.write(baos.toByteArray());
            baos.reset();
        }
    }

    @Override
    public Class getClassReference() {
        return reference;
    }
    
    @Override
    public Object getClassInstance() throws Exception {
        return reference.newInstance();
    }
}

@@ -1769,38 +1769,17 @@
         
-if (isNullable) %7B%0A    
+%0A
         
@@ -1833,28 +1833,24 @@
 r);%0A        
-    
 if (contentS
@@ -1858,28 +1858,24 @@
 ze == -1) %7B%0A
-    
             
@@ -1891,94 +1891,8 @@
 ll;%0A
-            %7D%0A        %7D else %7B%0A            contentSize = BMStreamUtils.readSize(lir);%0A
     
@@ -2506,42 +2506,8 @@
 t);%0A
-            if (isNullable) %7B%0A    
     
@@ -2575,101 +2575,8 @@
 e);%0A
-            %7D else %7B%0A                BMStreamUtils.writeSize(os, baos.size());%0A            %7D%0A
     

Update SimpleTagInstance.java