[FR] InputStream readString with length method


#1

So to save a variable length string in a binary file I first save an integer value for the string length and then I read the string using the previous length value.

Would it be possible to add this method to InputStream

String InputStream::readString (int stringLength)
{
    HeapBlock<char> stringValue;

    stringValue.calloc (stringLength + 1);

    read (stringValue.getData(), stringLength);

    return String::createStringFromData (stringValue.getData(), stringLength);
}

Thanks,

Rail


Working with Strings in getStateInformation() and setStateInformation
#2

Personally, I'd write that as a free function yourself rather than it being a method of InputStream.

Any reason why you chose to use an integer length? Seems like it'd be worse than a null-termination byte, both in terms of space and performance (?)


#3

[quote]
Any reason why you chose to use an integer length? Seems like it’d be worse than a null-termination byte, both in terms of space and performance (?)[/quote]

Only because I thought it would be faster to calculate the number of bytes to skip if I didn’t need to read the string. It’s also an old school methodology (Pascal Strings) – so I guess I instinctively revert back to the old days… perhaps I’m just showing my age :slight_smile:

Cheers,

Rail


#4

Fair enough. I guess it'd depend on the size of the strings and how often you need to skip them!


#5

If I recall an offsite user preset and the data isn’t available on the local system – then quite often.

Cheers,

Rail