JavaFX – Storage


JavaFX has Storage API which provides functionality similar to read and write from flat file. The API is part of common profile and hence will work across various platforms/devices. The application is not required to be signed to use this API. Also the access of saved data is restricted with in the codebase of the application.

For Applet mode, click on above image

For standalone mode

In above sample use PathTransition API to move the car along a path (road). User can create roads by dragging the mouse over the terrain. The path will be saved using storage API, so that it can be retrieved later. Try creating a new path, close the application and reload the application later, the path will be re-created.

The path elements can be represented in xml format as shown below.


<path>
    <element type="MoveTo" x="73.0" y="46.0" />
    <element type="LineTo" x="74.0" y="45.0" />
    <element type="LineTo" x="75.0" y="45.0" />
    <element type="LineTo" x="82.0" y="45.0" />
    <element type="ClosePath" />
</path>

Next we will create a Storage instance and open an OutputStream to this as shown below. The API is similar to FileOutputStream. The boolean in openOutputStream is to specify overwrite (opposite of append in FileOutputStream). Size of resource can be specified using resource.maxLength attribute.


var entry = Storage {
    source: "TerrainPath.dat"
};

var out = entry.resource.openOutputStream(true);

This way we obtain the OutputStream to which we can write the data.

We can retrieve the data using openInputStream which returns an instance of InputStream instance. The data can be read from this stream, similar to FileInputStream.


var in = entry.resource.openInputStream();

Once the InputStream is obtained we can use PullParser to parse the xml data and construct the path.


var pullParser = PullParser {

    // InputStream obtained from Storage
    input: in

    onEvent: function(e) {

        if(e.type == PullParser.START_ELEMENT) {
            var type = e.getAttributeValue("type");
            if(type.equals("MoveTo")) {
                var x = e.getAttributeValue("x");
                var y = e.getAttributeValue("y");
                insert MoveTo 
                    x: Number.parseFloat(x
                    y: Number.parseFloat(y
                into elements;
            else if(type.equals("LineTo")) {
                var x = e.getAttributeValue("x");
                var y = e.getAttributeValue("y");
                insert LineTo {
                    x: Number.parseFloat(x
                    y: Number.parseFloat(y
                into elements;
            else if(type.equals("ClosePath")) {
                insert ClosePath { } into elements;
            }
        }
    }
};

pullParser.parse();

After re-creating the path we can use AnimationPath.createFromPath( <path> ) and PathTransition to move the car along the path. Please refer to previous post for more information on Animation.

Update:
I didn’t mention about storage.properties, wasn’t sure whether its a documented feature. Anyway users are trying to find the secrets! Well.. there is no secret as such, you can de-compile the class files

Configuration file – storage.properties – is under ~/.javafx/deployment/ in Unix and %HOME%/Sun/JavaFX/Deployment/ on Windows. User can enable/disable the storage using “storage.enabled” attribute by setting it to true/false. The limit can be configured using “storage.limit.domain”. This file will be empty by default.

Try it out and let me know feedback

Source:

var dzone_style = ‘2’;

Advertisements

About Rakesh Menon
-

13 Responses to JavaFX – Storage

  1. Anonymous says:

    Really funny, esp. when road and car leave the frame…

  2. Carl Dea says:

    Rakesh,
    Very cool
    Any information about the maximum size the data file can reach?
    Are there any other security constraints?
    Carl

  3. Rakesh Menon says:

    @Carl Thanks! Attribute maxLength is a request to API, there is no guarantee. For an application max-size is 1 MB in total, for each resource its 8Kb (by default). Again these values depends on platform/device. User can configure these attributes in storage.properties.

  4. "Well.. there is no secret as such, you can de-compile the class files"
    Lol! Of course, we do that (I did for the GUI files at least), although we are supposed not to: see javafx-sdk1.2/LICENSE.txt: "5. Restrictions. […] you may not decompile, or reverse engineer Software"
    😉
    Thanks for the information.

  5. Rakesh Menon says:

    @Philippe Lhoste I didn’t recommend that! I just mentioned what some people do.. 🙂

  6. FXPerf says:

    Rakesh,
    How is JavaFX Performance?
    I downloaded JavaFX and played path animation sample on JavaFX site. …and my CPU usage gone up over 25% on Intel Dual Core with 2 GB RAM.
    And I end-up uninstalling FX so that by mistake if I ever visit any site with JavaFX animation I don’t compare my CPU usage to go crazy.
    Do you (or someone in your team) see any chance of performance improvement in JavaFX in near future?

  7. Rakesh Menon says:

    @FXPerf Yes, PathAnimation is taking 25%, need some investigation. I tried above sample with complex path, still it was only around 10% or less. Could you please let me know the CPU utilization for above sample?
    "Do you (or someone in your team) see any chance of performance improvement in JavaFX in near future?"
    Yes.. definitely.. Performance is current focus area..

  8. Canvin says:

    It’s really a cool application.I copied your source code and it works on my windows7 system.But I soon found the app can not save data and reload them locally. I tried to add a line "storage.enabled=true" in storage.properties file but the problem remains.so,would you please give me some help? thanks.

  9. Rakesh Menon says:

    @Canvin Sorry, I didn’t understand the question. The app works from website above, but not work on standalone mode? The codebase for app executed from above page and a standalone app is different. So same app executed on standalone mode will not be able to access data created by above app. This is to ensure that there is no security issues. The data is stored under <user.home>/Sun/JavaFX/Deployment/storage/muffin for Windows.

  10. Cavin says:

    Thanks a lot.

  11. guyvo says:

    I can confirm that on my MAC it’s really needed to alter the storage.properties file to get it work. Thanks for that tip !
    It should be documented in fact.
    I tried to work with the storage class on my mobile HTC and this is also working although I couldn’t find the muffin directory so far 😉
    Any idea where ?
    -G

  12. Pingback: JavaFX + Yahoo Weather « Java, JavaFX and beyond…

  13. Pingback: JavaFX + Yahoo Weather « Java, JavaFX and beyond…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: