Skip to content

Commit

Permalink
Update ISO8583Utils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoxingyun authored Sep 11, 2019
1 parent ced1182 commit afe53d6
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions common-utils/src/main/java/com/xy/util/ISO8583Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xy.util;



import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -11,14 +13,13 @@
*/
public class ISO8583Utils {


/**
* 打包
*
* @param isoMessageMap
* @return
*/
public static byte[] pack(HashMap<String, String> isoMessageMap) {
public static byte[] pack(Map<String, String> isoMessageMap) {
ISO8583 iso8583 = new ISO8583(isoMessageMap);
try {
iso8583.pack();
Expand All @@ -34,7 +35,7 @@ public static byte[] pack(HashMap<String, String> isoMessageMap) {
* @param isoMessageData
* @return
*/
public static HashMap<String, String> unpack(byte[] isoMessageData) {
public static Map<String, String> unpack(byte[] isoMessageData) {

ISO8583 iso8583 = new ISO8583(isoMessageData);

Expand Down Expand Up @@ -68,13 +69,13 @@ class ISO8583 {
/**
* ISO8583报文域数据
*/
private HashMap<String, String> ISO_MESSAGE_MAP;
private Map<String, String> ISO_MESSAGE_MAP;
/**
* ISO8583报文数据
*/
private byte[] ISO_MESSAGE_DATA;

public ISO8583(HashMap<String, String> isoMessageMap) {
public ISO8583(Map<String, String> isoMessageMap) {
this.ISO_MESSAGE_MAP = isoMessageMap;
}

Expand All @@ -83,7 +84,7 @@ public ISO8583(byte[] isoMessageData) {
}


public HashMap<String, String> getISO_MESSAGE_MAP() {
public Map<String, String> getISO_MESSAGE_MAP() {
return ISO_MESSAGE_MAP;
}

Expand Down Expand Up @@ -178,7 +179,26 @@ public void unpack() throws Exception {
}

} else if (IsoFieldSchema.IsoLengthType.LLVAR.equals(lengthType)) {
throw new RuntimeException("not support field type!");

if (IsoFieldSchema.IsoFieldType.N.equals(fieldType)) {
byte[] fieldLengthBytes = new byte[1];
System.arraycopy(ISO_MESSAGE_DATA, index, fieldLengthBytes, 0, 1);
index += 1;
fieldLength = Integer.valueOf(bcd2str(fieldLengthBytes));

int byteTempLength = fieldLength / 2;
if (fieldLength % 2 != 0) {
byteTempLength = byteTempLength + 1;
}

fieldBytes = new byte[byteTempLength];
System.arraycopy(ISO_MESSAGE_DATA, index, fieldBytes, 0, byteTempLength);
index += byteTempLength;
fieldValue = bcd2str(fieldBytes).substring(0, fieldLength);

} else {
throw new RuntimeException("not support field type!");
}
} else if (IsoFieldSchema.IsoLengthType.LLLVAR.equals(lengthType)) {

if (IsoFieldSchema.IsoFieldType.N.equals(fieldType)) {
Expand Down Expand Up @@ -334,7 +354,7 @@ private static void writeFieldByType(ByteArrayOutputStream baos, IsoFieldSchema.
String lengthFormat = "%0" + lengthType.getLength() + "d";
baos.write(str2bcd_l(String.format(lengthFormat, fieldValue.length())));
}
baos.write(str2bcd_l(fieldValue));
baos.write(str2bcd_r(fieldValue));
} else if (fieldType.equals(IsoFieldSchema.IsoFieldType.B)) {
if (lengthType.equals(IsoFieldSchema.IsoLengthType.FIXED)) {
baos.write(hex2bytes(fieldValue));
Expand Down

0 comments on commit afe53d6

Please sign in to comment.