diff --git a/src/core/org/luaj/vm/LoadState.java b/src/core/org/luaj/vm/LoadState.java index f394bf5d..592d3cfa 100644 --- a/src/core/org/luaj/vm/LoadState.java +++ b/src/core/org/luaj/vm/LoadState.java @@ -76,22 +76,38 @@ public class LoadState { /** The VM doing the loading */ LuaState L; - int loadByte() throws IOException { - return is.readUnsignedByte(); - } - + /** Read buffer */ + private byte[] buf = new byte[4]; + + private static int[] EMPTY_INT_ARRAY = {}; + int loadInt() throws IOException { - if ( this.luacLittleEndian ) { - int a = is.readUnsignedByte(); - int b = is.readUnsignedByte(); - int c = is.readUnsignedByte(); - int d = is.readUnsignedByte(); - return (d << 24) | (c << 16) | (b << 8) | a; - } else { - return is.readInt(); - } + is.readFully(buf,0,4); + return luacLittleEndian? + (buf[3] << 24) | ((0xff & buf[2]) << 16) | ((0xff & buf[1]) << 8) | (0xff & buf[0]): + (buf[0] << 24) | ((0xff & buf[1]) << 16) | ((0xff & buf[2]) << 8) | (0xff & buf[3]); } + int[] loadIntArray() throws IOException { + int n = loadInt(); + if ( n == 0 ) + return EMPTY_INT_ARRAY; + + // read all data at once + int m = n << 2; + if ( buf.length < m ) + buf = new byte[m]; + is.readFully(buf,0,m); + int[] array = new int[n]; + for ( int i=0, j=0; i