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 028public class NativePackager implements ISOPackager { 029 @Override 030 public byte[] pack(ISOComponent c) throws ISOException { 031 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 032 try { 033 if (c instanceof ISOMsg) { 034 ISOMsg m = (ISOMsg)c; 035 ISOPackager p = m.getPackager(); 036 m.setPackager(null); 037 ObjectOutputStream os = new ObjectOutputStream(baos); 038 ((Externalizable)c).writeExternal(os); 039 os.flush(); 040 m.setPackager(p); 041 } 042 } catch (IOException e) { 043 throw new ISOException (e); 044 } 045 return baos.toByteArray(); 046 } 047 048 @Override 049 public int unpack(ISOComponent m, byte[] b) throws ISOException { 050 ByteArrayInputStream bais = new ByteArrayInputStream(b); 051 if (m instanceof Externalizable) { 052 try { 053 unpack (m, bais); 054 } catch (IOException e) { 055 throw new ISOException (e); 056 } 057 } 058 return b.length - bais.available(); 059 } 060 061 @Override 062 public void unpack(ISOComponent m, InputStream in) throws IOException, ISOException { 063 try { 064 if (m instanceof Externalizable) { 065 ObjectInputStream is = new ObjectInputStream(in); 066 ((Externalizable) m).readExternal(is); 067 } 068 } catch (Exception e) { 069 throw new ISOException (e); 070 } 071 } 072 073 @Override 074 public String getDescription() { 075 return getClass().getName(); 076 } 077 078 @Override 079 public String getFieldDescription(ISOComponent m, int fldNumber) { 080 return null; 081 } 082 083 @Override 084 public ISOMsg createISOMsg() { 085 return new ISOMsg(); 086 } 087} 088