001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso.header;
020
021import org.jpos.iso.ISOHeader;
022import org.jpos.iso.ISOUtil;
023import org.jpos.util.Loggeable;
024
025import java.io.PrintStream;
026
027/**
028 * @author <a href="mailto:Eoin.Flood@orbiscom.com">Eoin Flood</a>
029 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
030 */
031public class BaseHeader implements ISOHeader, Loggeable {
032    /**
033     * 
034     */
035    private static final long serialVersionUID = 8674535007934468935L;
036    protected byte[] header;
037    transient boolean asciiEncoding = false;
038
039    /**
040     * Default Constructor.
041     * Used by Class.forName.newInstance(...);
042     */
043    public BaseHeader()
044    {
045        header = null;
046    }
047
048    public BaseHeader (byte[] header) {
049        unpack(header);
050    }
051
052    public Object clone()
053    {
054        try {
055            BaseHeader h = (BaseHeader) super.clone();
056            if (this.header != null)
057                h.header = this.header.clone();
058            return h;
059        } catch (CloneNotSupportedException e) {
060            throw new InternalError();
061        }
062    }
063
064    public byte[] pack() {
065        return header != null ? header.clone() : null;
066    }
067
068    public int unpack (byte[] header) {
069        if (header != null) {
070            this.header = header.clone();
071            return header.length;
072        } else {
073            this.header = null;
074        }
075        return 0;
076    }
077
078    public int getLength () {
079        return header != null ? header.length : 0;
080    }
081
082    public void setDestination(String dst) {}
083    public void setSource(String src) {}
084    public String getDestination() { return null; }
085    public String getSource() { return null; }
086    public void swapDirection() {}
087
088    public void dump (PrintStream p, String indent) {
089        if (header != null) {
090            p.println (
091                indent
092              + "<header>" + ISOUtil.hexString (header) + "</header>"
093            );
094        }
095    }
096    public void setAsciiEncoding(boolean asciiEncoding) {
097        this.asciiEncoding = asciiEncoding;
098    }
099    public boolean isAsciiEncoding() {
100        return asciiEncoding;
101    }
102}