Problem writing text as an individual element to a Binarystream disk file

Problem writing text as an individual element to a Binarystream disk file.

Is there a string limit using the WritePString to disk? I see it’s a Pascal string in the docs, so it might have a 255 char limit.

It appears to be chopping off my TextArea text after it’s written to disk, the TeaxtArea contains html.

Any idea what’s happening and a way around this?

Yes, PString is limited to 255 bytes. I use these extension methods when I don’t know how big a string will be. Hmm, maybe you wouldn’t want to define the encoding there. BString uses 4 bytes for the size instead of PStrings 1.

[code]Sub writeBString(extends bs As BinaryStream, s As String)
bs.WriteUInt32(s.LenB)
bs.Write(s)
End Sub

Function readBString(extends bs As BinaryStream) As String
dim size As UInt32 = bs.ReadUInt32
if size > 0 then
return bs.Read(size).DefineEncoding(Encodings.UTF8)
else
return “”
end
End Function
[/code]

Do you have to use a BinaryStream / File? An sqlite database file would be a better choice if you have any flexibility in what you use. And yes, a PString is limited to 255 bytes (not chars, bytes).

Yes, a Pstring starts with a byte that gives the length of the string and so a Pstring is limited to 255 bytes. When I use a binary stream and want to output text I first write out an a value stating the give the length, in bytes, of the string. That is then followed with the string itself. Something like:

sz = LenB(pName) bs.WriteShort sz bs.Write pName sz = LenB(aName) bs.WriteShort sz bs.Write aName sz = LenB(aType) bs.WriteShort sz bs.Write aType
Here I am writing the length to a Short (2 byte) value as I know for sure that none of the strings will exceed. Later to read these three items back in the code looks like:

sz = bs.ReadShort pName = bs.Read(sz, Encodings.UTF8) // preference name sz = bs.ReadShort aName = bs.Read(sz, Encodings.UTF8) // application name sz = bs.ReadShort aType = bs.Read(sz, Encodings.UTF8)

Oh that’s better, I’ll add encodings like that in my code. I’d suggest wrapping it in extension methods then your code looks like…

bs.writeBString pName bs.writeBString aName bs.WriteBstring aType

pName = bs.readBString aName = bs.readBString aType = bs.readBString

Maybe it’d be better to have WriteP2String, ReadP2String for a 2byte short size and WriteP4String, ReadP4String for a 4byte size.

Thanks everyone!

Will, that worked beautifully. :slight_smile:

I’m assuming changing the Int type in the method for that purpose is what you’re referring to here…

yes :slight_smile: