Skip to content

Commit 8d10d72

Browse files
committed
further optimize streams by reading straight into the destination when possible
1 parent 3929561 commit 8d10d72

File tree

3 files changed

+33
-66
lines changed

3 files changed

+33
-66
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public Object load(AssetInfo assetInfo) throws IOException {
7878
int chunkType = stream.readInt();
7979
if (chunkType == JSON_TYPE) {
8080
json = new byte[chunkLength];
81-
GltfUtils.readToByteArray(stream, json, chunkLength, -1);
81+
GltfUtils.readToByteArray(stream, json, chunkLength);
8282
} else {
8383
ByteBuffer buff = BufferUtils.createByteBuffer(chunkLength);
84-
GltfUtils.readToByteBuffer(stream, buff, chunkLength, -1);
84+
GltfUtils.readToByteBuffer(stream, buff, chunkLength);
8585
data.add(buff);
8686
}
8787
//8 is the byte size of the 2 ints chunkLength and chunkType.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ protected ByteBuffer getBytes(int bufferIndex, String uri, Integer bufferLength)
658658
BinDataKey key = new BinDataKey(info.getKey().getFolder() + decoded);
659659
try(InputStream input = (InputStream) info.getManager().loadAsset(key)){
660660
data = BufferUtils.createByteBuffer(bufferLength);
661-
GltfUtils.readToByteBuffer(input, data, bufferLength, -1);
661+
GltfUtils.readToByteBuffer(input, data, bufferLength);
662662
}
663663

664664
}

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

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import com.jme3.util.*;
4545
import java.io.*;
4646
import java.nio.*;
47+
import java.nio.channels.Channels;
48+
import java.nio.channels.ReadableByteChannel;
4749
import java.util.*;
4850
import java.util.logging.Level;
4951
import java.util.logging.Logger;
@@ -904,80 +906,45 @@ public static void dumpMesh(Mesh m) {
904906
}
905907
}
906908

907-
public static void readToByteBuffer(InputStream input, ByteBuffer dst, int bytesToRead, int chunkSize) throws IOException {
908-
if(chunkSize==-1){
909-
chunkSize = 1024*1024*16; // 16MB
910-
if(bytesToRead>0 && bytesToRead<chunkSize){
911-
chunkSize = bytesToRead;
912-
}
909+
public static void readToByteBuffer(InputStream input, ByteBuffer dst, int bytesToRead) throws IOException {
910+
if (bytesToRead <= 0) throw new IOException("bytesToRead must be > 0");
911+
912+
int startPos = dst.position();
913+
int remaining = dst.limit() - startPos;
914+
if (remaining < bytesToRead) {
915+
throw new IOException("Destination ByteBuffer too small: remaining=" + remaining + " < bytesToRead=" + bytesToRead);
913916
}
914-
byte chunk[]=new byte[chunkSize]; // 16MB chunk
915-
int totalRead=0;
916-
int read = 0;
917-
918-
while(true){
919-
int toRead = chunkSize;
920-
921-
// if we have a limit on bytes to read
922-
if(bytesToRead>=0){
923-
// if we reached the limit, stop reading
924-
if(totalRead>=bytesToRead) break;
925-
926-
// adjust toRead to not exceed bytesToRead
927-
toRead = Math.min(chunkSize, bytesToRead - totalRead);
928-
}
929-
930-
read = input.read(chunk, 0, toRead);
931-
932-
// if we reached EOF, stop reading
933-
if(read==-1) break;
934-
935-
// put read bytes into dst and update totalRead
936-
dst.put(chunk, 0, read);
937-
totalRead+=read;
917+
918+
ReadableByteChannel ch = Channels.newChannel(input);
919+
int total = 0;
920+
while (total < bytesToRead) {
921+
int n = ch.read(dst);
922+
if (n == -1) break;
923+
total += n;
938924
}
939925

940-
if (totalRead < bytesToRead) {
941-
throw new IOException("Data ended prematurely "+ totalRead + " < " + bytesToRead);
926+
if (total < bytesToRead) {
927+
throw new IOException("Data ended prematurely " + total + " < " + bytesToRead);
942928
}
943929

944-
dst.rewind();
930+
dst.flip();
945931
}
946932

947-
948-
public static void readToByteArray(InputStream input, byte[] dst, int bytesToRead, int chunkSize) throws IOException {
949-
if(chunkSize==-1){
950-
chunkSize = Math.min(1024*1024*16, bytesToRead); // 16MB or less
933+
public static void readToByteArray(InputStream input, byte[] dst, int bytesToRead) throws IOException {
934+
if (bytesToRead < 0) throw new IllegalArgumentException("bytesToRead < 0");
935+
if (bytesToRead > dst.length) {
936+
throw new IOException("Destination array too small: length=" + dst.length + " < bytesToRead=" + bytesToRead);
951937
}
952-
953-
byte chunk[]=new byte[chunkSize];
954-
int totalRead=0;
955-
int read = 0;
956-
957-
while(true){
958-
int toRead = chunkSize;
959-
960-
// if we have a limit on bytes to read
961-
if(bytesToRead>=0){
962-
// if we reached the limit, stop reading
963-
if(totalRead>=bytesToRead) break;
964-
965-
// adjust toRead to not exceed bytesToRead
966-
toRead = Math.min(chunkSize, bytesToRead - totalRead);
967-
}
968-
969-
read = input.read(chunk, 0, toRead);
970-
971-
// if we reached EOF, stop reading
972-
if(read==-1) break;
973-
974-
// put read bytes into dst and update totalRead
975-
System.arraycopy(chunk, 0, dst, totalRead, read);
976-
totalRead+=read;
938+
939+
int totalRead = 0;
940+
while (totalRead < bytesToRead) {
941+
int n = input.read(dst, totalRead, bytesToRead - totalRead);
942+
if (n == -1) break;
943+
totalRead += n;
977944
}
978945

979946
if (totalRead < bytesToRead) {
980-
throw new IOException("Data ended prematurely "+ totalRead + " < " + bytesToRead);
947+
throw new IOException("Data ended prematurely " + totalRead + " < " + bytesToRead);
981948
}
982949
}
983950

0 commit comments

Comments
 (0)