Parsing and encapsulating java source code of IS08583 packet protocol package

Source: Internet
Author: User

I. Introduction to IS08583:
 
The ISO8583 package (8583 for short) is an international standard package format, which consists of up to 128 field fields. Each domain has a unified specification and has a fixed length and a variable length.
The first section of the 8583 package is a bitmap to determine the composition of the field fields of the package. The bitmap is the soul of the 8583 package. It is the key to package and unpackage to determine the field fields. Understanding the attributes of each field is the basis for filling in data.
 
1: bitmap description:
 
Location: 8583 bits in the 1st package
Format: Fixed Length
Type: blob (Binary 16-bit, 16*8 = 128bit)
Description:
For example, if the first bitmap is set to '1', the extended Bitmap (128 fields) is used; otherwise, only the basic Bitmap (64 fields) is used ).
If a data field is used, set the corresponding bit to '1' In the bitmap. If the 41 field is used, set the 41 bit of the bitmap to '1 '.
Selection Conditions: If 65 to 128 fields are used, the first bitmap field must be set to '1'
2: domain definition:
Typedef struct ISO8583
{
Int bit_flag;/* Field Data Type 0 -- string, 1 -- int, 2 -- binary */
Char * data_name;/* domain name */
Int length;/* data domain length */
Int length_in_byte;/* actual length (if it is longer )*/
Int variable_flag;/* indicates whether the variable length is 0: NO, 2 digits become longer, and 3 digits become longer */
Int datatyp;/* 0 -- string, 1 -- int, 2 -- binary */
Char * data;/* store specific values */
Int attribute;/* Reserved */
} ISO8583;
 
Ii. Defining BitMap classes
 
Class description: defines the BitMap class to store the information of each domain according to the domain definition of the ISO8583 package. For example:
 
Package com. lottery. pos. model;
 
Public class BitMap {
Private int bit; // bit
Private int bittype; // data type 1 ascii 2 binary
Private int variable; // whether the length is longer. 0 is not 2. Two digits become longer. 3. Three digits become longer.
Private int len; // Data Length
Private byte [] dat; // data
 
Public int getBit (){
Return bit;
}
Public void setBit (int bit ){
This. bit = bit;
}
Public int getBittype (){
Return bittype;
}
Public void setBittype (int bittype ){
This. bittype = bittype;
}
Public int getVariable (){
Return variable;
}
Public void setVariable (int variable ){
This. variable = variable;
}
Public byte [] getDat (){
Return dat;
}
Public void setDat (byte [] dat ){
This. dat = dat;
}
Public int getLen (){
Return len;
}
Public void setLen (int len ){
This. len = len;
}
 
 
}
 
3. Define the PortConfig class
 
Class description: defines the configuration information class. Parse and encapsulate data based on this type. For example:
 
Package com. lottery. pos. model;
 
Public class PortConfig {
/**
* Stores the configuration information of all interfaces.
* [] [0] bit: bit in Map
* [] [1] type: 1 ascii 2 binary
* [] [2] len Length: (valid for fixed length)
* [] [3] varLen Variable Length: 0 non-variable length 2-bit variable length 3-bit Variable Length
*/
// Define a two-digit array to store configuration information.
Public static final int [] [] config = {
{11,1, 6,0 },
{12,1, 6,0 },
{13, 1, 4, 0 },
{32, 1, 11, 0 },
{37,1, 12,0 },
{39,1, 2, 0 },
{40, 2, 50, 2 },
},
{48, 1, 52,3 },
{, 2 },
};
 
}
 
4. Define the BitMapiso class
 
Class description: This class provides two methods to parse the request package and encapsulate the information package, for example:
 
Package com. lottery. pos. utils;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import com. lottery. pos. model. BitMap;
 
