Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ReadAsciiFile returns empty string

I am trying to read in an xml file with ReadAsciiFile but the content is always empty. I've added some checking to see if the file exists (I know it does) but again nothing. If I perform the checking on say an image file, it works fine (exists = true / type is PNG etc). When I replace the filename with my XML it just fails.

fileName = "pkg:/xml/data.xml" - or "pkg:/xml/test.png"

LocalFileBrowser = CreateObject("roFileSystem")
print (LocalFileBrowser.exists(fileName))

contents = ReadAsciiFile(fileName)
print "Type: "+type(contents )


Why can it not read my XML file (some elements removed to save space)?

<DefenderBitmapSet>
<ExtraInfo cellsize="40"/>
<Bitmap name="Background" filespec="pkg:/snake_assets/snake.map_w-dirt.png" />
<Bitmap name="game-over" filespec="pkg:/snake_assets/snake.gameover.png" />
<Bitmap name="title-screen" filespec="pkg:/snake_assets/snake.title.3.png" />

<Bitmap name="snake" filespec="pkg:/snake_assets/snake.body_sprite.png">
<Region name="butt-North" x="00" y="00" w="40" h="40" />
<Region name="butt-East" x="40" y="00" w="40" h="40" />
<Region name="butt-South" x="80" y="00" w="40" h="40" />
<Region name="butt-West" x="120" y="00" w="40" h="40" />
</Bitmap>

<Bitmap name="water_strip" filespec="pkg:/snake_assets/snake.water_sprite.png">
<Region name="a" x="0" y="0" w="40" h="40" t="400" />
</Bitmap>

<Bitmap name="empty" shape="rect" color="0" w="40" h="40" t="680" />

<Animation name="water">
<frame use="water_strip.a" />
</Animation>

<Animation name="tongue-North">
<frame use="snake.tongue-North" />
<frame use="empty" />
</Animation>

<Animation name="tongue-East">
<frame use="snake.tongue-East" />
<frame use="empty" />
</Animation>

<Animation name="tongue-South">
<frame use="snake.tongue-South" />
<frame use="empty" />
</Animation>

<Animation name="tongue-West">
<frame use="snake.tongue-West" />
<frame use="empty" />
</Animation>

</DefenderBitmapSet>
0 Kudos
9 REPLIES 9
belltown
Roku Guru

Re: ReadAsciiFile returns empty string

Try reading the file into an roByteArray and examine the first few bytes to see if there's anything there other than a non-null ASCII character, i.e. check that each byte is between 1 and 127 inclusive. If you encounter anything else (like 0, or 254 or 255) then it's not an ASCII file. It might, for example, be UTF-8 encoded with a byte order mark, or some other non-ASCII encoding.
0 Kudos
EnTerr
Roku Guru

Re: ReadAsciiFile returns empty string

"AlexHolsgrove" wrote:
If I perform the checking on say an image file, it works fine (exists = true / type is PNG etc). When I replace the filename with my XML it just fails.
...    
contents = ReadAsciiFile(fileName)
print "Type: "+type(contents )


There is no way the code you show returned type "PNG". It will always be saying String, no matter if read failed (len(contents) = 0) or not. Which makes me think you need to look more closely at what you were doing.
0 Kudos
EnTerr
Roku Guru

Re: ReadAsciiFile returns empty string

"belltown" wrote:
Try reading the file into an roByteArray and examine the first few bytes to see if there's anything there other than a non-null ASCII character, i.e. check that each byte is between 1 and 127 inclusive. If you encounter anything else (like 0, or 254 or 255) then it's not an ASCII file. It might, for example, be UTF-8 encoded with a byte order mark, or some other non-ASCII encoding.

Checking with roByteArray is good idea!
To clarify though, there is nothing wrong with reading utf-8 file with readAsciiFile(). The naming ASCII is a misnomer, just like with the function ASC(). I checked the behavior and file gets read with some caveats:
  • all file contents (text/binary/what-have-you) will be returned, until first \0. CHR(0) is end of the line, think C ASCIIZ char array.

  • utf-8 in the file will be decoded to proper Unicodes on fw5 - while on fw3 it will remain as sequence of bytes

  • the optional 3-byte BOM at file beginning - if present - is not processed/removed; that is, on fw5 string will start with chr(65279), on fw3 it will be chr(&hEF)+chr(&hBB)+chr(&hBF)
0 Kudos
belltown
Roku Guru

Re: ReadAsciiFile returns empty string

"EnTerr" wrote:

