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 * Base implementation of {@link org.jpos.iso.ISOHeader} providing common header handling for ISO-8583 channels.
029 * @author <a href="mailto:Eoin.Flood@orbiscom.com">Eoin Flood</a>
030 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a>
031 */
032public class BaseHeader implements ISOHeader, Loggeable {
033    /**
034     * 
035     */
036    private static final long serialVersionUID = 8674535007934468935L;
037    /** Raw header bytes. */
038    protected byte[] header;
039    transient boolean asciiEncoding = false;
040
041    /**
042     * Default Constructor.
043     * Used by Class.forName.newInstance(...);
044     */
045    public BaseHeader()
046    {
047        header = null;
048    }
049
050    /** Creates a BaseHeader wrapping the given raw bytes.
051     * @param header raw header bytes
052     */
053    public BaseHeader (byte[] header) {
054        unpack(header);
055    }
056
057    public Object clone()
058    {
059        try {
060            BaseHeader h = (BaseHeader) super.clone();
061            if (this.header != null)
062                h.header = this.header.clone();
063            return h;
064        } catch (CloneNotSupportedException e) {
065            throw new InternalError();
066        }
067    }
068
069    public byte[] pack() {
070        return header != null ? header.clone() : null;
071    }
072
073    public int unpack (byte[] header) {
074        if (header != null) {
075            this.header = header.clone();
076            return header.length;
077        } else {
078            this.header = null;
079        }
080        return 0;
081    }
082
083    public int getLength () {
084        return header != null ? header.length : 0;
085    }
086
087    public void setDestination(String dst) {}
088    public void setSource(String src) {}
089    public String getDestination() { return null; }
090    public String getSource() { return null; }
091    public void swapDirection() {}
092
093    public void dump (PrintStream p, String indent) {
094        if (header != null) {
095            p.println (
096                indent
097              + "<header>" + ISOUtil.hexString (header) + "</header>"
098            );
099        }
100    }
101    /**
102     * Sets the encoding for source/destination fields.
103     * @param asciiEncoding true for ASCII, false for BCD
104     */
105    public void setAsciiEncoding(boolean asciiEncoding) {
106        this.asciiEncoding = asciiEncoding;
107    }
108    /**
109     * Returns true if ASCII encoding is used for source/destination fields.
110     * @return true if ASCII encoding is active
111     */
112    public boolean isAsciiEncoding() {
113        return asciiEncoding;
114    }
115}