Create account / Log in

How to access files stored in a module?

Talk about module design ideas and techniques.

Moderators: uckelman, Tim M

How to access files stored in a module?

Postby L.Tankersley » November 16th, 2010, 12:10 am

I'm programming some custom code for a module I'm working on, and after some trial-and-error have got things more or less figured out. Now I'm up against what is (probably/hopefully) a simple problem, but the solution isn't obvious to me. I want to store some data files within my module, to read using code (for use in defining some custom piece behavior, for example). What path do I use to access such a file?

Given a test file "foo" placed in the root of the module (same location as buildFile and moduledata), I have tried to create a new FileInputStream on "foo" and "/foo", getting a FileNotFound exception in both cases. I figure there must be a way to do this, since VASSAL must do it when loading images stored in the module, but the VASSAL code is sufficiently arcane and overloaded that I can't readily figure out what to do. Any assistance would be appreciated.

---
Leland J. Tankersley
---
Leland J. Tankersley
L.Tankersley
 
Posts: 9
Joined: November 14th, 2010, 1:53 pm

Re: How to access files stored in a module?

Postby L.Tankersley » November 16th, 2010, 3:57 am

Ah, some additional digging allowed me to answer my own question. In case someone from the future comes here looking for the answer to the question:

Look at VASSAL.tools.DataArchive; it is a wrapper for accessing members within a zip archive (in this case, a module file). There are convenience routines for accessing images in the images/ directory (which is how I eventually found it), but you can also do things like:

Code: Select all
               String myFile = "foo";
               try {
                  InputStream is = GameModule.getGameModule().getDataArchive().getInputStream(myFile);
                  DataInputStream in = new DataInputStream(new BufferedInputStream(is));
                  // do stuff with in
               } catch (FileNotFoundException e) {
               } catch (IOException e) {
               }


which will open the file named "foo" located in the root of the module archive.
---
Leland J. Tankersley
L.Tankersley
 
Posts: 9
Joined: November 14th, 2010, 1:53 pm

Re: How to access files stored in a module?

Postby uckelman » November 22nd, 2010, 10:38 pm

L.Tankersley wrote:Look at VASSAL.tools.DataArchive; it is a wrapper for accessing members within a zip archive (in this case, a module file). There are convenience routines for accessing images in the images/ directory (which is how I eventually found it), but you can also do things like:


Note that you really, really should have a finally block which properly closes the stream here:

Code: Select all
final   String myFile = "foo";
InputStream is = null;
try {
  is = GameModule.getGameModule().getDataArchive().getInputStream(myFile);

  DataInputStream in = null;
  try {
    in = new DataInputStream(new BufferedInputStream(is));
    // do stuff with in
    in.close();
  }
  finally {
    IOUtils.closeQuietly(in);
  }
}
catch (FileNotFoundException e) {
  // handle the exception
}
catch (IOException e) {
  // handle the exception
}
finally {
  IOUtils.closeQuietly(is);
}
User avatar
uckelman
Site Admin
 
Posts: 8798
Joined: December 10th, 2007, 9:48 am
Location: Durham, England

Re: How to access files stored in a module?

Postby L.Tankersley » November 23rd, 2010, 3:09 am

Good point.
---
Leland J. Tankersley
L.Tankersley
 
Posts: 9
Joined: November 14th, 2010, 1:53 pm


Return to Module Design

Who is online

Users browsing this forum: No registered users and 4 guests