Checking with roByteArray is good idea!
To clarify though, there is nothing wrong with reading utf-8 file with readAsciiFile(). The naming ASCII is a misnomer, just like with the function ASC(). I checked the behavior and file gets read with some caveats:
  • all file contents (text/binary/what-have-you) will be returned, until first \0. CHR(0) is end of the line, think C ASCIIZ char array.

  • utf-8 in the file will be decoded to proper Unicodes on fw5 - while on fw3 it will remain as sequence of bytes

  • the optional 3-byte BOM at file beginning - if present - is not processed/removed; that is, on fw5 string will start with chr(65279), on fw3 it will be chr(&hEF)+chr(&hBB)+chr(&hBF)

That sounds about right. I know that the different Roku firmwares and components vary in the way they handle UTF-8 encoded files. But definitely a null character will be seen as a string terminator no matter what the encoding. I've never had a problem with ReadAsciiFile reading an Xml file, whether that file was packaged with my channel, or read off a server, except in cases where there were nulls before the end of the file.
0 Kudos

Re: ReadAsciiFile returns empty string

"EnTerr" wrote:
"AlexHolsgrove" wrote:
If I perform the checking on say an image file, it works fine (exists = true / type is PNG etc). When I replace the filename with my XML it just fails.
...    
contents = ReadAsciiFile(fileName)
print "Type: "+type(contents )


There is no way the code you show returned type "PNG". It will always be saying String, no matter if read failed (len(contents) = 0) or not. Which makes me think you need to look more closely at what you were doing.


The PNG was actually the raw output. This is the actual code I was running - just as a test:

fileName = "pkg:/images/Logo_Overhang_HD.png"
xml = ReadAsciiFile(fileName)
print "Type: "+type(xml)
print xml
print xml.Len()
print "Done"


The length was only 7 but I expected that to fail anyway. It still doesn't work if the fileName pointed to an XML document. I'll try the array next and report back.

Update:
Before even getting as far as reading in the file, I should be able to use roFileSystem to check if the XML file exists. I am certain I have the correct filename / path (the code returns true when I test for any of the image assets)

fileName = "pkg:/xml/sprite.small.map.xml"
LocalFileBrowser = CreateObject("roFileSystem")
print "Exists: "
print LocalFileBrowser.exists(fileName)


Exists:
false
0 Kudos
EnTerr
Roku Guru

Re: ReadAsciiFile returns empty string

"AlexHolsgrove" wrote:
The PNG was actually the raw output. This is the actual code I was running - just as a test: [...] The length was only 7 but I expected that to fail anyway.

Aha. So the discussion about binary files above applies, there must have been a \0 right after the 8-byte PNG header.

Before even getting as far as reading in the file, I should be able to use roFileSystem to check if the XML file exists. I am certain I have the correct filename / path (the code returns true when I test for any of the image assets) [...]

Right. And seeing the result of your test, i am forced* to conclude the file "sprite.small.map.xml" indeed does not exist inside "pkg:/xml/". Have you checked if it is inside the ZIP bundle that gets uploaded to Roku? It might be that the whole directory is not included at all. Are you using the "eclipse plugin", i can imagine that thing causing it?

(*) for "when all other contingencies fail, whatever remains, however improbable, must be the truth". I checked if by any chance player has issue when file name contains multiple dots "." - but no, it didn't.
0 Kudos

Re: ReadAsciiFile returns empty string

That's exactly the problem - Eclipse wasn't including the directories with my XML (and other assets: viewtopic.php?f=34&t=44784)

The one thing that bugs me, is if I go to File->Export and then select the directories that I need - Eclipse doesn't remember them next time. It means that I can use the toolbar button to do a silent deployment (Ctrl+Alt+E). Is there a way to fix this?
0 Kudos
malloys
Visitor

Re: ReadAsciiFile returns empty string

"AlexHolsgrove" wrote:
That's exactly the problem - Eclipse wasn't including the directories with my XML (and other assets: viewtopic.php?f=34&t=44784)

The one thing that bugs me, is if I go to File->Export and then select the directories that I need - Eclipse doesn't remember them next time. It means that I can use the toolbar button to do a silent deployment (Ctrl+Alt+E). Is there a way to fix this?


The bug is centered around directory selection/deselection. This was reported yesterday by RokuJoel. I'm now just waiting for the official go-ahead to fix it.
In the meantime... if you select the individual files instead of the directories, it will remember your selections between exports.
0 Kudos

Re: ReadAsciiFile returns empty string

Thanks for the update. I should have raised it as a bug had I know the issue was the plugin and not Eclipse.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.