Re: Getting Applet into Flash ROM?
Mark Wolforth <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.tini |
|---|---|
| Message-ID | <[email protected]> |
Hi Anguel,
Good point. Converting the original binary data to a string can have a
few pitfalls. Java does lots of fancy character set conversions to make
internationalization easy, but these can mess things up when all you
want is the raw data. I went through a lot of trial and error to find
something that worked. For example, you can do without the try{}catch{}
block around the string declaration I posted before - it was left over
from attempts to use different Java character encoding.
What I did in the end was use the default encoding and escape the
characters that don't exist. A code snippet for doing this, where "out"
is some output stream and "in" is the input stream would be:
c = in.read();
while (c != -1) {
// Things that need to be escaped
if (c == '"') {
out.write ('\\');
out.write ('"');
} else if (c == '\\') {
out.write ('\\');
out.write ('\\');
} else if (c == '\n') {
out.write ('\\');
out.write ('n');
} else if (c == '\r') {
out.write ('\\');
out.write ('r');
} else {
// Escape characters that don't fit in
// ISO8859-1 encoding.
if (((c & 0x0FF) >= 128) && ((c & 0x0FF) <= 159)) {
out.write ('\\');
out.write ('u');
out.write ('0');
out.write ('0');
temp = (c & 0x0F0) >> 4;
if (temp < 10) {
out.write (temp + '0');
} else {
out.write (temp - 10 + 'A');
}
temp = c & 0x0F;
if (temp < 10) {
out.write (temp + '0');
} else {
out.write (temp - 10 + 'A');
}
} else {
out.write ((byte)(c & 0x0FF));
}
}
c = in.read();
}
There may be a better way to do it, but the above has worked fine for me
for a while.
Mark.
zer0flag wrote:
> Hi Mark!
>
> Thank you very much for your help! I like this approach! Maybe it's
> not as elegant and efficient as the solution in assembler, but the
> filesystem is built only once at startup so it should be enough for
> most of us.
> I just wanted to ask you how you put the string into Java. Just copy
> and paste from a text editor or do I have to make some special
> preparations? What about encoding? Thanks once again for your help!
>
> Best regards,
> Anguel
>
> Mark Wolforth wrote:
>
>>
>> Hi,
>>
>> I've done this entirely in Java by storing the data as a string and
>> then using the getBytes() method to extract the bytes back. A String
>> is the most data efficient way to do this in Java since all other
>> array types add code to set up the array itself. As an example, you
>> could have a class like:
>>
>> class Web_applet {
>> public byte[] JARfile;
>> public web_applet() {
>> try {
>> JARfile = "-Þd;o³õÃݶ¡¸Ç6ï´ÍÀ»l'bض\u0".getBytes();
>> } catch (Throwable _) {}
>> }
>> }
>>
>> and then extract the data with something like:
>>
>> private static final void CreateAppletJAR() {
>> FileOutputStream jarfile;
>> try {
>> jarfile = new FileOutputStream (new File("Applet.jar"));
>> Web_applet applet = new Web_applet();
>> jarfile.write (applet.JARfile, 0, applet.JARfile.length);
>> jarfile.close();
>> } catch (Throwable t) {
>> System.out.println ("Error creating web applet JAR file.");
>> System.out.println (t);
>> }
>> } // End of CreateAppletJAR()
>>
>> Watch out for the 64K class limit size. If you hit up against this
>> you'll need to create multiple classes to hold your data.
>>
>> Good luck,
>> Mark.
>>
>>
>> _______________________________________________
>> TINI mailing list
>> TINI-6tN4nzCoH/[email protected]
>> To UNSUBSCRIBE, edit your profile, or see list archives:
>> http://lists.dalsemi.com/mailman/listinfo/tini
>>
>>
>
_______________________________________________
TINI mailing list
TINI-6tN4nzCoH/[email protected]
To UNSUBSCRIBE, edit your profile, or see list archives:
http://lists.dalsemi.com/mailman/listinfo/tini