import java.io.*;

/*
Reading SGY-file for text header (3200 byte)
 */

public class TextHeader extends Header {

    private byte[] headerByBytes = new byte[3200];
    private char[] headerNewEncoding;

    public TextHeader(File f) { setFile(f); }

    public String getHeader() {
        StringBuilder sb = new StringBuilder();
        try {
            RandomAccessFile stream = new RandomAccessFile(getFile(), "r");

            for (int k = 0; k < 3200; k++)
                headerByBytes[k] = stream.readByte();

            //if (UTF-8) == 3200 -> use UTF-8, else use CP-1047 (EBCDIC)
            headerNewEncoding = new String(headerByBytes, "utf8").toCharArray();
            if (headerNewEncoding.length != 3200)
                headerNewEncoding = new String(headerByBytes, "cp1047").toCharArray();

            //40 lines, one line = 80 characters
            for (int line = 0; line < 40; line++) {
                for (int chars = line * 80; chars < line * 80 + 80; chars++)
                    sb.append(headerNewEncoding[chars]);
                sb.append("\n");
            }
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}