Pasting a file into a Java String

Discussion in 'Java' started by Anguel Stankov, May 11, 2004.

  1. 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
     
    Anguel Stankov, May 11, 2004
    #1
    1. Advertising

  2. Anguel Stankov

    Rogan Dawes Guest

    Anguel Stankov wrote:

    > 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
    --
    Rogan Dawes

    *ALL* messages to will be dropped, and added
    to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
     
    Rogan Dawes, May 11, 2004
    #2
    1. Advertising

  3. Anguel Stankov

    Roedy Green Guest

    On 11 May 2004 02:12:02 -0700, (Anguel Stankov) wrote
    or quoted :

    >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
    "...".

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 11, 2004
    #3
  4. Anguel Stankov

    Rob Shepherd Guest

    Anguel Stankov wrote:
    > 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
     
    Rob Shepherd, May 11, 2004
    #4
  5. Anguel Stankov wrote:
    > 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
     
    Thomas Weidenfeller, May 11, 2004
    #5
  6. Anguel Stankov

    Rob Shepherd Guest

    > 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
     
    Rob Shepherd, May 11, 2004
    #6
  7. Thanks! That's what I am looking for :)

    Rogan Dawes <> wrote in message news:<c7q5pf$m9m$>...

    > 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
     
    Anguel Stankov, May 12, 2004
    #7
  8. Hi Rob, hi all!

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


    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
     
    Anguel Stankov, May 12, 2004
    #8
  9. Anguel Stankov

    Rob Shepherd Guest

    Anguel Stankov wrote:
    > Hi Rob, hi all!
    >
    >
    >>>So why on earth don't you just load all your bytecode from ROM?

    >
    >

    Aaah That explains it.....

    Best of Luck

    Rob
     
    Rob Shepherd, May 12, 2004
    #9
  10. Anguel Stankov

    Roedy Green Guest

    On 12 May 2004 02:37:55 -0700, (Anguel Stankov) wrote
    or quoted :

    >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?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 12, 2004
    #10
  11. Thanks Roedy! Very nice code, exactly what I needed :)


    Roedy Green <> wrote in message news:<>...

    > 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
    > "...".
     
    Anguel Stankov, May 12, 2004
    #11
  12. Anguel Stankov

    Roedy Green Guest

    On 12 May 2004 13:32:45 -0700, (Anguel Stankov) wrote
    or quoted :

    >Thanks Roedy! Very nice code, exactly what I needed :)


    Which of the four options I gave you was the one you wanted?

    >
    >Roedy Green <> wrote in message news:<>...
    >
    >> 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 "



    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 13, 2004
    #12
  13. Hi Roedy!

    Roedy Green <> wrote in message news:<>...

    > Which of the four options I gave you was the one you wanted?


    I chose the Base64.

    Regards,
    Anguel
     
    Anguel Stankov, May 13, 2004
    #13
  14. Hi Roedy!

    Roedy Green <> wrote in message news:<>...

    > 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
     
    Anguel Stankov, May 13, 2004
    #14
  15. Anguel Stankov

    Roedy Green Guest

    On 13 May 2004 04:06:17 -0700, (Anguel Stankov) wrote
    or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 13, 2004
    #15
  16. Hi!

    Roedy Green <> wrote in message news:<>...

    > 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
     
    Anguel Stankov, May 14, 2004
    #16
  17. Anguel Stankov

    Roedy Green Guest

    On 14 May 2004 01:39:42 -0700, (Anguel Stankov) wrote
    or quoted :

    >> 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.


    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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 14, 2004
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Z2FuZXNoX2luZm9zeXM=?=

    pasting xml data into an excel template

    =?Utf-8?B?Z2FuZXNoX2luZm9zeXM=?=, Aug 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    686
  2. Marc Van Laer
    Replies:
    0
    Views:
    432
    Marc Van Laer
    Apr 11, 2005
  3. Wladimir Borsov
    Replies:
    0
    Views:
    385
    Wladimir Borsov
    Aug 28, 2003
  4. TG
    Replies:
    3
    Views:
    498
    Robert Kern
    Jul 26, 2006
  5. AjitGoel
    Replies:
    2
    Views:
    360
    AjitGoel
    Apr 17, 2007
Loading...

Share This Page