Pasting a file into a Java String

A

Anguel Stankov

Hi all!

I am looking for a possibility to paste a whole Java file (.class or
..jar) into a Java String. This has to happen while coding my app, this
means before compilig it! Later, during runtime, the string will be
written into a new file in the filesystem.
The most easy solution would be to just copy and paste the class file
binary code from an editor into the String. But because the data is
binary this will probably not work (different encodings).
I am looking for any tips or ready programs to be able to convert the
original file into a format, that can be pasted into a String (and can
later be written from the String into a new file in the file system).
I need the whole thing because I am writing an app for a Java-enabled
embedded system (TINI) which has only ROM and RAM. The filesystem is
stored in RAM and is erased on reboot, so I have to recreate it from
my Java app which is loaded from the ROM.

Thank you very much for any help!

Best regards,
Anguel
 
R

Rogan Dawes

Anguel said:
Hi all!

I am looking for a possibility to paste a whole Java file (.class or
.jar) into a Java String. This has to happen while coding my app, this
means before compilig it! Later, during runtime, the string will be
written into a new file in the filesystem.
The most easy solution would be to just copy and paste the class file
binary code from an editor into the String. But because the data is
binary this will probably not work (different encodings).
I am looking for any tips or ready programs to be able to convert the
original file into a format, that can be pasted into a String (and can
later be written from the String into a new file in the file system).
I need the whole thing because I am writing an app for a Java-enabled
embedded system (TINI) which has only ROM and RAM. The filesystem is
stored in RAM and is erased on reboot, so I have to recreate it from
my Java app which is loaded from the ROM.

Thank you very much for any help!

Best regards,
Anguel

If you have to do this, you could Base64 encode the file, and decode it
before writing it out. Be aware that the size of the file will grow by
1/4th, if you do this.

Regards,

Rogan
 
R

Roedy Green

I am looking for a possibility to paste a whole Java file (.class or
.jar) into a Java String. This has to happen while coding my app, this
means before compilig it! Later, during runtime, the string will be
written into a new file in the filesystem.
The most easy solution would be to just copy and paste the class file
binary code from an editor into the String. But because the data is
binary this will probably not work (different encodings).
I am looking for any tips or ready programs to be able to convert the
original file into a format, that can be pasted into a String (and can
later be written from the String into a new file in the file system).
I need the whole thing because I am writing an app for a Java-enabled
embedded system (TINI) which has only ROM and RAM. The filesystem is
stored in RAM and is erased on reboot, so I have to recreate it from
my Java app which is loaded from the ROM.

I think you just want a way of making binary data printable.

see http://mindprod.com/jgloss/printable.html
e.g. base64
see http://mindprod.com/jgloss/base64.html
i.e. armouring for transport:
http://mindprod.com/jgloss/armouring.html

If you want to take arbitrary text and convert it to a Java program
with that text as a string literal see http://mindprod.com/quoter.html
e.g. turns " to \" , \ to \\, nl to \n and wraps the whole thing in
"...".
 
R

Rob Shepherd

Anguel said:
Hi all!

I am looking for a possibility to paste a whole Java file (.class or
.jar) into a Java String. This has to happen while coding my app, this
means before compilig it! Later, during runtime, the string will be
written into a new file in the filesystem.
The most easy solution would be to just copy and paste the class file
binary code from an editor into the String. But because the data is
binary this will probably not work (different encodings).
I am looking for any tips or ready programs to be able to convert the
original file into a format, that can be pasted into a String (and can
later be written from the String into a new file in the file system).
I need the whole thing because I am writing an app for a Java-enabled
embedded system (TINI) which has only ROM and RAM. The filesystem is
stored in RAM and is erased on reboot,


No it isn't. The RAM is battery backed and Filesystem areas are maintained across reboots
and power cycles. The heap areas are just garbage collected.
(ie reclaimed when dereferenced)

When you ftp files (.tini) etc they are stored in RAM. The VM and Libs are stored in ROM.
along with a bank7 application (Slush..)

I am inclined to ask why you need to hold a class/Jar inside a string [or some other data
structure] in another class to then write it out to the same file system?

You do realise primitive Classloader functionality is accessible in the Firmware libs?

Please tell more about your problem/requirements

Are you using slush? Or have you written your own Bank7 Application? which needs
*RockSolid* access to filesystem areas in case of FS error.

Rob
 
T

Thomas Weidenfeller

Anguel said:
I am looking for a possibility to paste a whole Java file (.class or
.jar) into a Java String.

A byte array would be a slightly better choice, but IMHO, the whole idea
doesn't make to much sense, see below.
I am looking for any tips or ready programs to be able to convert the
original file into a format, that can be pasted into a String (and can
later be written from the String into a new file in the file system).

A few lines of e.g. a perl script should do this, nicely embedded into
your build mechanism. But IMHO, the whole idea doesn't make to much
sense, see below.
I need the whole thing because I am writing an app for a Java-enabled
embedded system (TINI) which has only ROM and RAM. The filesystem is
stored in RAM and is erased on reboot, so I have to recreate it from
my Java app which is loaded from the ROM.

