/*
 * 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 turgen.tapeimage;

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

/**
 * FSK Chunk
 * 
 */
public class FSKChunk implements TapeImageChunk {
    
    /** Chunk type*/
    private static final String type="fsk ";
    /** Chunk length*/
    private int length;
    /** Chunk data*/
    private int[] data;
    /** Auxiliary value - IRG length*/
    private int aux;
    
    
    public String getType() {
        return type;
    }

    public int getLength() {
        return length;
    }

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

    public boolean isGeneretedUsingParent() {
        return false;
    }

    public TapeImageChunk getParent() {
        return null;
    }

    public void readFromStream(InputStream s) throws Exception {
        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 fsk chunk header");
        }
        
        length = lengthLo + 256*lengthHi;
        aux = auxLo+256*auxHi;
        
        data = new int[length];
        for(int i=0;i<length;i++) {
            data[i]=s.read();
            if (data[i]==-1) {
                throw new FileFormatException("Truncated fsk chunk data");
            }
        }
            
    }

    public void writeToStream(OutputStream s) throws Exception {
        
    }
    
    public int getAux() {
        return aux;
    }
    
    public String toString() {
        return "fsk: ["+Integer.toString(aux)+" ms] ("+Integer.toString(length)+")";
    }

}
