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.packager;
020
021import org.jpos.iso.ISOComponent;
022import org.jpos.iso.ISOException;
023import org.jpos.iso.ISOMsg;
024import org.jpos.iso.ISOPackager;
025
026import java.io.*;
027
028/** {@link ISOPackager} that round-trips messages via Java serialization. */
029public class NativePackager implements ISOPackager {
030    /** Default constructor; no instance state to initialise. */
031    public NativePackager() {}
032    @Override
033    public byte[] pack(ISOComponent c) throws ISOException {
034        ByteArrayOutputStream baos = new ByteArrayOutputStream();
035        try {
036            if (c instanceof ISOMsg) {
037                ISOMsg m = (ISOMsg)c;
038                ISOPackager p = m.getPackager();
039                m.setPackager(null);
040                ObjectOutputStream os = new ObjectOutputStream(baos);
041                ((Externalizable)c).writeExternal(os);
042                os.flush();
043                m.setPackager(p);
044            }
045        } catch (IOException e) {
046            throw new ISOException (e);
047        }
048        return baos.toByteArray();
049    }
050
051    @Override
052    public int unpack(ISOComponent m, byte[] b) throws ISOException {
053        ByteArrayInputStream bais = new ByteArrayInputStream(b);
054        if (m instanceof Externalizable) {
055            try {
056                unpack (m, bais);
057            } catch (IOException e) {
058                throw new ISOException (e);
059            }
060        }
061        return b.length - bais.available();
062    }
063
064    @Override
065    public void unpack(ISOComponent m, InputStream in) throws IOException, ISOException {
066        try {
067            if (m instanceof Externalizable) {
068                ObjectInputStream is = org.jpos.util.Serializer.createAllowListObjectInputStream(in, "org.jpos.iso.");
069                ((Externalizable) m).readExternal(is);
070            }
071        } catch (Exception e) {
072            throw new ISOException (e);
073        }
074    }
075
076    @Override
077    public String getDescription() {
078        return getClass().getName();
079    }
080
081    @Override
082    public String getFieldDescription(ISOComponent m, int fldNumber) {
083        return null;
084    }
085
086    @Override
087    public ISOMsg createISOMsg() {
088        return new ISOMsg();
089    }
090}
091