codificação de textos

Olá, estou criando um pequeno software para aparelhos CLDC1.0 e MIDP1.0. Como há uma quantidade considerável de textos nesse software, criei arquivos txt que são lidos durante a execução. E aí surge um problema.

Se os arquivos txt são gravados em ANSI, aparelhos nokia os lêem normalmente, mas motorola não, apresentando caracteres acentuados incorretamente. Já em UTF-8 acontece o contrário…

Imagino que a resposta seja “não”, mas há alguma forma “universal” para gravar/ler esses textos? Caso não, há alguma função que eu possa usar para converter ANSI para UTF-8 ou vice-versa, como o utf8_encode do php?

Mais uma questão relacionada: uso esses arquivos externos assumindo que isso irá reduzir o consumo de memória de execução no aparelho, carregando os textos apenas no momento em que vou usá-los. Estou certo? Ou incorporar esses textos no código geraria o mesmo resultado?

Encontrei a saída em um site, usando funções de conversão da codificação dos caracters. Daí foi só fazer um teste simples com um arquivo padronizado e ao que parece está tudo ok. O código é o seguinte:

[code]/*

Author : Shivakumar
Mail : shiva (at) blisspark.com
Disclaimer : This code is provided without any implied or expressed warranty and may not work as
expected. If have any bugs, inform me or post the fix here.

*/

public static String UTF8Decode(byte in[], int offset, int length)
{
StringBuffer buff = new StringBuffer();
int max = offset + length;
for( int i = offset ; i < max ; i++)
{
char c = 0;
if((in[i] & 0x80) == 0)
{
c = (char) in[i];
}
else if(( in[i] & 0xe0 ) == 0xc0) // 11100000
{
c |= ((in[i] & 0x1f) << 6); // 00011111
i++;
c |= ((in[i] & 0x3f) << 0); // 00111111
}
else if(( in[i] & 0xf0) == 0xe0) // 11110000
{
c |= ((in[i] & 0x0f) << 12); // 00001111
i++;
c |= ((in[i] & 0x3f) << 6); // 00111111
i++;
c |= ((in[i] & 0x3f) << 0); // 00111111
}
else if((in[i] & 0xf8) == 0xf0) // 11111000
{
c |= ((in[i] & 0x07) << 18); // 00000111 (move 18, not 16?)
i++;
c |= ((in[i] & 0x3f) << 12); // 00111111
i++;
c |= ((in[i] & 0x3f) << 6); // 00111111
i++;
c |= ((in[i] & 0x3f) << 0); // 00111111
}
else
{
c = ‘?’;
}
buff.append(c);
}
return buff.toString();
}

public static byte[] UTF8Encode(String str)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
int strlen = str.length();

	for&#40; int i = 0 ; i &lt; strlen ; i++ &#41;
	&#123;
		char t = str.charAt&#40;i&#41;;
		int c = 0;
		c |= &#40; t &amp; 0xffff &#41;;

		if&#40;c &gt;= 0 &amp;&amp; c &lt; 0x80&#41;
		&#123;
			bos.write&#40;&#40;byte&#41;&#40; c &amp; 0xff &#41;&#41;;
		&#125;
		else if&#40;c &gt; 0x7f &amp;&amp; c &lt; 0x800&#41;
		&#123;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 6 &#41; &amp; 0x1f &#41; | 0xc0 &#41;&#41;;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 0 &#41; &amp; 0x3f &#41; | 0x80 &#41;&#41;;
		&#125;
		else if&#40;c &gt; 0x7ff &amp;&amp; c &lt; 0x10000&#41;
		&#123;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 12 &#41; &amp; 0x0f &#41; | 0xe0 &#41;&#41;; // &lt;-- correction &#40;mb&#41;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 6 &#41; &amp; 0x3f &#41; | 0x80 &#41;&#41;;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 0 &#41; &amp; 0x3f &#41; | 0x80 &#41;&#41;;
		&#125;
		else if&#40;c &gt; 0x00ffff &amp;&amp; c &lt; 0xfffff&#41;
		&#123;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 18 &#41; &amp; 0x07 &#41; | 0xf0 &#41;&#41;;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 12 &#41; &amp; 0x3f &#41; | 0x80 &#41;&#41;;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 6 &#41; &amp; 0x3f &#41; | 0x80 &#41;&#41;;
			bos.write&#40; &#40;byte&#41; &#40;&#40;&#40; c &gt;&gt;&gt; 0 &#41; &amp; 0x3f &#41; | 0x80 &#41;&#41;;
		&#125;
	&#125;
	bos.flush&#40;&#41;;
&#125;
catch&#40;Exception e&#41;
&#123;
&#125;
return bos.toByteArray&#40;&#41;;

}
[/code]

A função de teste simples que criei foi:

[code]private void checkUTF8() {
// loading the check file that must have only an ã char and saved as UTF-8

    // var
    StringBuffer buffer = null;
    InputStream is = null;
    InputStreamReader isr = null;

    // loading the file
    try &#123;
        Class c = this.getClass&#40;&#41;;
        is = c.getResourceAsStream&#40;&quot;/data/check.txt&quot;&#41;;
        if &#40;is == null&#41; &#123;
            throw new Exception&#40;&quot;The file check.txt does not exist!&quot;&#41;;
        &#125;
        isr = new InputStreamReader&#40;is&#41;;
        buffer = new StringBuffer&#40;&#41;;
        int ch;
        while &#40;&#40;ch = isr.read&#40;&#41;&#41; &gt; -1&#41; &#123;
            buffer.append&#40;&#40;char&#41; ch&#41;;
        &#125;
        if &#40;isr != null&#41; &#123;
            isr.close&#40;&#41;;
        &#125;
    &#125; catch &#40;Exception ex&#41; &#123;
        System.out.println&#40;ex.toString&#40;&#41;&#41;;
    &#125;

    // now that the text is loaded, comparing the char read
    if &#40;buffer.toString&#40;&#41; == &quot;ã&quot;&#41; &#123;
        // this system reads texts as UTF-8
        this.useUTF8 = true;
    &#125; else &#123;
        // this system does not read as UTF-8
        this.useUTF8 = false;
    &#125;
&#125;[/code]

O código foi retirado de http://www.j2meforums.com/wiki/index.php/UTF-8_Encoder/Decoder

Mas ainda fica a questão sobre o uso desses arquivos externos. Ele é mesmo justificável pelos motivos que coloquei aí em cima?