Thursday, September 22, 2011

Creating Binary Arrays

Lately I have been using PeopleCode to manipulate binary files: moving files, copying files, and even creating zip files. A prerequisite for reading from and writing to binary files is the basic binary array -- the buffer. My blog post Base64 Encoding for PeopleSoft demonstrated a very complicated method for creating binary files that worked with PeopleTools 8.49 and earlier, but does not work on my PeopleTools 8.51 systems. While studying PeopleBooks I found a much easier, well documented method for creating binary arrays:

Local JavaObject &bytes = CreateJavaArray("byte[]", 1024 /* length of array */);

For arrays with known values at construction time, you can use the CreateJavaObject function:

Local JavaObject &bytes = CreateJavaObject("byte[]", 5, 10, 15, 20);

Note: Since this is documented, I suspect these functions will work with PeopleTools 8.49 and earlier, but I haven't tested them on earlier PeopleTools versions. If this method won't work in PeopleTools 8.49 or earlier, then you are welcome to use the alternative:

REM ** get a reference to a Java class instance for the primitive byte array;
Local JavaObject &arrayClass = GetJavaClass("java.lang.reflect.Array");
Local JavaObject &bytes = &arrayClass.newInstance(GetJavaClass("java.lang.Byte").TYPE, 5);

I would like to call out Kris who posted a comment on Base64 Encoding for PeopleSoft stating that the older method no longer worked. I happened to be working on zipping files with PeopleCode the week before Kris's comment and discovered the same issue and resolution. Very timely.

4 comments:

trybowski said...

Hey Jim,
I am wondering if you know the solution to the following case. I'd like to fetch column names from an SQL query (not tables, but query, including generated columns). I was thinking about using getMetaData() method, but here's a problem: how do I convert a PeopleSoft object, rowset, to java ResultSet? Or should I approach it differently? I wouldn't like to create any additional Java classes, a pure-peoplecode solution would be perfect.

Jim Marion said...

@trybowski, when you say SQL query, I assume you are referring to the SQL object or an SQLExec, not the PeopleSoft Query object. If this is the case, then, yes, getting column information for an SQL object or SQLExec is difficult. I'm not sure if it is even possible. If I were approaching this task, I would use PeopleSoft query to design my SQL and PeopleCode to get at the information. The PeopleCode API includes a query object that will give you all the meta-data you desire as well as the results. I used the Query API once for a "Favorite Queries" pagelet. The pagelet listed the user's favorite queries, and when clicked, would show the user the first 10 rows.

jb said...

Jim, is there a way for me to interpret what bits are set in binary field using PeopleCode? For example, if I have a binary field with a value of 33, bits 1 and 6 are 1, all others are 0. I can do this in SQL but I am looking for a simple Java function to use in PeopleCode. There doesn't appear to be a PC function...

Jim Marion said...

@jb, for bitwise operations? Java has operators for that. I am not sure about Java functions for this.