libgdx: [GWT] FileHandle.readBytes() doesn't work for internal

ver 1.6.4 The method reads and returns only 1 byte of data:

FileHandle tfile = Gdx.files.internal(("badlogic.jpg"));
L.d("tfile", tfile + " : " + tfile.exists());

byte[] byts = tfile.readBytes();
L.d("byts", byts.length + " " + tfile.length());

Texture img = new Texture(tfile);
L.d("img1", img);

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (12 by maintainers)

Most upvoted comments

I have nothing against being creative to find workarounds, the thing is I chose libGDX, I don’t expect it to be perfect I just want to know that issues exists regarding some functions and if there are, workarounds have been found.

The first thing I did when I discovered (I shouldn’t have discover it in fact) that readBytes doesn’t work on gwt is to look into the java doc of the class to see what I missed, and: no clue, not even a mention to a different behaviour with gwt in the wiki explaining File Handling.

And when I found this issue here, it’s marked as closed, meaning that there is no problem or it has been solved. What is not the case.

The main problem here for me is a lack of centralization of the information.

At minimum, known issues or different behaviours on platforms should be indicated in the documentation of the function.

But the best would be to do something like php Manual which enables to separate the code from the documentation and above all enables users to enlight specific behaviours observed, good practices in the using of the function, issues, workarounds and so on.

How can we help ?

You have to change the extension to .bin. I wanted to add an issue “inconsistent behaviours with FileHandle” when I found this one.

Duplicate badlogic.jpg and rename it badlogic.bin. tfile.readBytes() tfile.length() will then work as expected BUT you won’t be able to load the bin file as a texture.

On gwt, it seems that everything depends on the file extension. On Android it doesn’t care. It can recognize an image even if the extension has been changed.

For me this issue shouldn’ be closed. FileHandle should work the same on all platforms (internal files case), and if not possible, at least this trick should be mentioned in the Handling file documentation.

I used to have binary files (not images) in my android projects with no extension and obviously FileHandle fails to treat them correctly.

Same goes for UTF-8 encoding text files without the .bin extension. In this case the data loaded are corrupted. “\r\n” -> “\n” and length files differ from Desktop to Gwt. So searching for “\r\n” obviously fails.

It can save days of debugging knowing those !