Public class BitMapiso {
 
/**
* Parse request packets
* @ Param body
* @ Param config
* @ Return List
*/
@ SuppressWarnings ("unchecked ")
Public static List unpackRequest (byte [] body, int [] [] config ){
List outList = new ArrayList ();
// Obtain package information other than the information type. That is, the initial position of the bitmap.
Byte [] realbody = new byte [body. length-4];
System. arraycopy (body, 4, realbody, 0, realbody. length );
// Obtain the bitmap
Byte [] map = null;
Byte [] map8 = new byte [8];
System. arraycopy (realbody, 0, map8, 0, 8 );
Boolean [] bmap8 = LoUtils. getBinaryFromByte (map8 );
If (bmap8 [1]) {
// If the first digit is 1, it is an extensible bitmap with a length of 16 bytes.
Map = new byte [16];
System. arraycopy (realbody, 0, map, 0, 16 );
} Else {
Map = map8;
}
Boolean [] bmap = LoUtils. getBinaryFromByte (map );
 
Int tmplen = map. length;
For (int I = 2; I <bmap. length; I ++ ){
If (bmap [I]) {
// BitMap bitMap = null;
// Search for the data corresponding to 1 in the bitmap
Int bit =-1;
For (int j = 0; j <config. length; j ++ ){
If (config [j] [0] = I ){
Bit = j;
Break;
}
}
BitMap outBitMap = new BitMap ();
OutBitMap. setBit (I );
OutBitMap. setBittype (config [bit] [1]);
// Len is useless for variable length.
OutBitMap. setLen (config [bit] [2]);
OutBitMap. setVariable (config [bit] [3]);
Byte [] nextData = null;
If (config [bit] [3]> 0 ){
// Retrieve the value of the variable-length part.
Int varLen = config [bit] [3];
If (config [bit] [1] = 2 ){
VarLen = varLen-1;
}
Byte [] varValue = new byte [varLen];
System. arraycopy (realbody, tmplen, varValue, 0, varValue. length );
Int datLen = 0;
If (config [bit] [1] = 2 ){
DatLen = LoUtils. bcdToint (varValue );
} Else {
DatLen = byteToInt (varValue );
}
 
Tmplen + = varLen;
// Extract the value of the variable part.
NextData = new byte [datLen];
 
System. arraycopy (realbody, tmplen, nextData, 0, nextData. length );
Tmplen + = nextData. length;
} Else {
NextData = new byte [config [bit] [2];
System. arraycopy (realbody, tmplen, nextData, 0, nextData. length );
Tmplen + = config [bit] [2];
}
OutBitMap. setDat (nextData );
OutList. add (outBitMap );
}
}
 
Return outList;
}
 
/**
* Package the response package, excluding the Message Type
* @ Param list
* @ Return byte []
*/
@ SuppressWarnings ("unchecked ")
Public static byte [] PackResponse (List list ){
Int len = 16;
For (int I = 0; I <list. size (); I ++ ){
BitMap bitMap = (BitMap) list. get (I );
// Calculate the total length of the Request package
If (bitMap. getBittype () = 2 ){
If (bitMap. getVariable ()> 0 ){
Len + = bitMap. getVariable ()-1 + bitMap. getDat (). length;
} Else {
Len + = bitMap. getVariable () + bitMap. getDat (). length;
}
} Else {
Len + = bitMap. getVariable () + bitMap. getDat (). length;
}
}
Byte [] body = new byte [len];
// Bitmap
Boolean [] bbitMap = new boolean [129];
BbitMap [1] = true;
Int temp = (bbitMap. length-1)/8;
For (int j = 0; j <list. size (); j ++ ){
BitMap bitMap = (BitMap) list. get (j );
BbitMap [bitMap. getBit ()] = true;
Byte [] bitmap = LoUtils. getByteFromBinary (bbitMap );
System. arraycopy (bitmap, 0, body, 0, bitmap. length );
// Data
If (bitMap. getVariable ()> 0 ){
// The data is variable-length: the variable-length value.
Byte [] varValue = null;
If (bitMap. getBittype () = 2 ){
VarValue = LoUtils. StrToBCDBytes (String. format ("% 0" + bitMap. getVariable () + "d", bitMap. getDat (). length ));
} Else {
VarValue = String. format ("% 0" + bitMap. getVariable () + "d", bitMap. getDat (). length). getBytes ();
}
System. arraycopy (varValue, 0, body, temp, varValue. length );
Temp + = varValue. length;
// The value of the number after the variable-length part.
System. arraycopy (bitMap. getDat (), 0, body, temp, bitMap. getDat (). length );
Temp + = bitMap. getDat (). length;
} Else {
// The data is of a fixed length.
Byte dat [] = new byte [bitMap. getLen ()];
If (bitMap. getDat (). length! = BitMap. getLen ()){
System. arraycopy (bitMap. getDat (), 0, dat, 0, bitMap. getLen ());
} Else {
Dat = bitMap. getDat ();
}
System. arraycopy (dat, 0, body, temp, dat. length );
Temp + = bitMap. getDat (). length;
}
}
Return body;
}
 
}

Author: ERDP Technical Architecture"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.