From 410003a612e7f93bcca54d187602bfd3ab8a19a7 Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Sun, 4 Jan 2009 01:17:10 +0000 Subject: [PATCH] Fix iolib test case. All junit tests now pass (against Lua 5.1.3 on GNU/Linux). --- src/core/org/luaj/lib/IoLib.java | 9 +++++---- src/j2me/org/luaj/lib/j2me/Cldc10IoLib.java | 8 ++++---- src/j2se/org/luaj/lib/j2se/J2seIoLib.java | 19 ++++++------------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/core/org/luaj/lib/IoLib.java b/src/core/org/luaj/lib/IoLib.java index 2eb19f58..8b42356b 100644 --- a/src/core/org/luaj/lib/IoLib.java +++ b/src/core/org/luaj/lib/IoLib.java @@ -52,8 +52,8 @@ public class IoLib extends LFunction { public int peek() throws IOException, EOFException; // return char if read, -1 if eof, throw IOException on other exception public int read() throws IOException, EOFException; - // return length if fully read, false if eof, throw IOException on other exception - public int readFully(byte[] bytes, int offset, int length) throws IOException; + // return number of bytes read if positive, false if eof, throw IOException on other exception + 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 { 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 new LString(b); + return new LString(b, 0, r); } public static LValue freaduntil(File f,int delim) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/j2me/org/luaj/lib/j2me/Cldc10IoLib.java b/src/j2me/org/luaj/lib/j2me/Cldc10IoLib.java index 32cdfc6c..80048159 100644 --- a/src/j2me/org/luaj/lib/j2me/Cldc10IoLib.java +++ b/src/j2me/org/luaj/lib/j2me/Cldc10IoLib.java @@ -187,8 +187,8 @@ public class Cldc10IoLib extends IoLib { return 0; } - // return length if fully read, -1 if eof, throws IOException - public int readFully(byte[] bytes, int offset, int length) throws IOException { + // return number of bytes read if positive, -1 if eof, throws IOException + public int read(byte[] bytes, int offset, int length) throws IOException { int n,i=0; if (is!=null) { if ( length > 0 && lookahead >= 0 ) { @@ -196,10 +196,10 @@ public class Cldc10IoLib extends IoLib { lookahead = -1; i += 1; } - for ( ; i 0 ? i : -1 ); i += n; } } else { diff --git a/src/j2se/org/luaj/lib/j2se/J2seIoLib.java b/src/j2se/org/luaj/lib/j2se/J2seIoLib.java index 5f970f6c..9430d6b0 100644 --- a/src/j2se/org/luaj/lib/j2se/J2seIoLib.java +++ b/src/j2se/org/luaj/lib/j2se/J2seIoLib.java @@ -142,14 +142,12 @@ public class J2seIoLib extends IoLib { if ( file != null ) { if ( "set".equals(option) ) { file.seek(pos); - return (int) file.getFilePointer(); } else if ( "end".equals(option) ) { - file.seek(file.length()+1+pos); - return (int) file.length()+1; + file.seek(file.length()+pos); } else { file.seek(file.getFilePointer()+pos); - return (int) file.getFilePointer(); } + return (int) file.getFilePointer(); } notimplemented(); return 0; @@ -190,17 +188,12 @@ public class J2seIoLib extends IoLib { return 0; } - // return length if fully read, -1 if eof, throws IOException - public int readFully(byte[] bytes, int offset, int length) throws IOException { + // return number of bytes read if positive, -1 if eof, throws IOException + public int read(byte[] bytes, int offset, int length) throws IOException { if (file!=null) { - file.readFully(bytes, offset, length); + return file.read(bytes, offset, length); } else if (is!=null) { - for ( int i=0; i