Changeset 33724


Ignore:
Timestamp:
2019-11-27T23:41:30+13:00 (4 years ago)
Author:
ak19
Message:
  1. A bugfix to Base64.decode(String s) to handle null strings returned from Base64.decode(byte[], ...) variant. 2. Adding a copy of the stringToHex function debugUnicodeString() to gli code.
Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/feedback/Base64.java

    r24915 r33724  
    720720    {   
    721721        byte[] bytes = s.getBytes();
     722        byte[] origBytes = bytes;
    722723        bytes = decode( bytes, 0, bytes.length );
    723        
     724       
     725        // if something went wrong base64 decoding the string, just return the original bytes
     726        if(bytes == null) {
     727            return origBytes;
     728        }
     729       
    724730        // Check to see if it's gzip-compressed
    725731        // GZIP Magic Two-Byte Number: 0x8b1f (35615)
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r32271 r33724  
    9595    }
    9696
     97    // Copied from GS3 main java code at GSDL3SRCHOME\src\java\org\greenstone/util\Misc.java
     98    // Debugging function to print a string's non-basic chars in hex, so stringToHex on all non-basic and non-printable ASCII
     99    // Dr Bainbridge said that printing anything with charCode over 128 in hex is okay, but I'd already made extra allowances for non-printable ASCII
     100    // Based on https://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java
     101    public static String debugUnicodeString(String str) {
     102      String result = "";
     103      for(int i = 0; i < str.length(); i++) {
     104            int charCode = str.codePointAt(i); // unicode codepoint / ASCII code
     105           
     106            // ASCII table: https://cdn.sparkfun.com/assets/home_page_posts/2/1/2/1/ascii_table_black.png
     107            // If the unicode character code pt is less than the ASCII code for space and greater than for tilda, let's display the char in hex (x0000 format)
     108            if((charCode >= 20 && charCode <= 126) || charCode == 9 || charCode == 10 || charCode == 13) { // space, tilda, TAB, LF, CR are printable, leave them in for XML element printing
     109                result += str.charAt(i);
     110            } else {
     111                result += "x{" + String.format("%04x", charCode) + "}"; // looks like: x{4-char-codepoint}
     112            }
     113      }
     114     
     115      return result;
     116    }
     117   
    97118    /**
    98119     * Handy function to display the list of calling functions by
Note: See TracChangeset for help on using the changeset viewer.