package turgen.tapeimage;

import java.io.InputStream;
import java.io.OutputStream;
import turgen.FileFormatException;


public class FujiChunk implements TapeImageChunk {

    private static final String type = "FUJI";
    private int length;
    private int[] data;
    private int aux;


    public int getAux() {
        return aux;
    }


    public String getType() {
        return type;
    }

    public int getLength() {
        return length;
    }

    public int[] getData() {
        return data;
    }

    public boolean isGeneretedUsingParent() {
        return false;
    }

    public void readFromStream(InputStream s) throws Exception {

        /*Read length and AUX*/
        int lengthLo = s.read();
        int lengthHi = s.read();
        int auxLo=s.read();
        int auxHi=s.read();

        if (lengthLo==-1 || lengthHi==-1 || auxLo==-1 || auxHi==-1) {
            throw new FileFormatException("Truncated FUJI chunk header");
        }

        length = lengthLo + (256*lengthHi);
        aux = auxLo + (256*auxHi);
        
        /*Read data*/
        data = new int[length];
        for(int i=0;i<length;i++) {
            int b = s.read();
            if (b==-1) throw new FileFormatException("Truncated FUJI chunk data");
            data[i]=b;
        }
        
    }

    public void writeToStream(OutputStream s) throws Exception {
        
    }

    public String toString() {
        return "FUJI ("+Integer.toString(length)+")";
    }

    public TapeImageChunk getParent() {
        return null;
    }

    

}
