ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to do java casting in RPG

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to do java casting in RPG

    How do I do casting in RPG?

    Im trying to read a fileinputstream. Im using some example code that uses casting to convert from int to char. I dont know how to set up the prototype for this.

    I have the stringbuffer and append method prototyped, but Im coming short with the int to char conversion.
    I have the section highlighted (i think). All the other java prototypeing has been handled with the exception of the int->char conversion

    //create file object
    File file = new File("/tmp/xlstest.xlsx");

    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;

    try
    {

    fin = new FileInputStream(file);

    while( (ch = fin.read()) != -1)
    strContent.append((char)ch);


    fin.close();

    }

  • #2
    Re: How to do java casting in RPG

    The RPG prototype for the append method should be the one for the char parameter (a UCS-2 parameter in RPG). Then you can use an RPG data structure to do the "casting". The data structure would have a 1C subfield with a 5i subfield overlaying it. Assign your integer to the 5i subfield, and pass the 1C subfield as the parameter.

    But using that exact code is going to perform terribly in RPG since it involves two Java method calls for each byte of the file.

    And it might not even be correct in all cases, if there are any double-byte characters in the file. This would create two (wrong) characters from those two bytes.

    The documentation for FileInputStream says this: "FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader."

    Since your code is converting each byte to a character, it doesn't sound like it is dealing with raw bytes. Maybe you should consider something like FileReader that understands text.

    The read method for the FileReader class returns a integer representing a character, not an integer representing a byte. You would use the same RPG data structure to convert the integer to a UCS2 character. That won't make your code any more efficient, but it I think it will make it correct.

    But reading and appending one character at a time will involve way too many Java method calls from RPG. Seems like it would be better to use something like the ReadLine method of BufferedReader that will handle whole lines of text at a time.



    That class says "Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient."

    Comment


    • #3
      Re: How to do java casting in RPG

      Thank you for your reply.

      The filereader is kinda what I need(I think). Ive created a POI excel file, that Im ...trying...(making feeble attempts) to import into an jquery ajax response. I need it in binary form. I trying to get it to display in the browser.

      It's the only thing I can think of that might work. If you know of an easier way to do this, I need all the suggestions I can get. Im the most novice of novices at this. I hope this makes sence, since I have no idea what Im talking about.

      Do you have an example of the prototype that you have mentioned
      Last edited by davisty; May 10, 2013, 08:20 AM. Reason: Left something out

      Comment


      • #4
        Re: How to do java casting in RPG

        All that code seems to do is call Java methods to read data from the IFS into a variable. Is there a reason you want to call Java to do this instead of using the native, built-in IFS APIs? I promise you that the latter will perform dramatically better. (Probably by at least two orders of magnitude)

        Comment


        • #5
          Re: How to do java casting in RPG

          To be honest. I ddint even think about the native ifs methods. Ive had java on the brain and it never even crossed my mind.

          Ive spent hours trying to figure this stuff out, and the solution was right in front of me ...

          Comment


          • #6
            Re: How to do java casting in RPG

            On the thread about the Java main method, you asked for more information about the casting in this thread. I can't reply there since the thread is closed, and really, it makes more sense to reply here anyway.

            Here's how you can define a data structure to "cast" a UCS-2 character as an short integer. It sets the ucs-2 character to 'a', then adds 1 to the short integer that overlays the ucs-2 character.

            Code:
            D ucs2_cvt        ds                  qualified
            D  char                          1c
            D  int                           5[B]u[/B] 0 overlay(char)
             * [B]Edit[/B]:Changed from 5i to 5u as suggested by Scott in the next post in this thread.
             * Also see my post a couple further down which explains why 5i was just plain wrong.)
             /free
                 // ucs2 'a' = x'0061'  = decimal 97
                 ucs2_cvt.char = 'a';
            
                 ucs2_cvt.int += 1;
                 // decimal 98 = x'0062' = ucs2 'b'
            
                 return;
            I think you were expecting something more complicated since you mentioned "offsets" in the other thread. But this simply overlays one subfield with another subfield. So they are sharing the same storage, but the different data types cause the storage to be interpreted in different ways.
            Last edited by Barbara Morris; June 5, 2013, 03:29 PM. Reason: Correct 5i to 5u. See comment in the code for more info.

            Comment


            • #7
              Re: How to do java casting in RPG

              Consider using "5u 0" instead of "5i 0" in this situation. I don't think you need to allow negative numbers...

              Comment


              • #8
                Re: How to do java casting in RPG

                Thank You VERY MUCH for this. It is VERY appreciated.

                I still get kinda confused about all the different java types and how to "cast" them to RPG types.

                Comment


                • #9
                  Re: How to do java casting in RPG

                  Thanx Scott.

                  Will the lack of a sign will still take to the two bytes for the 1c. I wont have a format problem?

                  I dont know, Im asking.

                  Comment


                  • #10
                    Re: How to do java casting in RPG

                    Yes, 5u 0 is still two bytes.

                    Comment


                    • #11
                      Re: How to do java casting in RPG

                      Thank You, VERY MUCH.

                      I hope your back is ok now ...

                      Comment


                      • #12
                        Re: How to do java casting in RPG

                        I suffered a spinal cord injury that left me with improper nerve connections from the waist down. (aka a paraplegic). At first I was completely paralyzed (unable to move) from the waist down, but I have been recovering/rehabilitating. The process is expected to take 18 months or so, but I have been making great progress.

                        Nothing wrong with my back, though :-)

                        Comment


                        • #13
                          Re: How to do java casting in RPG

                          Scott, I used an integer because the original post in this thread was about passing a Java char (RPG UCS-2) to a method that needed a Java int.

                          But (sorry to mislead you, davisty) that wasn't a good reason for using a 5i in the RPG overlay. If the UCS-2 character has a hex value with a 1 in the high-order bit, such as x'A27B', and that value is then assigned to a 4-byte integer parameter for the Java method that needs the int, it should have the value x'0000A27B'. That can only happen if the 2-byte overlay is defined as unsigned. If the overlay it is defined as signed, the 4-byte integer would have the value x'FFFFA27B'.

                          Comment


                          • #14
                            Re: How to do java casting in RPG

                            Hey, I appreciate ALL your help. We do the best we can, and your help has been invaluable.

                            Im still a novice at this point but your help has enabled me to get a handle on some of the points I didnt quite grasp.

                            Thank You very much for ALL your help

                            Comment


                            • #15
                              Re: How to do java casting in RPG

                              That is truly wonderful to hear. I will continue my prayers for you ...

                              Comment

                              Working...
                              X