×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jul 2003
    Location
    Finland, Tampere
    Posts
    1,113

    Using localized String constants (no Unicode)

    As I understand every String constant in any MIDlet is in Unicode, i.e. uses at least two bytes per character. One byte for actual character code and one byte for language/alphabet codepage.

    However, when using resource bundles, I already know which codepage should be used for each String constant. I don't want to spend lots of extra bytes to tell my program information that I already know.

    I can imagine only two ways of saving these extra bytes: either store String constants as byte arrays and convert them to String in RunTime or to store them in external resource once again in 1 byte encoding (at least without language specific byte).

    Both ways seem to be a little tricky.
    Does anybody use on of these of some third approach?
    Could you give me soem simple template of your code?
    This could greatly save JAR space in text-intensive applicatons.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Doc,

    You can store Strings as byte arrays to save memory, but they will have to go into a resource file (in the jar) to save jar space. Forgive me if I tell you something you already well know, but an array definition in code like:

    byte [] ao = { 65, 66, 67, 68, 69, 70 };

    compiles to code that creates an array, then inserts values into the array one by one, (as integers, then converted to bytes, I think), so you have about six bytes of code for each byte!! It's not like doing the same think in a C program, where the array is simply stored preinitialised in the executable image.

    I don't have an alternative, but maybe storing text in the jar is not so tricky...

    I store all my text in a simple ascii file in the jar, then read it into a Vector of String objects (could easily be an array instead). A set of constants allow me to identify each item from the text resource, so you end up replacing:

    item.setLabel ("Fish");

    with

    item.setLabel (textResource [Text_FISH]);

    I end up with a large number of constants! These take space too, so I shove all my Java through a C preprocessor before compilation; that way I can use C-style header files, and #define all my numeric constants. That way, they take no more space than a literal.

    Actually, I say "ascii file", it's UTF-8 as its purpose is to simplify translating the software to other languages. A file in ISO-8859-1 would be smaller, and easier to convert to unicode, if it fits your needs. (Any other 8-bit code will need a translation table). The version of Notepad with Windows 2000 (and presumably with XP, though I haven't checked) will edit files in ascii, UTF-8, unicode (with bytes in both orders), and what it describes as "ANSI", which I think is close enough to ISO-8859-1. It has an annoying habbit of prefixing the first line in the file with '\ufeff'.

    Any help?

    Graham.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Location
    Finland, Tampere
    Posts
    1,113
    Hi, graham
    Thanx for your opinion

    Forgive me if I tell you something you already well know
    No need to be sorry. Those who are too cool and proud, don't appear here and don't ask questions

    array definition in code like... compiles to code that creates an array
    Yep, that's one more reason to change something in this schema.

    I don't have an alternative, but maybe storing text in the jar is not so tricky...
    Sure, it isn't, but.. you know, I'd like to store only ascii strings in external file. Just because I can easily edit them with the favourite text editor. I would like chinese or cyrillic text stay in the code, just in the form translation company provides it, not to touch it at all.
    There can be other issues that are not evident to me when I'm just theoretically thinking..

    Actually I wanted someone to share his working code for templating, it's simpler then design everything by myself

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    So, are you actually just after some code to read an ASCII file into a String array (or similar?)

    Is your ASCII text all English?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×