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; 020 021public class IsoFieldHeaderFormatter { 022 023 private boolean tagFirst; 024 025 public IsoFieldHeaderFormatter(boolean tagFirst) { 026 this.tagFirst = tagFirst; 027 } 028 029 030 public boolean isTagFirst() { 031 return tagFirst; 032 } 033 034 /** 035 * 036 * @param tagPrefixer the tag part prefixer 037 * @param lengthPrefixer the length part prefixer 038 * @param tagData byte array containing the tag value bytes 039 * @param lengthData byte array containing the length value bytes 040 * @return byte array containing the header (tag and length), size of returned bytes is just the header length 041 */ 042 public byte[] format(final Prefixer tagPrefixer, final Prefixer lengthPrefixer, final byte[] tagData, final byte[] lengthData) { 043 if (tagPrefixer != null && lengthPrefixer != null && tagData != null && lengthData != null) { 044 final byte[] destinationData = new byte[tagPrefixer.getPackedLength() + lengthPrefixer.getPackedLength()]; 045 format(tagPrefixer, lengthPrefixer, tagData, lengthData, destinationData); 046 return destinationData; 047 } 048 return null; 049 } 050 051 public void format(final Prefixer tagPrefixer, final Prefixer lengthPrefixer, final byte[] tagData, final byte[] lengthData, final byte[] destinationData) { 052 if (tagPrefixer != null && lengthPrefixer != null && tagData != null && lengthData != null && destinationData != null) { 053 System.arraycopy(tagData, 0, destinationData, tagFirst ? 0 : lengthPrefixer.getPackedLength(), tagPrefixer.getPackedLength()); 054 System.arraycopy(lengthData, 0, destinationData, tagFirst ? tagPrefixer.getPackedLength() : 0, lengthPrefixer.getPackedLength()); 055 } 056 } 057 058 public int getTagIndex(Prefixer lengthPrefixer) { 059 return tagFirst ? 0 : lengthPrefixer.getPackedLength(); 060 } 061 062 public int getLengthIndex(Prefixer tagPrefixer) { 063 return tagFirst ? tagPrefixer.getPackedLength() : 0; 064 } 065 066 public static IsoFieldHeaderFormatter TAG_FIRST = new IsoFieldHeaderFormatter(true); 067 public static IsoFieldHeaderFormatter LENGTH_FIRST = new IsoFieldHeaderFormatter(false); 068 069 public int getTotalLength(final Prefixer tagPrefixer, final Prefixer prefixer) { 070 if (tagPrefixer == null || prefixer == null) { 071 throw new IllegalArgumentException("Neither tag or length prefixer was provided."); 072 } 073 return tagPrefixer.getPackedLength() + prefixer.getPackedLength(); 074 } 075}