Add some helper methods:

(1) getKeys() in LTable is now public. It provides a convenient interface
    to access the table's elements from Java instead of Lua.
(2) LString.toInputStream produces an instance of InputStream from which
    the string's bytes can be read.
This commit is contained in:
Ian Farmer
2007-09-12 05:22:30 +00:00
parent 31e2d95076
commit 6d0e9bb566
2 changed files with 58 additions and 32 deletions

View File

@@ -1,5 +1,6 @@
package lua.value;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
@@ -161,6 +162,26 @@ public class LString extends LValue {
System.arraycopy( m_bytes, m_offset+strOffset, bytes, arrayOffset, len );
}
/**
* Produce an InputStream instance from which the bytes of this LString can be read.
* Underlying byte array is not copied.
*/
public ByteArrayInputStream toInputStream() {
// Well, this is really something.
// Javadoc for java versions 1.3 and earlier states that if reset() is
// called on a ByteArrayInputStream constructed with the 3-argument
// constructor, then bytes 0 .. offset will be returned by the next
// calls to read(). In JDK 1.4, the behavior improved, so that the
// initial mark is set to the initial offset. We still need to
// override ByteArrayInputStream here just in case we run on a
// JVM with the older behavior.
return new ByteArrayInputStream( m_bytes, m_offset, m_length ) {
public synchronized void reset() {
pos = Math.max( m_offset, mark );
}
};
}
public boolean luaBinCmpUnknown(int opcode, LValue lhs) {
return lhs.luaBinCmpString(opcode, this);
}