1

I've recently decided to convert my 3d model format over to a binary file instead of ASCII in the hope of a speed increase... I read some java tutorials and the simplest way of reading my format seems to be using the ObjectInputStream and a mixture of readLong() and readFloat() commands... but I can't seem to get the code to work...

Here is my test code:

void testLoadBin(String fileName, Context context){
try {
    InputStream fis = context.getAssets().open(fileName);
    ObjectInputStream is = new ObjectInputStream(fis);
    long test;
    test = is.readLong();
    Log.i("World", "output" + test);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Log.i("World", "ERROR");
    }
}

In this example I'm just trying to read the first LONG from the file, but the IOException is always thrown and I'm not sure what to do to fix this.

Any help would be useful.


Quick add: the stack trace error output:

09-18 00:16:08.559: INFO/World(3861): java.io.StreamCorruptedException
09-18 00:16:08.559: INFO/World(3861):     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2369)
09-18 00:16:08.559: INFO/World(3861):     at java.io.ObjectInputStream.<init>(ObjectInputStream.java:433)
09-18 00:16:08.559: INFO/World(3861):     at org.ogl.ndkvbo.Screen.testLoadBin(Screen.java:6605)
09-18 00:16:08.559: INFO/World(3861):     at org.ogl.ndkvbo.camState_intload.update(camState_intload.java:24)
09-18 00:16:08.559: INFO/World(3861):     at org.ogl.ndkvbo.StateMachine.update(StateMachine.java:65)
09-18 00:16:08.559: INFO/World(3861):     at org.ogl.ndkvbo.camera.update(camera.java:46)
09-18 00:16:08.559: INFO/World(3861):     at org.ogl.ndkvbo.GameThread.run(GameThread.java:50)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Kalisme
  • 518
  • 1
  • 10
  • 19
  • What does the IOException say in detail? Are you sure, the file exists in the given path? Does it hold actually a ''long''? – Fildor Sep 17 '12 at 13:50

1 Answers1

2

You must use ObjectOutputStream() if you want to use ObjectInputStream(). However nothing prohibits you as ObjectInputStream and ObjectOutputStream extend from DataOutputStream and DataInputStream.

So it will be helpful if you could also post the code snippet where you are writing to file. Also check if fis in the line InputStream fis = context.getAssets().open(fileName); is correct and actually points to the file that you intend to read from.

Fildor
  • 14,510
  • 4
  • 35
  • 67
sharadendu sinha
  • 827
  • 4
  • 10
  • 1
    Ah... I wasn't aware that the binary file had to specifically be made through ObjectOutputStream... I wrote the file through Blender exporting using the file.write(struct.pack("f",output_float)) styled commands... ("l" for outputed longs)... If this is all wrong, could you possibly point me in the direction of what commands I should be looking into? – Kalisme Sep 17 '12 at 15:22
  • Well if you do want to read from a binary file , you must know the protocol that you use to write to the file. Suppose you wrote and int and if you were reading an int using DataInputStream, you may not always get the right results, as DataInputStream mandates int to BigEndian, so if you wrote the int in LittleEndian, you will get garbage. – sharadendu sinha Sep 17 '12 at 17:54
  • The closes in java could be Buffer. Check out http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html. However I still feel that it may get tough if you call pack before write as you will not always know how many bytes to read ... but this can definitely help you if you don't call pack – sharadendu sinha Sep 17 '12 at 17:54
  • I ended up browsing for a different method thanks to your comment and ended up finding a tutorial that explained how to use InputStream and read the binary... It changed the load time from about a minute to less then a second! I'll post the link in the question. Thanks for your help. – Kalisme Sep 18 '12 at 15:40