Fail to read and write file using xbmcvfs.File in Frodo
#1
Hi,
EDIT: I have runned into more issues and have updated the post.

I try to use the new xbmcvfs.File api for reading and writing to files.

PHP Code:
fd_a xbmcvfs.File(file_a)
  
buffer fd_a.read(1024)
  
fd_a.close()
  
fd_b xbmcvfs.File(file_b'w')
  
result fd_b.write(buffer)
  
fd_b.close() 

However I end up getting the error for buffer "TypeError: argument 1 must be string without null bytes, not str"
The problem is that the file I try to read us full of null bytes...
PHP Code:
for line in buffer:
  print 
repr(line)
.....
22:25:28 T:7212  NOTICE'h'
22:25:28 T:7212  NOTICE'\xf8'
22:25:28 T:7212  NOTICE'\x00'
22:25:28 T:7212  NOTICE'\x00'
22:25:28 T:7212  NOTICE'\x00'
22:25:28 T:7212  NOTICE'\x12'
22:25:28 T:7212  NOTICE'g'
22:25:28 T:7212  NOTICE'\x01'
22:25:28 T:7212  NOTICE'\x00'
22:25:28 T:7212  NOTICE'\x00' 

So what should I do?
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#2
No one have any clue? Who was the developer adding the API?
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#3
There's a ticket on trac that may be associated with this:

http://trac.xbmc.org/ticket/13545

Cheers,
Jonathan
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#4
Thanks for your answer!

The ticket is about the the xbmcvfs.write() appends \x00 to the buffer being written. In my case the buffer cannot contain \x00.

If I do:

PHP Code:
fd_a xbmcvfs.File(file_a)
  
buffer fd_a.read(1024)
  
fd_a.close()
  
fd_b =open(local_file_b'wb')
  
fd_b.write(buffer)
  
fd_b.close() 

The local_file_b gets written without any issues..
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#5
We're thinking through ways to resolve this. It will probably result in a change to the current API. It will probably use a bytebuffer in python on both the read and write sides. Plus, MAYBE a string and length on both the read and write sides. How does that work?
Reply
#6
Not sure I understand by byte buffer.

For string and length. Are you saying.

Text="this is some text"
f.write(text, len(text))

This would work I guess, but can't you strip the \x00 character? Isn't that the terminator for char?
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#7
I'm not sure I understand either. Do you have a mock up code example?
All I want is to read and write in a similar fashion as when working with local files (e.g. open(file, 'w')). On top of this I lack the tell() method Big Grin.
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#8
Sorry, given that I primarily deal in java, I said 'bytebuffer' but meant 'bytearray.' The length written would be the exact length of the byte array provided.
Reply
#9
Ok, will you post here once you have merged your code? I have a early addon release not working once your change is done...
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#10
the read seems to work fine for me as is, meaning there does not have to be a \x00 at the end of the file.

PHP Code:
text unicode(xbmcvfs.File(lyrics_path"r").read().strip(BOM_UTF8), "UTF-8"

as for the write.

PHP Code:
return file->Write( (void*) pBufferstrlenpBuffer ) + ) > 0

i'm testing removing the +1, is that needed for binary writes?
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#11
I can't see where:

PHP Code:
return file->Write( (void*) pBufferstrlenpBuffer ) ) > 0

wouldn't work. it seems to work fine. I did not test binary too well, i just wrote the same text as "wb", so not sure if that is a good test.

I do notice open for append doesn't seem supported. Is that something that can be added?

PHP Code:
if (mode && strncmp(mode"w"1) == 0)
          
file->OpenForWrite(filepath,true);
        else if (
mode && strncmp(mode"a"1) == 0)
          
file->OpenForWrite(filepath,false);
        else
          
file->Open(filepathREAD_NO_CACHE); 

by the above i guess binary writes aren't supported anyways.
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#12
I see I was a bit unclear. For writing the buffer cannot contain \x00 and it also appends \x00 at the end of a written file.
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#13
the change above corrects the appending of \x00, but if it's a terminating character, then yes your text to write can not include it.
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#14
I don't fully follow you , all I want is to read and write binary data and not text (e.g. read and write the first 1000 bytes of a big file) Big Grin
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#15
binary reads and writes don't appear to be supported. my issue was \x00 being appended.

PHP Code:
else if (mode && strncmp(mode"a"1) == 0)
          
file->OpenForWrite(filepath,false); 

bOverWrite appears to be ignored, so "a" append does not work"
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply

Logout Mark Read Team Forum Stats Members Help
Fail to read and write file using xbmcvfs.File in Frodo0