IMHO your idea is a waste of time. You start the unpacking of the .class
files from the ROM, right? Since you asked for a Java String, I assume
this unpacking is done from a Java application. Therefore it follows
that you can run a Java application in ROM on your environment. Which
also means, you must have a ClassLoader which is able to load bytecode
from the ROM (otherwise your initial Java application for unpacking
can't start).

So why on earth don't you just load all your bytecode from ROM? I would
even expect that your default system class loader is capable of loading
the bytecode from the ROM, otherwise your initial application won't come
very fare.

/Thomas
 
R

Rob Shepherd

So why on earth don't you just load all your bytecode from ROM?

The ROM banks are quite limited on this piece of hardware.

The ROM holds TiniOS, Runtime env stuff and Core API libraries.

There is space [starting at 0x70000] for 65280 bytes of Primary application in the 512k
ROM chip.

The Primary Application works like /sbin/init and loads up the first Process for the
system to run. Slush is the development environment shell thingy which also does
serial,telnet,ftp services.

You can also have other classes [bits of bytecode] stored in the battery backed RAM space
[this could be your "functionaly" main Application.]

One scenario is, if you havn't space to put it all in ROM, then put a bit in ROM and the
rest in RAM.

However it seems like Anguel doesn't want to use RAM in fear of losing something in case
of a memory error or FS integrity check failure..

So I still don't get, why write out a new class?

....Oh and *just thought* , the tini system will only run bytecode that has been
pre-converted using TINIConvertor [which btw you can actually do onboard]

So there's another step you'll have to do, as well as getting the Classloader to accept
the new class into the pool.

Rob
 
A

Anguel Stankov

Hi Rob, hi all!

Ok, I did not want to confuse you by explaining WHY I want to do that.
But now everybody wants to know and I will tell you why :)
Very simple: TINI is going to control a device. The control GUI will
be also stored on TINI as a Java applet. So the TINI will serve the
control applet. This applet is a normal Java class file and will be
executed by the client (a PC - so it will not be TINI converted, it is
a standard Java class file!). This makes my solution completely
platform independant. So far so good, but...
The standard solution would be to upload the applet once into the TINI
RAM filesystem and serve it from there. Now if this is a production
device you can imagine what will happen if the RAM gets corrupted or
the battery fails (which is not uncommon). This means no more applet.
No applet means no control to my device = very very bad.
So the only possibility is to get the applet somehow into ROM and load
it from there into the RAM filesystem on TINI startup. Because I am
not familiar with assembler and do not want to write some complicated
low level routines that store the applet class into ROM banks and then
load it back from ROM banks, the only possibility would be to load the
whole thing from my main Java app. And to get the applet class file
into this app I have to put it into a String :) Got it? ;-)
Any other solutions to the problem are welcome!

Best regards,
Anguel
 
R

Roedy Green

the only possibility would be to load the
whole thing from my main Java app. And to get the applet class file
into this app I have to put it into a String :)

I lost you when you said you needed a String. Why not a byte array?
 
A

Anguel Stankov

Hi Roedy!

Roedy Green said:
I lost you when you said you needed a String. Why not a byte array?

First of all I have to put the code of the whole .class file into my
java app before compiling it. That is needed because during runtime I
don't have any filesystem on the ROM to read from, just my app.
Therefore I must be able to open a .class file in an editor and paste
the whole thing into my java app. The only possibility I can think of
is to paste it into a String. Do you know a way to paste it into a
byte array? If I could paste it into a byte array, then maybe I would
not need to convert it to base64 at all...

Regards,
Anguel
 
R

Roedy Green

First of all I have to put the code of the whole .class file into my
java app before compiling it. That is needed because during runtime I
don't have any filesystem on the ROM to read from, just my app.
Therefore I must be able to open a .class file in an editor and paste
the whole thing into my java app. The only possibility I can think of
is to paste it into a String. Do you know a way to paste it into a
byte array? If I could paste it into a byte array, then maybe I would
not need to convert it to base64 at all..

So you are putting a base64 version of your class hard coded as a
string constant into ROM. At run time you will convert this back to
byte array and use a class loader to convert it to a live class?

Why not just burn the class file into ROM? It would be more compact?
It seems you are burning your container class into RAM an binary.

If you must do what you are doing, and ROM is tight, for $100US I
could cook up a variant of Base 64 which is about twice as compact.
Base64 presumes 8-bit rather than 16-bit printable output.
 
A

Anguel Stankov

Hi!

Roedy Green said:
So you are putting a base64 version of your class hard coded as a
string constant into ROM. At run time you will convert this back to
byte array and use a class loader to convert it to a live class?

Almost. I will write the byte array into a file in the filesystem
which is in RAM. From there I will serve the class (which is an
applet) using a mini HTTPServer. No class loader is needed.
Why not just burn the class file into ROM? It would be more compact?
It seems you are burning your container class into RAM an binary.

I would need to write assembler code to read and write from and to
flash ROM. I would have to know which ROM banks are free. If I do all
from Java it is easier to do it and to update the program.
If you must do what you are doing, and ROM is tight, for $100US I
could cook up a variant of Base 64 which is about twice as compact.
Base64 presumes 8-bit rather than 16-bit printable output.

Thanks but I've got plenty of ROM - 2 MB :) Base64 will do it.

Regards,
Anguel
 
R

Roedy Green

Thanks but I've got plenty of ROM - 2 MB :) Base64 will do it.

you would think code like this

byte[] classimage = new byte[]{ 0x43, 0x41, 0x46, .... };
would produce a string of hex bytes in the class file, but it
generates a horribly bulky code, to construct the array. Each of
those items could be a method call, so the code generated is fully
general, rather than optimised for compile-time constants.


This is one area the JVM class file format needs an extension. you
need a way to efficiently encode arrays of constant bytes or mostly
constant bytes, ints etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top