We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Opening a file with createInput
Page Index Toggle Pages: 1
Opening a file with createInput (Read 677 times)
Opening a file with createInput
Apr 12th, 2009, 8:26am
 
I have created a sketch to read MD2 Quake models files and display them. It works fine in Processing but when exported to an applet and uploaded to a website the createInput() method throws an exception. The following code opens a file stream

Any suggestions or solutions greatly appreciated.

Thanks
.
Code:

private boolean openBuffer(String mdlFile){
try {
fin = (FileInputStream)app.createInput(mdlFile);
}
catch (Exception ex){
System.out.println("Can't create file stream");
return false;
}
try{
fc = fin.getChannel();
}
catch(Exception ex) {
System.out.println("Can't create channnel");
return false;
}
if(fin == null){
System.out.println("File Channel is NULL");
return false;
}
try{
buf = ByteBuffer.allocateDirect((int)fc.size());
if(buf == null){
System.out.println("failed to allocate buffer buf = null");
}
fc.read(buf);
// BIG_ENDIAN is the default - might be for Java
// but the file was created with a C++ program
buf.order(ByteOrder.LITTLE_ENDIAN);
buf.rewind();
}
catch(Exception ex) {
System.out.println("Can't allocate buffer");
return false;
}
return true;
}
Re: Opening a file with createInput
Reply #1 - Apr 13th, 2009, 3:33am
 
1) You don't tell us which exception.
2) It is probably some security exception: applets are restricted on operations outside of the browser, like accessing local filesystem or external Web site. You must sign the applet to lift these restrictions.
Somehow, this should go to the FAQ...
Re: Opening a file with createInput
Reply #2 - Apr 13th, 2009, 10:43am
 
The problem is that createInput() returns a FileInputStream when run inside Processing and I assumed that it did the same when run as an applet in a web browser - I was wrong (my thanks to Fry for pointing out the error).

I did some experiments and found that createInput() returns a ZipFile object because the file is now being read from the sketch.jar file.

Since an MD2 file is a binary file and is normally read using C++ it requires unsigned integral types (i.e. unsigned short, unsigned int etc) which Java hasn't got. I got round the problem by using a ByteBuffer reading the appropriate number of bytes and when required performing the conversion from C++ primitive to Java primitive types and this (ByteBuffer) works well with FileInputStream but not ZipFile.

Anyway I have got to the point were I can read the entries in a jar file and the next stage is to read the entries I need into a ByteBuffer, thus removing the need for a FileInputStream

When it is complete I will make it available in the Exhibition forum
Page Index Toggle Pages: 1