Skip to main content

alphaWorks  >  Forums  >  IBM JZOS Batch Toolkit for z/OS SDKs  >  developerWorks

ZOS having encoding problems with Properties    Point your RSS reader here for a feed of the latest messages in this thread


     

 
 

My developerWorks
 Welcome, Guest
Sign in or register
Permlink Replies: 2 - Pages: 1 - Last Post: Sep 25, 2009 4:04 PM Last Post By: arsi Threads: [ Previous | Next ]
arsi

Posts: 14
Registered: Aug 12, 2009 12:34:08 PM
ZOS having encoding problems with Properties
Posted: Sep 25, 2009 11:10:38 AM
Click to report abuse...   Click to reply to this thread Reply
Hello,

I want to create a Properties file (if not found) and load it with default entries. So far, I am able to create it and load it but its contents is not ebcdic. Looks like some encoding problem but I have tried using JVM option -Dfile.encoding=ISO8859-1, JZOS option export JZOS_OUTPUT_ENCODING=Cp1047 and also below coding .... but all have same results.

String enc = ZUtil.getDefaultPlatformEncoding();
zProp.put(new String("A1".getBytes(), enc),
new String("B2".getBytes(), enc));

Any ideas!. Open for suggestions.
Regards
Ulises

String zDD = ZFile.allocDummyDDName();
String zName = "TEST.PROP";
ZFile.bpxwdyn("alloc fi(" + zDD + ") da(" + zName + ") new reuse recfm(f,b) dsorg(ps) lrecl(256) catalog msg(wtp)");
.
.
ZFile props = new ZFile("//DD:" + zDD, "w+b,type=record,noseek");
Prooperties zProp = new Properties();
zProp.load(props.getInputStream());
zProp.put("A1", "B2");
zProp.list(System.out);
zProp.store(props.getOutputStream(), "default properties");
props.close();

Program output:
A1=B2

Contents of ZOS TEST.PROP file.
000000 23646566 61756C74 2070726F 70657274 6965730A 23467269 20536570 20323520 ..../.%....?....................
000020 31343A33 353A3433 20474D54 2B30303A 30302032 3030390A 41313D42 320A0000 ..........(.....................

KirkWolf

Posts: 195
Registered: Jan 17, 2006 03:13:58 PM
Re: ZOS having encoding problems with Properties
Posted: Sep 25, 2009 12:23:48 PM   in response to: arsi in response to: arsi's post
Click to report abuse...   Click to reply to this thread Reply
First, Java Properties files are always stored in ISO8859-1. That's the specification, and the Properties.load() and store() methods will use that encoding. (Refer to the javadoc for java.util.Properties)

Second, Java Properties objects (in memory), are always java.lang.String (in Unicode). So, it doesn't make sense to try to convert the String key and value to some other encoding when you are trying to create the Properties object.

The following code will only serve to create a garbage String:

new String("A1".getBytes(), ZUtil.getDefaultPlatformEncoding())

  • first, the String "A1" (in Unicode) is converted to bytes in the default file encoding (ISO8859-1)
  • next, you create a String from ISO8859-1 bytes, but using IBM-1047 (EBCDIC) encoding.

If you really want to dump properties to a dataset in EBCDIC from a Properties object, you will have to use the Properties.store() method to store ISO8859-1 bytes onto a Stream and then convert that stream to EBCDIC. Loading properties from EBCDIC will have to reverse the process.

Something like:

Properties props = new Properties();
props.put("A", "B");String zDD = ZFile.allocDummyDDName();
 
ZFile.bpxwdyn("alloc fi(" + zDD + ") da(" + zName + ") new reuse recfm(f,b) dsorg(ps) lrecl(256) catalog msg(wtp)");
BufferedWriter writer = FileFactory.newBufferedWriter("//DD:" + zDD);
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    props.store(bos, "example properties");
    String propertiesFileAsString = new String(bos.toByteArray(), "ISO8859-1");
    writer.append(propertiesFileAsString);
} finally {
    try { 
        writer.close();
    } catch(Exception ignore) {}
    ZFile.bpxwdyn("free fi(" + zDD + ") msg(wtp)");
}
arsi

Posts: 14
Registered: Aug 12, 2009 12:34:08 PM
Re: ZOS having encoding problems with Properties
Posted: Sep 25, 2009 04:04:57 PM   in response to: KirkWolf in response to: KirkWolf's post
Click to report abuse...   Click to reply to this thread Reply
Great! Thanks for the code. It work!

Point your RSS reader here for a feed of the latest messages in all forums