8 Replies - 1634 Views - Last Post: 20 November 2010 - 10:51 AM Rate Topic: -----

#1 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Interpreting a input stream

Posted 19 November 2010 - 09:57 AM

Hey Folks,

Okay, I am writing a small program, and I have found that getting images from within a compiled jar is easy. Though I need to grab some xml files. I'm a little confused that I have to interpret it as an input stream.

Anyway, I have googled around and found that I have to read the file within a jar as an input stream, the problem being is I don't know how to interpret it with the way I'm reading in the file now.

This is how I'm doing it now within eclipse (and works perfectly) but as soon as i compile it can't find the file. If I put a hard link to where the file is on my computer it works.

file = new File(this.getClass().getResource("/resources/maps/"+map.getMapName()).toURI());

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();


What I have been trying to do is read the inputstream into a file that is in memory. I don't want to actually create the file.

Here is what I have so far:

File file = new File("data.xml");//Creates unwanted file
InputStream input = this.getClass().getResourceAsStream("/resources/maps/"+map.getMapName());
OutputStream output = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len;
while((len=input.read(buf))>0){
	output.write(buf,0,len);
	output.close();
	input.close();
	Log.msg("Finished reading input stream");
}


This method partially works, it creates a file (i dont want it too) and then in the file there is only part of the actual XML file.

I would like to thank everyone in advance for the help.

Cheers
Metric

Is This A Good Question/Topic? 0
  • +

Replies To: Interpreting a input stream

#2 raymens  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 02-June 10

Re: Interpreting a input stream

Posted 19 November 2010 - 12:54 PM

Printing the file you're referring to on screen might show you why it's not working, compare it to the required result.
Was This Post Helpful? 1
  • +
  • -

#3 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Interpreting a input stream

Posted 19 November 2010 - 01:20 PM

Thanks for the suggestion, I did that and found my loop was ending the streams in the middle which makes no sense...

Okay now I have it reading in the file and I'm comparing it to the file that was there previously and it appears to be exactly the same, but im getting this error:
Unexpected end of ZLIB input stream


EDIT: I copied the contents from the created file into a manually created file and it worked. So theres an issue with the created file, i assume its not closing properly, i will look into this.

Also I still am not sure how how to generate the File object into memory and to not actually create the file.

This post has been edited by metric: 19 November 2010 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,995
  • Joined: 20-September 08

Re: Interpreting a input stream

Posted 19 November 2010 - 05:06 PM

Quote

but as soon as i compile it can't find the file.


What do you mean by that? Actually Eclipse by default compiles automatically
Was This Post Helpful? 1
  • +
  • -

#5 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Interpreting a input stream

Posted 19 November 2010 - 06:22 PM

I apologize, that was not what I meant to say.

Here is my modified code:
File file = new File("data.xml");//Creates unwanted file
InputStream input = this.getClass().getResourceAsStream("/resources/maps/"+map.getMapName());
OutputStream output = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len = 0;
while((len=input.read(buf))> 0){
	Log.msg(len+" "+input.read());
	output.write(buf,0,len);
}
input.close();
output.close();
Log.msg("Finished reading input stream into file");


All the information in this file created, is correct; but for some reason the file is not saved correctly (I'm really not sure whats happening after the loop finishes and the streams close).

I made a copy of the original, removed all the data so it was an empty file, and copied all the data from the file the program made into the empty file and it loads perfectly with no errors.

So I believe that it is something to do with the way the file is closed or finalized...

Here is the error:
java.io.EOFException: Unexpected end of ZLIB input stream
	at java.util.zip.InflaterInputStream.fill(Unknown Source)
	at java.util.zip.InflaterInputStream.read(Unknown Source)
	at java.util.zip.GZIPInputStream.read(Unknown Source)
	at java.util.zip.InflaterInputStream.read(Unknown Source)
	at maps.MapParser.parseIt(MapParser.java:173)
	at core.Game.setMap(Game.java:128)
	at core.Game.<init>(Game.java:113)
	at core.MainMenu.<init>(MainMenu.java:53)
	at core.MainMenu.main(MainMenu.java:186)


Thanks Metric


EDIT: I seem to have partially fixed it. I changed the byte array size from 1024 to 4096, I don't understand why this makes a difference...maybe because the file is larger then 1024 bytes? That makes sense...okay I'm rambling.

I'm still trying to figure out how to do this without creating a physical file.

Also if someone could explain why the change in array size worked i would appreciate it!

This post has been edited by metric: 19 November 2010 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,995
  • Joined: 20-September 08

Re: Interpreting a input stream

Posted 20 November 2010 - 05:49 AM

Personally i would always use while((len=input.read(buf))> -1) as that guarantees eof is reached
Was This Post Helpful? 1
  • +
  • -

#7 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Interpreting a input stream

Posted 20 November 2010 - 08:52 AM

That makes sense g00se.

Any idea of how to create the file that the inputStream goes to in memory? (oppose to actually creating on the harddisk)
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,995
  • Joined: 20-September 08

Re: Interpreting a input stream

Posted 20 November 2010 - 09:58 AM

Write to a ByteArrayOutputStream instead
Was This Post Helpful? 1
  • +
  • -

#9 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Interpreting a input stream

Posted 20 November 2010 - 10:51 AM

Awesome thank-you g00se! Your the pro!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1