WriteBytes ?

BlitzMax Forums/BlitzMax Programming/WriteBytes ?

Filax(Posted 2005) [#1]
Hi :)

I'm looking under module code stream and i see a writebytes()
method, the question is :

It is possible to use bank with this method ? because when i try
to do this with bank my output file are corrupted ?

Is see in the code

WriteBytes( buf:Byte Ptr,count )

The forst arg is Byte ? not TBank ? it is normal ?

exemple don"t work :

Local CurrentPakFile:TStream=ReadStream(Pakname)
Local Tmp_CompressSize:Int=200000
Local SourceBank:TBank

SourceBank=TBank.Create(Tmp_CompressSize)
CurrentPakFile.Writebytes(SourceBank,Tmp_CompressSize)

If i want do do the same, wihout bug, i must use my hown writebytes function :

Function MPK_WriteBytes(File:TStream,Bank:TBank)
For Local I=0 To BankSize(Bank)-1
WriteByte(File,PeekByte(Bank,i))
Next
End Function


ziggy(Posted 2005) [#2]
WriteBytes spect a pointer, so you have to pass a TBank.buf instead of the TBank object itself.

CurrentPakFile.Writebytes(SourceBank.buf(),Tmp_CompressSize)

this way you're passing the bank buffer, and not the bank buffer object.


Filax(Posted 2005) [#3]
ok ok !!! many thanks ziggy


ImaginaryHuman(Posted 2005) [#4]
I usually use stream.write(), you pass it the buffer address of the bank and how many bytes to write.


ziggy(Posted 2005) [#5]
be carefull doing stream.write. Some streams uses a CString pointer, so any null byte (byte zero) could corrupt the output data. This doesn't happens with WriteBytes. And this doesn't happens with all streams, just text oriented streams (such as standardiostream). And there's one thing else, your bytes, when using write, all characters greater than 127 will be written in UTF8 format (because this is the default and most compatible cross platform format in BlitzMax) so this conversion to UTF8 will make bytes greater than 127 be represented in a 16 bit format (so the lenght of the streamed data could be greater than the original, and some BYES will be changed unless they're currently representin an UTF8 value).
So, as a suggestion, I recomand you to use WRITE for TEXT, and WRITEBYTES for Bytes.

:)
It was only a suggestion...


ImaginaryHuman(Posted 2005) [#6]
Interesting. I didn't find any of the above problems with using stream.write(). I drew images on the backbuffer and then grabbed then with glReadPixels into a bank, then simply used stream.write with the bank's buffer pointer and the size of the bank in bytes. The resulting byte size was as expected (width*height*bytesperpixel), and when reading this into a graphics application as RAW format showed onscreen the same as the original. The image being saved had all values ranging from 0 through 255.


ziggy(Posted 2005) [#7]
Gey, notice that This may only happen when using TEXT oriented STREAMS. And I'm not sure if the UTF8 conversion happens when the stream contens is sent to the standardiostream (and displayed in console), or when it is written. For what you say, it may happens when it's sent to the standardiostream, so coping them won't make any change to the data. and in addition, if the lenght parameter is ok, it should skip the zero termination mark, but take in cosideration that, even if it's working, you're accessing 'out of bounds' data, and I'm not sure if it can produce any unspected memory exception.
Anyway, it was only a suggestion.
:)
happy coding!