From dc96c86ce6e65a961e4493c7e59a10b5f40e40bf Mon Sep 17 00:00:00 2001 From: jjenista Date: Fri, 27 Feb 2009 19:56:19 +0000 Subject: [PATCH] Bug fix in detecting EOF for input streams in readLine --- Robust/src/ClassLibrary/FileInputStream.java | 9 +++++---- .../FileInputStreamTest/FileInputStreamTest.java | 14 ++++++++++++++ Robust/src/Tests/FileInputStreamTest/makefile | 9 +++++++++ Robust/src/Tests/FileInputStreamTest/test.txt | 8 ++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java create mode 100644 Robust/src/Tests/FileInputStreamTest/makefile create mode 100644 Robust/src/Tests/FileInputStreamTest/test.txt diff --git a/Robust/src/ClassLibrary/FileInputStream.java b/Robust/src/ClassLibrary/FileInputStream.java index 5a833ade..da717979 100644 --- a/Robust/src/ClassLibrary/FileInputStream.java +++ b/Robust/src/ClassLibrary/FileInputStream.java @@ -33,14 +33,15 @@ public class FileInputStream extends InputStream { int c = read(); // if we're already at the end of the file - // don't even return the empty string - if( c == -1 ) { + // or there is an error, don't even return + // the empty string + if( c <= 0 ) { return null; } - while( c != '\n' && c != -1 ) { + while( c != '\n' && c > 0 ) { line += (char)c; - c = read(); + c = read(); } return line; diff --git a/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java b/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java new file mode 100644 index 00000000..7a32b575 --- /dev/null +++ b/Robust/src/Tests/FileInputStreamTest/FileInputStreamTest.java @@ -0,0 +1,14 @@ +public class FileInputStreamTest { + static public void main( String[] args ) { + FileInputStream in = new FileInputStream( "test.txt" ); + + System.out.println( "#####################" ); + String line = in.readLine(); + while( line != null ) { + System.out.println( line ); + line = in.readLine(); + } + System.out.println( "#####################" ); + in.close(); + } +} \ No newline at end of file diff --git a/Robust/src/Tests/FileInputStreamTest/makefile b/Robust/src/Tests/FileInputStreamTest/makefile new file mode 100644 index 00000000..a59670a6 --- /dev/null +++ b/Robust/src/Tests/FileInputStreamTest/makefile @@ -0,0 +1,9 @@ +all: + ../../buildscript -mainclass FileInputStreamTest *.java + a.bin + +clean: + rm -rf tmpbuilddirectory + rm -f a.bin + rm -f *~ + diff --git a/Robust/src/Tests/FileInputStreamTest/test.txt b/Robust/src/Tests/FileInputStreamTest/test.txt new file mode 100644 index 00000000..060f1240 --- /dev/null +++ b/Robust/src/Tests/FileInputStreamTest/test.txt @@ -0,0 +1,8 @@ +gah + +ya + +yo + +pho +dude -- 2.34.1