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?
No one have any clue? Who was the developer adding the API?
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..
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?
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?
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
.
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.
Ok, will you post here once you have merged your code? I have a early addon release not working once your change is done...
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*) pBuffer, strlen( pBuffer ) + 1 ) > 0;
i'm testing removing the +1, is that needed for binary writes?
I can't see where:
PHP Code:
return file->Write( (void*) pBuffer, strlen( pBuffer ) ) > 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(filepath, READ_NO_CACHE);
by the above i guess binary writes aren't supported anyways.
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.
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.
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)
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"