JavaFormatting Messages in Java to Communicate with Legacy Systems, Part 2

Formatting Messages in Java to Communicate with Legacy Systems, Part 2 content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


In Part 1 of our tutorial, we began discussing how legacy systems usually expect messages in a rigid format, such as EBCDIC (Extended Binary Coded Decimal Interchange Code), and how such messages can be built in Java. Here in Part 2, we’ll continue this discussion, with more detail on the BuildMsg.processRequest() and
BuildMsg.getMsg() methods, and generating an EBCDIC byte stream using “cp500” encoding.

BuildMsg Class

The BuildMsg class has two critical methods namely processRequest() and getMsg(). The processRequest method constructs the message. The getMsg method returns the message as an EBCDIC byte array.

BuildMsg.processRequest() method

The processRequest method accepts a parameter of the type InputData and uses it to construct the message. The testMsg StringBuffer will hold the message that is built. The set methods for each data item return a string that is appended to the testMsg StringBuffer.

The set methods for each data item (like setDi1 ) will take care of the details of setting up that particular data item. They will also set a flag to indicate whether the data item exists. These flags are used by the method inpDiDefine.setPBitmapFromFlags() to set the bitmap. The bitmap for the message is built dynamically for every message based on the flags.

Note the following line in the processRequest method.

pBitmap[i] = (byte)( inpDiDefine.intPBitmap[i] ) ;

This is where the integer value is converted to a byte, and thus the bitmap is set. Then the bitmap is converted to a string and inserted at the beginning of the testMsg buffer.

Listing 3.

public class BuildMsg {
/* For EBCDIC conversion */
private String encoding= "cp500" ;
StringBuffer testMsg ;
int i ;
int maxBitmapArray = 2 ;
private byte[] pBitmap ;
private int msgBuildFail ;
private DiDefine inpDiDefine ;
public BuildMsg()
throws Exception
inpDiDefine = new DiDefine() ;
msgBuildFail = 0 ;
public void processRequest(InputData inp)
throws Exception
testMsg = new StringBuffer() ;
// Initialize the Bitmap
byte[] pBitmap = new byte[2] ;
for ( i = 0 ; i < maxBitmapArray; i++ )
pBitmap[i] = 0 ;
testMsg.append( setDi1( inp ) );
testMsg.append( setDi2( inp ) );
/* Note the bitmaps need to be set after all the Di elements are set.
inpDiDefine.setPBitmapFromFlags() ;
for ( i = 0 ; i < maxBitmapArray ; i++ ){
pBitmap[i] = (byte)( inpDiDefine.intPBitmap[i] ) ; // Converted to bytes
String pBmpStr = new String(pBitmap , encoding ) ;
testMsg.insert( 0 , pBmpStr ) ;

The setDi1 method called by the processRequest method is shown below. Note that it sets the flag Di1Exists to true. In case of an error, the msgBuildFail is set to false. This method returns a string that will be appended to the testMsg StringBuffer in the processRequest method. There is a similar method for every single data item. Implementing in this way will help us to isolate the details of each data item in a separate method.

Listing 4.

public String setDi1( InputData inp)
int di1Len = 6 ;
StringBuffer tempBuf = new StringBuffer() ;
tempBuf.append( inp.getDi1() ) ;
if ( tempBuf.length() != di1Len )
System.out.println("*** Warning setDi1: length ="
+ tempBuf.length()
+ " Expected = "
+ di1Len
) ;
msgBuildFail = 1 ;
return null ;
inpDeDefine.Di1Exists = true ;
return ( tempBuf.toString() ) ;

BuildMsg.getMsg() method

The getMsg method of the BuildMsg class is called to return a byte stream of the message to be sent to the legacy system. “cp500” encoding is used to generate an EBCDIC byte stream from the testMsg String. This will only be needed if the legacy system expects an EBCDIC message.

Listing 5

public byte[] getMsg() throws Exception{
String tempString = new String( testMsg.toString() ) ;
return( tempString.getBytes(encoding) ) ;

DiDefine class:

This class is a helper class used by the methods of the BuildMsg class. This class is used to test the flags and set the corresponding bits in the bitmap for each data item. Below is the section of code that sets the bitmap based on the flags checked. Note that in the method setPBitmapFromFlags() we are setting integers. They get converted to bytes in the processRequest method of the BuildMsg class.

If the di1 data item exists, then the first bit in the bitmap needs to be set. If the di1Exists flag is set then the intPBitmap is set to 128. The integer value 128 when converted to bytes is 10000000.

If the di1 and di2 data items exist, then the first bit and the second bit in the bitmap need to be set. In this case, the intPBitmap variable is set to 128 + 64. The integer value when converted to bytes is 11000000. Similar code exists for every single data item required.

Listing 6.

public static final int BITPOSN128 = 128;
public static final int BITPOSN64 = 64;
public static final int BITPOSN32 = 32;
public static final int BITPOSN16 = 16;
public static final int BITPOSN8 = 8;
public static final int BITPOSN4 = 4;
public static final int BITPOSN2 = 2;
public static final int BITPOSN1 = 1;
public int[] intPBitmap ;
boolean di1Exists ; // Flag for each data item
boolean di2Exists ;
* Method to set the bitmap array from flags
* @author: Sharath Sahadevan
* Date: Oct 00
public void setPBitmapFromFlags()
int i = 0 ;
for ( i = 0 ; i < 2; i ++ )
intPBitmap[i] = 0 ;
// Byte 0
if ( this.di1Exists )
intPBitmap[0] += BITPOSN128 ;
if ( this.di2Exists )
intPBitmap[0] += BITPOSN64 ;
if ( this.di3Exists )
intPBitmap[0] += BITPOSN32 ;
if ( this.di4Exists )
intPBitmap[0] += BITPOSN16 ;


This article sheds light on how a message can be built for a Java application to communicate with a legacy system. Some legacy systems expect their messages to be in the EBCDIC format. Java provides a simple way to generate an EBCDIC byte stream using “cp500” encoding. This document illustrates methods to construct bitmaps dynamically, depending on the input data. Similar ideas are used to interpret messages sent to a Java application by the legacy system. In the interests of keeping this article brief, those details are left for the reader to research separately.

About the Author

Sharath Sahadevan is a senior software engineer with MasterCard International in St. Louis. His team supports the MasterCard Settlement Account Management application. He has a bachelor’s degree in electrical and electronics engineering from P.S.G College of Technology, India (1990). When not working, he enjoys spending time with his family, as well as playing tennis, cricket, basketball, and chess.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories