Skip to content

Use FileStream to write json gltf #232

@xPaw

Description

@xPaw

Currently trying to save a file over 2gib results in this exception:

System.OutOfMemoryException: Cannot allocate a buffer of size 2147487611.
   at System.Text.Json.ThrowHelper.ThrowOutOfMemoryException(UInt32 capacity)
   at System.Text.Json.Utf8JsonWriter.Grow(Int32 requiredSize)
   at SharpGLTF.IO._JSonSerializationExtensions.TryWriteValue(Utf8JsonWriter writer, Object value)

I believe this is due to the use of MemoryStream (which itself is limited to 2gib). Specifically here:

using (var m = new MemoryStream())
{
model._WriteJSON(m, this.JsonOptions, this.JsonPostprocessor);
string finalName = Path.HasExtension(name) ? name : $"{name}.gltf";
WriteAllBytesToEnd(finalName, m.ToArraySegment());
}

I believe switching this to a direct FileStream would solve this, and not require keeping the entire JSON object in memory.

EDIT: This can also be done in WriteBinarySchema2.

This may also require calling Utf8JsonWriter.Flush() in strategic places.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions