Add CLDC-1.0 based implementation of io libraries.
This commit is contained in:
@@ -21,7 +21,6 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.lib.j2se;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -29,12 +28,13 @@ import java.io.RandomAccessFile;
|
||||
|
||||
import org.luaj.lib.BaseLib;
|
||||
import org.luaj.lib.IoLib;
|
||||
import org.luaj.vm.LNil;
|
||||
import org.luaj.vm.LString;
|
||||
import org.luaj.vm.LTable;
|
||||
import org.luaj.vm.LValue;
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of the lua io library for J2se using RandomAccessFile
|
||||
* to implement seek.
|
||||
*/
|
||||
public class J2seIoLib extends IoLib {
|
||||
|
||||
public static void install( LTable globals ) {
|
||||
@@ -52,27 +52,39 @@ public class J2seIoLib extends IoLib {
|
||||
protected IoLib newInstance(int index) {
|
||||
return new J2seIoLib(index);
|
||||
}
|
||||
|
||||
protected File wrapStdin() throws IOException {
|
||||
return new FileImpl(BaseLib.STDIN != null? BaseLib.STDIN: System.in);
|
||||
}
|
||||
|
||||
protected File openFile(String filename, String mode) throws IOException {
|
||||
boolean isstdfile = "-".equals(filename);
|
||||
boolean isreadmode = mode.startsWith("r");
|
||||
if ( isstdfile )
|
||||
return isreadmode?
|
||||
new FileImpl(BaseLib.STDIN != null? BaseLib.STDIN: System.in):
|
||||
new FileImpl(BaseLib.STDOUT != null? BaseLib.STDOUT: System.out);
|
||||
boolean isappend = mode.startsWith("a");
|
||||
// TODO: handle update mode
|
||||
// boolean isupdate = mode.endsWith("+");
|
||||
RandomAccessFile f = new RandomAccessFile(filename,isreadmode? "r": "rw");
|
||||
if ( isappend ) {
|
||||
protected File wrapStdout() throws IOException {
|
||||
return new FileImpl(BaseLib.STDOUT != null? BaseLib.STDOUT: System.out);
|
||||
}
|
||||
|
||||
protected File openFile( String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode ) throws IOException {
|
||||
RandomAccessFile f = new RandomAccessFile(filename,readMode? "r": "rw");
|
||||
if ( appendMode ) {
|
||||
f.seek(f.length());
|
||||
} else {
|
||||
if ( ! isreadmode )
|
||||
if ( ! readMode )
|
||||
f.setLength(0);
|
||||
}
|
||||
return new FileImpl( f );
|
||||
}
|
||||
|
||||
protected File openProgram(String prog, String mode) throws IOException {
|
||||
final Process p = Runtime.getRuntime().exec(prog);
|
||||
return "w".equals(mode)?
|
||||
new FileImpl( p.getOutputStream() ):
|
||||
new FileImpl( p.getInputStream() );
|
||||
}
|
||||
|
||||
protected File tmpFile() throws IOException {
|
||||
java.io.File f = java.io.File.createTempFile(".luaj","bin");
|
||||
f.deleteOnExit();
|
||||
return new FileImpl( new RandomAccessFile(f,"rw") );
|
||||
}
|
||||
|
||||
private static void notimplemented() {
|
||||
throw new RuntimeException("not implemented");
|
||||
}
|
||||
@@ -145,88 +157,54 @@ public class J2seIoLib extends IoLib {
|
||||
public void setvbuf(String mode, int size) {
|
||||
nobuffer = "no".equals(mode);
|
||||
}
|
||||
public LValue readBytes(int count) throws IOException {
|
||||
if ( file != null ) {
|
||||
byte[] b = new byte[count];
|
||||
file.readFully(b);
|
||||
return new LString(b);
|
||||
}
|
||||
notimplemented();
|
||||
return LNil.NIL;
|
||||
}
|
||||
public LValue readLine() throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
int c;
|
||||
while ( true ) {
|
||||
if ( is != null ) {
|
||||
c = is.read();
|
||||
} else {
|
||||
c = file.read();
|
||||
}
|
||||
if ( c < 0 || c == '\n' )
|
||||
break;
|
||||
baos.write(c);
|
||||
}
|
||||
return ( c < 0 && baos.size() == 0 )?
|
||||
LNil.NIL:
|
||||
new LString(baos.toByteArray());
|
||||
}
|
||||
public LValue readFile() throws IOException {
|
||||
if ( file != null ) {
|
||||
return readBytes((int) (file.length() - file.getFilePointer()));
|
||||
}
|
||||
notimplemented();
|
||||
return null;
|
||||
}
|
||||
public Double readNumber() throws IOException {
|
||||
if ( is == null && file == null )
|
||||
notimplemented();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
readChars(" \t\r\n",null);
|
||||
readChars("-+",baos);
|
||||
//readChars("0",baos);
|
||||
//readChars("xX",baos);
|
||||
readChars("0123456789",baos);
|
||||
readChars(".",baos);
|
||||
readChars("0123456789",baos);
|
||||
//readChars("eEfFgG",baos);
|
||||
// readChars("+-",baos);
|
||||
//readChars("0123456789",baos);
|
||||
String s = baos.toString();
|
||||
return s.length()>0? Double.valueOf(s): null;
|
||||
}
|
||||
private void readChars(String chars, ByteArrayOutputStream baos) throws IOException {
|
||||
int c;
|
||||
while ( true ) {
|
||||
if ( is != null ) {
|
||||
is.mark(1);
|
||||
c = is.read();
|
||||
} else {
|
||||
c = file.read();
|
||||
}
|
||||
if ( chars.indexOf(c) < 0 ) {
|
||||
if ( is != null )
|
||||
is.reset();
|
||||
else if ( file != null )
|
||||
file.seek(file.getFilePointer()-1);
|
||||
return;
|
||||
}
|
||||
if ( baos != null )
|
||||
baos.write( c );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected File openProgram(String prog, String mode) throws IOException {
|
||||
final Process p = Runtime.getRuntime().exec(prog);
|
||||
return "w".equals(mode)?
|
||||
new FileImpl( p.getOutputStream() ):
|
||||
new FileImpl( p.getInputStream() );
|
||||
}
|
||||
|
||||
protected File tmpFile() throws IOException {
|
||||
java.io.File f = java.io.File.createTempFile(".luaj","bin");
|
||||
f.deleteOnExit();
|
||||
return new FileImpl( new RandomAccessFile(f,"rw") );
|
||||
// get length remaining to read
|
||||
public int remaining() throws IOException {
|
||||
return file!=null? (int) (file.length()-file.getFilePointer()): -1;
|
||||
}
|
||||
|
||||
// peek ahead one character
|
||||
public int peek() throws IOException {
|
||||
if ( is != null ) {
|
||||
is.mark(1);
|
||||
int c = is.read();
|
||||
is.reset();
|
||||
return c;
|
||||
} else if ( file != null ) {
|
||||
int c = file.read();
|
||||
file.seek(file.getFilePointer()-1);
|
||||
return c;
|
||||
}
|
||||
notimplemented();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return char if read, -1 if eof, throw IOException on other exception
|
||||
public int read() throws IOException {
|
||||
if ( is != null )
|
||||
return is.read();
|
||||
else if ( file != null ) {
|
||||
return file.read();
|
||||
}
|
||||
notimplemented();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return length if fully read, -1 if eof, throws IOException
|
||||
public int readFully(byte[] bytes, int offset, int length) throws IOException {
|
||||
if (file!=null) {
|
||||
file.readFully(bytes, offset, length);
|
||||
} else if (is!=null) {
|
||||
for ( int i=0; i<length; i++, offset++ ) {
|
||||
int c = is.read();
|
||||
if ( c < 0 )
|
||||
return -1;
|
||||
bytes[offset++] = (byte) c;
|
||||
}
|
||||
} else {
|
||||
notimplemented();
|
||||
}
|
||||
return length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user