Skip to content

Commit b29ff63

Browse files
committed
don't throw EOF exception if the stride steps outside the stream
1 parent ae012d2 commit b29ff63

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

jme3-core/src/plugins/java/com/jme3/export/binary/ByteUtils.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,24 @@ public static void readFully(InputStream in, byte[] b) throws IOException {
6565
}
6666
}
6767

68-
6968

7069
public static void skipFully(InputStream in, long n) throws IOException {
70+
skipFully(in, n, true);
71+
}
72+
73+
public static void skipFully(InputStream in, long n, boolean throwOnEOF) throws IOException {
7174
while (n > 0) {
7275
long skipped = in.skip(n);
7376
if (skipped > 0 && skipped <= n) { // skipped some bytes
7477
n -= skipped;
7578
} else if (skipped == 0) { // skipped nothing
7679
// distinguish between EOF and no bytes available
7780
if (in.read() == -1) {
78-
throw new EOFException();
81+
if (throwOnEOF) {
82+
throw new EOFException();
83+
} else {
84+
return;
85+
}
7986
} else {
8087
// stream was just hangling
8188
n--;
@@ -88,13 +95,25 @@ public static void skipFully(InputStream in, long n) throws IOException {
8895
}
8996

9097
public static void skipFully(DataInput in, int n) throws IOException {
98+
skipFully(in, n, true);
99+
}
100+
101+
public static void skipFully(DataInput in, int n, boolean throwOnEOF) throws IOException {
91102
while (n > 0) {
92103
long skipped = in.skipBytes(n);
93104
if (skipped > 0 && skipped <= n) { // skipped some bytes
94105
n -= skipped;
95106
} else if (skipped == 0) { // skipped nothing
96107
// distinguish between EOF and no bytes available
97-
in.readByte();
108+
try {
109+
in.readByte();
110+
} catch (EOFException e) {
111+
if (throwOnEOF) {
112+
throw e;
113+
} else {
114+
return;
115+
}
116+
}
98117
n--;
99118
} else {
100119
throw new IOException(
@@ -103,6 +122,7 @@ public static void skipFully(DataInput in, int n) throws IOException {
103122
}
104123
}
105124

125+
106126
/**
107127
* Takes an InputStream and returns the complete byte content of it
108128
*

jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.jme3.plugins.json.JsonObject;
3737
import com.jme3.asset.AssetInfo;
3838
import com.jme3.asset.AssetLoadException;
39+
import com.jme3.export.binary.ByteUtils;
3940
import com.jme3.math.*;
4041
import com.jme3.plugins.json.Json;
4142
import com.jme3.plugins.json.JsonParser;
@@ -338,14 +339,14 @@ private static void populateShortBuffer(ShortBuffer buffer, LittleEndien stream,
338339
int dataLength = componentSize * numComponents;
339340
int stride = Math.max(dataLength, byteStride);
340341
int end = count * stride + byteOffset;
341-
stream.skipBytes(byteOffset);
342+
ByteUtils.skipFully((InputStream) stream, byteOffset);
342343
while (index < end) {
343344
for (int i = 0; i < numComponents; i++) {
344345
buffer.put(stream.readShort());
345346
}
346347

347348
if (dataLength < stride) {
348-
stream.skipBytes(stride - dataLength);
349+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
349350
}
350351
index += stride;
351352
}
@@ -358,13 +359,13 @@ private static void populateIntBuffer(IntBuffer buffer, LittleEndien stream, int
358359
int dataLength = componentSize * numComponents;
359360
int stride = Math.max(dataLength, byteStride);
360361
int end = count * stride + byteOffset;
361-
stream.skipBytes(byteOffset);
362+
ByteUtils.skipFully((InputStream) stream, byteOffset);
362363
while (index < end) {
363364
for (int i = 0; i < numComponents; i++) {
364365
buffer.put(stream.readInt());
365366
}
366367
if (dataLength < stride) {
367-
stream.skipBytes(stride - dataLength);
368+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
368369
}
369370
index += stride;
370371
}
@@ -376,13 +377,13 @@ private static void populateFloatBuffer(FloatBuffer buffer, LittleEndien stream,
376377
int dataLength = componentSize * numComponents;
377378
int stride = Math.max(dataLength, byteStride);
378379
int end = count * stride + byteOffset;
379-
stream.skipBytes(byteOffset);
380+
ByteUtils.skipFully((InputStream) stream, byteOffset);
380381
while (index < end) {
381382
for (int i = 0; i < numComponents; i++) {
382383
buffer.put(readAsFloat(stream, format));
383384
}
384385
if (dataLength < stride) {
385-
stream.skipBytes(stride - dataLength);
386+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
386387
}
387388
index += stride;
388389
}
@@ -423,7 +424,7 @@ private static void populateByteArray(byte[] array, LittleEndien stream, int cou
423424
int dataLength = componentSize * numComponents;
424425
int stride = Math.max(dataLength, byteStride);
425426
int end = count * stride + byteOffset;
426-
stream.skipBytes(byteOffset);
427+
ByteUtils.skipFully((InputStream) stream, byteOffset);
427428

428429
if (dataLength == stride) {
429430
read(stream, array, end - index);
@@ -438,7 +439,7 @@ private static void populateByteArray(byte[] array, LittleEndien stream, int cou
438439
System.arraycopy(buffer, 0, array, arrayIndex, numComponents);
439440
arrayIndex += numComponents;
440441
if (dataLength < stride) {
441-
stream.skipBytes(stride - dataLength);
442+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
442443
}
443444
index += stride;
444445
}
@@ -461,7 +462,7 @@ private static void populateShortArray(short[] array, LittleEndien stream, int c
461462
int dataLength = componentSize * numComponents;
462463
int stride = Math.max(dataLength, byteStride);
463464
int end = count * stride + byteOffset;
464-
stream.skipBytes(byteOffset);
465+
ByteUtils.skipFully((InputStream) stream, byteOffset);
465466
int arrayIndex = 0;
466467
while (index < end) {
467468
for (int i = 0; i < numComponents; i++) {
@@ -473,7 +474,7 @@ private static void populateShortArray(short[] array, LittleEndien stream, int c
473474
arrayIndex++;
474475
}
475476
if (dataLength < stride) {
476-
stream.skipBytes(stride - dataLength);
477+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
477478
}
478479
index += stride;
479480
}
@@ -563,15 +564,15 @@ private static void populateFloatArray(float[] array, LittleEndien stream, int c
563564
int dataLength = componentSize * numComponents;
564565
int stride = Math.max(dataLength, byteStride);
565566
int end = count * stride + byteOffset;
566-
stream.skipBytes(byteOffset);
567+
ByteUtils.skipFully((InputStream) stream, byteOffset);
567568
int arrayIndex = 0;
568569
while (index < end) {
569570
for (int i = 0; i < numComponents; i++) {
570571
array[arrayIndex] = readAsFloat(stream, format);
571572
arrayIndex++;
572573
}
573574
if (dataLength < stride) {
574-
stream.skipBytes(stride - dataLength);
575+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
575576
}
576577
index += stride;
577578
}
@@ -583,7 +584,7 @@ private static void populateVector3fArray(Vector3f[] array, LittleEndien stream,
583584
int dataLength = componentSize * numComponents;
584585
int stride = Math.max(dataLength, byteStride);
585586
int end = count * stride + byteOffset;
586-
stream.skipBytes(byteOffset);
587+
ByteUtils.skipFully((InputStream) stream, byteOffset);
587588
int arrayIndex = 0;
588589
while (index < end) {
589590
array[arrayIndex] = new Vector3f(
@@ -594,7 +595,7 @@ private static void populateVector3fArray(Vector3f[] array, LittleEndien stream,
594595

595596
arrayIndex++;
596597
if (dataLength < stride) {
597-
stream.skipBytes(stride - dataLength);
598+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
598599
}
599600

600601
index += stride;
@@ -607,7 +608,7 @@ private static void populateQuaternionArray(Quaternion[] array, LittleEndien str
607608
int dataLength = componentSize * numComponents;
608609
int stride = Math.max(dataLength, byteStride);
609610
int end = count * stride + byteOffset;
610-
stream.skipBytes(byteOffset);
611+
ByteUtils.skipFully((InputStream) stream, byteOffset);
611612
int arrayIndex = 0;
612613
while (index < end) {
613614
array[arrayIndex] = new Quaternion(
@@ -619,7 +620,7 @@ private static void populateQuaternionArray(Quaternion[] array, LittleEndien str
619620

620621
arrayIndex++;
621622
if (dataLength < stride) {
622-
stream.skipBytes(stride - dataLength);
623+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
623624
}
624625
index += stride;
625626
}
@@ -631,7 +632,7 @@ private static void populateMatrix4fArray(Matrix4f[] array, LittleEndien stream,
631632
int dataLength = componentSize * numComponents;
632633
int stride = Math.max(dataLength, byteStride);
633634
int end = count * stride + byteOffset;
634-
stream.skipBytes(byteOffset);
635+
ByteUtils.skipFully((InputStream) stream, byteOffset);
635636
int arrayIndex = 0;
636637
while (index < end) {
637638

@@ -657,7 +658,7 @@ private static void populateMatrix4fArray(Matrix4f[] array, LittleEndien stream,
657658

658659
arrayIndex++;
659660
if (dataLength < stride) {
660-
stream.skipBytes(stride - dataLength);
661+
ByteUtils.skipFully((InputStream) stream, stride - dataLength, false);
661662
}
662663

663664
index += stride;

0 commit comments

Comments
 (0)