| 4. Mon Jun 26, 2017 - UBJsonReader parse() a short[] throws EOFException

Badlogic Games • View topic - UBJsonReader parse() a short[] throws EOFException

UBJsonReader parse() a short[] throws EOFException

Anything libgdx related goes here!

UBJsonReader parse() a short[] throws EOFException

Postby libbyjix » Mon Jun 26, 2017 8:39 am
Hi, there is a field in UBJsonReader.java named 'oldFormat' and it is defaulted to the value 'true'. When parsing a short data type, it executes this line:

Code: Select all
else if (type == 'I')
   return new JsonValue(oldFormat ? (long)din.readInt() : (long)din.readShort());

This causes the dataOutputStream to read 4 bytes instead of 2 which causes a mismatch between array length and the data being pulled from it and then throws the EOFException. So here's my question, should I use UBJsonWriter to write int arrays instead of short arrays* to accommodate for this old format and whatever technical reason(s) are behind it or is this a bug I should open an issue for? Here is the code that produces the problem:

Code: Select all
public class UBJsonReadWriteTest implements ApplicationListener {

   @Override
   public void create () {
      try{
         UBJsonWriter writer = new UBJsonWriter( new FileOutputStream("test.bin") );
         writer.object();
         writer.set("shortArray", new short[]{((short)1), ((short)2), ((short)3)});
         //writer.set("floatArray", new float[]{3,2,3}); <-- this run alone causes no error
         //writer.set("stringArray", new String[]{"dog", "hat", "run"}); <-- this run alone also causes no error
         writer.pop();
         writer.close();

      }catch(FileNotFoundException fnf){}catch(IOException ioe){}
      UBJsonReader reader = new UBJsonReader();
      reader.parse(Gdx.files.getFileHandle("test.bin", Files.FileType.Internal));
   }
   @Override
   public void render () {}
   @Override
   public void dispose () {}
   @Override
   public void resume(){}
   @Override
   public void pause(){}
   @Override
   public void resize(int x, int y){}
}

and here is error output:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error parsing file: test.bin
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:56)
at com.mygdx.meshtest.UBJsonReadWriteTest.create(UBJsonReadWriteTest.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Caused by: com.badlogic.gdx.utils.SerializationException:
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:45)
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:54)
... 3 more
Caused by: java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:392)
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:86)
at com.badlogic.gdx.utils.UBJsonReader.parseArray(UBJsonReader.java:123)
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:70)
at com.badlogic.gdx.utils.UBJsonReader.parseObject(UBJsonReader.java:159)
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:72)
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:62)
at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:43)
... 4 more

Thanks for any help.

*I checked the UBJsonWriter.java 'public UBJsonWriter value (short[] values) throws IOException{...}' function against the UBJson spec here: http://ubjson.org/type-reference/ and I don't see an error on that end which leads back to the 'oldFormat' UBJsonReader side of things.
Last edited by libbyjix on Mon Jun 26, 2017 9:16 am, edited 1 time in total.
libbyjix
 
Posts: 18
Joined: Fri May 15, 2015 5:07 pm

Re: UBJsonReader parse() a short[] throws EOFException

Postby evilentity » Mon Jun 26, 2017 8:53 am
I dont think it counts as hardcoded if its public and non final.
Code: Select all
reader.oldFormat = false;
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4867
Joined: Wed Aug 24, 2011 6:37 am

Re: UBJsonReader parse() a short[] throws EOFException

Postby libbyjix » Mon Jun 26, 2017 9:26 am
Hey, thanks for the quick reply. Good point, I didn't even notice the public field. I updated the post to say "defaulted" instead. Any idea what format this "oldFormat" refers to and what considerations might go into leaving it set to true and using int, int[] or changing it to false for use with short, short[] values?
libbyjix
 
Posts: 18
Joined: Fri May 15, 2015 5:07 pm

Re: UBJsonReader parse() a short[] throws EOFException

Postby evilentity » Mon Jun 26, 2017 11:40 am
Presumably there was different format before, oldFormat if you will. And it defaults to it so stuff doesnt suddenly stops working if someone updated the lib.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4867
Joined: Wed Aug 24, 2011 6:37 am

Re: UBJsonReader parse() a short[] throws EOFException

Postby libbyjix » Tue Jun 27, 2017 2:00 am
*edited*

Ok, so it's aimed at backward compatibility for end user-created stuff not inter-library code. Since there was no code comment about it or an explanation regarding it on the Java docs, I just wanted to be sure there wasn't anything deeper to it so that when I started altering the field it wouldn't cause a seemingly unrelated error somewhere else sorta thing. Like if another class I pass it to relies on the UBJsonReader having it set to 'true', etc. I went ahead and opened an issue regarding this mismatch between the default states of the ubjson reader and writer regarding short values. Thanks for your help.
libbyjix
 
Posts: 18
Joined: Fri May 15, 2015 5:07 pm

Re: UBJsonReader parse() a short[] throws EOFException

Postby libbyjix » Tue Jul 11, 2017 9:29 am
For anyone who might read this thread later here's more info about this problem:

It turns out that if 'oldFormat = false' then UBJsonReader parse() breaks when trying to read in a .g3db file from FBX-Conv so I guess there is inter-library dependency on this 'oldFormat' field after all.

The github issue with full details is here: https://github.com/libgdx/libgdx/issues/4799
libbyjix
 
Posts: 18
Joined: Fri May 15, 2015 5:07 pm



Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], libbyjix and 1 guest