|
44 | 44 | import com.jme3.util.*; |
45 | 45 | import java.io.*; |
46 | 46 | import java.nio.*; |
| 47 | +import java.nio.channels.Channels; |
| 48 | +import java.nio.channels.ReadableByteChannel; |
47 | 49 | import java.util.*; |
48 | 50 | import java.util.logging.Level; |
49 | 51 | import java.util.logging.Logger; |
@@ -904,80 +906,45 @@ public static void dumpMesh(Mesh m) { |
904 | 906 | } |
905 | 907 | } |
906 | 908 |
|
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); |
913 | 916 | } |
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; |
938 | 924 | } |
939 | 925 |
|
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); |
942 | 928 | } |
943 | 929 |
|
944 | | - dst.rewind(); |
| 930 | + dst.flip(); |
945 | 931 | } |
946 | 932 |
|
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); |
951 | 937 | } |
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; |
977 | 944 | } |
978 | 945 |
|
979 | 946 | if (totalRead < bytesToRead) { |
980 | | - throw new IOException("Data ended prematurely "+ totalRead + " < " + bytesToRead); |
| 947 | + throw new IOException("Data ended prematurely " + totalRead + " < " + bytesToRead); |
981 | 948 | } |
982 | 949 | } |
983 | 950 |
|
|
0 commit comments