Fix iolib test case. All junit tests now pass (against Lua 5.1.3 on GNU/Linux).

This commit is contained in:
Ian Farmer
2009-01-04 01:17:10 +00:00
parent 69a438e0d4
commit 410003a612
3 changed files with 15 additions and 21 deletions

View File

@@ -52,8 +52,8 @@ public class IoLib extends LFunction {
public int peek() throws IOException, EOFException; public int peek() throws IOException, EOFException;
// return char if read, -1 if eof, throw IOException on other exception // return char if read, -1 if eof, throw IOException on other exception
public int read() throws IOException, EOFException; public int read() throws IOException, EOFException;
// return length if fully read, false if eof, throw IOException on other exception // return number of bytes read if positive, false if eof, throw IOException on other exception
public int readFully(byte[] bytes, int offset, int length) throws IOException; public int read(byte[] bytes, int offset, int length) throws IOException;
} }
@@ -454,9 +454,10 @@ public class IoLib extends LFunction {
public static LValue freadbytes(File f, int count) throws IOException { public static LValue freadbytes(File f, int count) throws IOException {
byte[] b = new byte[count]; byte[] b = new byte[count];
if ( f.readFully(b,0,b.length) < 0 ) int r;
if ( ( r = f.read(b,0,b.length) ) < 0 )
return LNil.NIL; return LNil.NIL;
return new LString(b); return new LString(b, 0, r);
} }
public static LValue freaduntil(File f,int delim) throws IOException { public static LValue freaduntil(File f,int delim) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();

View File

@@ -187,8 +187,8 @@ public class Cldc10IoLib extends IoLib {
return 0; return 0;
} }
// return length if fully read, -1 if eof, throws IOException // return number of bytes read if positive, -1 if eof, throws IOException
public int readFully(byte[] bytes, int offset, int length) throws IOException { public int read(byte[] bytes, int offset, int length) throws IOException {
int n,i=0; int n,i=0;
if (is!=null) { if (is!=null) {
if ( length > 0 && lookahead >= 0 ) { if ( length > 0 && lookahead >= 0 ) {
@@ -196,10 +196,10 @@ public class Cldc10IoLib extends IoLib {
lookahead = -1; lookahead = -1;
i += 1; i += 1;
} }
for ( ; i<length; ) { for ( ; i<length; ) {
n = is.read(bytes, offset+i, length-i); n = is.read(bytes, offset+i, length-i);
if ( n < 0 ) if ( n < 0 )
return -1; return ( i > 0 ? i : -1 );
i += n; i += n;
} }
} else { } else {

View File

@@ -142,14 +142,12 @@ public class J2seIoLib extends IoLib {
if ( file != null ) { if ( file != null ) {
if ( "set".equals(option) ) { if ( "set".equals(option) ) {
file.seek(pos); file.seek(pos);
return (int) file.getFilePointer();
} else if ( "end".equals(option) ) { } else if ( "end".equals(option) ) {
file.seek(file.length()+1+pos); file.seek(file.length()+pos);
return (int) file.length()+1;
} else { } else {
file.seek(file.getFilePointer()+pos); file.seek(file.getFilePointer()+pos);
return (int) file.getFilePointer();
} }
return (int) file.getFilePointer();
} }
notimplemented(); notimplemented();
return 0; return 0;
@@ -190,17 +188,12 @@ public class J2seIoLib extends IoLib {
return 0; return 0;
} }
// return length if fully read, -1 if eof, throws IOException // return number of bytes read if positive, -1 if eof, throws IOException
public int readFully(byte[] bytes, int offset, int length) throws IOException { public int read(byte[] bytes, int offset, int length) throws IOException {
if (file!=null) { if (file!=null) {
file.readFully(bytes, offset, length); return file.read(bytes, offset, length);
} else if (is!=null) { } else if (is!=null) {
for ( int i=0; i<length; i++, offset++ ) { return is.read(bytes, offset, length);
int c = is.read();
if ( c < 0 )
return -1;
bytes[offset++] = (byte) c;
}
} else { } else {
notimplemented(); notimplemented();
} }