I'm having a problem with the method to load from an URL.
I've created the method to load from an InputStream and it seems to be correct.
The problem is that when loading from the URL, i get a byte count that is less than the actual file.
Th 9candles.ops is 1422 bytes long, but when i read it through a URL - by the url.openStream() - i get 1403 bytes.
If i use this in the ZipInputStream i can get the xml file name inside the zip stream - 9candles.xml - but i can't get the size of the XML file - which should be around 35 KB - nor can i read any bytes whatsoever.
@Override
public ParticleSystem load(InputStream inputStream) throws Throwable {
BufferedInputStream bis = new BufferedInputStream(inputStream);
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry entry = null;
entry = zipInputStream.getNextEntry();
if (entry == null)
throw new FileNotFoundException("the Zip entry is null");
if (entry.isDirectory())
throw new FileNotFoundException("the first entry is a directory");
String zeName = entry.getName();
int size = (int) entry.getSize();
// -1 means unknown size.
System.out.println("Found file name -> " + zeName + " size : " + size + " csize : " + entry.getCompressedSize());
int rb = 0;
int chunk = 0;
String file = "";
while ( (chunk = zipInputStream.read()) > 0) {
file+=chunk;
rb += chunk;
}
System.out.println("U Size : " + file.length());
if (size == -1) {
throw new RuntimeException(
"Impossible to get the size of the entry in the ZIP Stream");
}
byte[] b = new byte[(int) size];
rb = 0;
chunk = 0;
while (((int) size - rb) > 0) {
chunk = zipInputStream.read(b, rb, (int) size - rb);
if (chunk == -1) {
break;
}
rb += chunk;
}
System.out.println("Read -> " + rb + " bytes");
ByteArrayInputStream bArrayInputStream = new ByteArrayInputStream(b);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ParticleSystem obj = (ParticleSystem) uctx.unmarshalDocument(
bArrayInputStream, null);
ParticleSystem ps = obj;
setObjectReferences(ps);
bArrayInputStream.close();
zipInputStream.close();
return obj;
}
@Override
public ParticleSystem load(URL url) throws Throwable {
InputStream inputStream = url.openStream();
BufferedInputStream bif = new BufferedInputStream(inputStream);
InputStreamReader isr = new InputStreamReader(bif);
BufferedReader br = new BufferedReader(isr);
String file = null;
String temp = null;
while ((temp = br.readLine()) != null) {
if(file == null)
file = temp;
else
file += temp;
}
// System.out.println(file);
ByteArrayOutputStream bais = new ByteArrayOutputStream();
bais.write(file.getBytes());
System.out.println("Size : " + file.length());
System.out.println("byte output Size : " + bais.size());
ByteArrayInputStream inStream;
inStream = new ByteArrayInputStream(bais.toByteArray());
// FileOutputStream file = new FileOutputStream();
ZipInputStream zis = new ZipInputStream(inStream);
ZipEntry entry = zis.getNextEntry();
System.out.println(" entry : " + entry.getName() + " | " + entry.getSize());
File test = new File("test.ops");
test.createNewFile();
FileWriter fw = new FileWriter(test);
fw.write(file);
fw.flush();
fw.close();
System.out.println("Wrote a new file");
ParticleSystem ps = load(inStream);
inputStream.close();
return ps;
}