From 28c242159e154c717f5f171b422b1d4fce0c4ca2 Mon Sep 17 00:00:00 2001 From: Toni Helenius Date: Sat, 4 Nov 2023 20:33:58 +0200 Subject: [PATCH] Stream might not have reached the end even if it doesn't return requested amount of bytes --- .../jme3/scene/plugins/gltf/GltfUtils.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java index c641fb4e5a..fc84681a2e 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java @@ -428,8 +428,14 @@ private static void populateByteArray(byte[] array, LittleEndien stream, int cou if (dataLength == stride) { int length = end - index; - int read = stream.read(array, 0, length); - assertReadLength(read, length); + int n = 0; + while (n < length) { + int cnt = stream.read(array, n, length - n); + if (cnt < 0) { + throw new AssetLoadException("Data ended prematurely"); + } + n += cnt; + } return; } @@ -437,9 +443,7 @@ private static void populateByteArray(byte[] array, LittleEndien stream, int cou int arrayIndex = 0; byte[] buffer = new byte[numComponents]; while (index < end) { - int read = stream.read(buffer, 0, numComponents); - assertReadLength(read, numComponents); - + stream.read(buffer, 0, numComponents); System.arraycopy(buffer, 0, array, arrayIndex, numComponents); arrayIndex += numComponents; if (dataLength < stride) { @@ -449,12 +453,6 @@ private static void populateByteArray(byte[] array, LittleEndien stream, int cou } } - private static void assertReadLength(int read, int length) { - if (read < length) { - throw new AssetLoadException("Data ended prematurely"); - } - } - private static void populateShortArray(short[] array, LittleEndien stream, int count, int byteOffset, int byteStride, int numComponents, VertexBuffer.Format format) throws IOException { int componentSize = format.getComponentSize(); int index = byteOffset;