[kaffe] InputStream#available() for System.in throws IOException

Ito Kazumitsu kaz at maczuka.gcd.org
Mon Aug 29 21:57:07 PDT 2005


From: Ito Kazumitsu <kaz at maczuka.gcd.org>
Subject: [kaffe] InputStream#available() for System.in throws IOException
Date: Tue, 30 Aug 2005 01:54:52 +0900 (JST)

> Case 1: Terminal input
> $ kaffe TestBufferedReader
> aaaa
> java.io.IOException: No such file or directory

> Case 2: Pipe
> $ echo aaaa | kaffe TestBufferedReader     
> java.io.IOException: No such file or directory

> Case 3: File input - OK
> $ kaffe TestBufferedReader <TestBufferedReader.java 

> Case 4: Here document - OK
> $ kaffe TestBufferedReader <<EOF
> > aaaa
> > EOF

The message text of the IOException is meaningless.
This IOException comes from the macro TARGET_NATIVE_FILE_AVAILABLE
defined in libraries/clib/target/Linux/target_native_file.h.

If the standard input is piped to another process or it is a
character device, KFSTAT(filedescriptor,&__statBuffer) returns 0
but S_ISREG(__statBuffer.st_mode) is false because it is not a
regular file.

The solution may be either

  (a) if S_ISREG(__statBuffer.st_mode) is false, try other
      methods as libraries/clib/nio/FileChannelImpl.c did before.

or

  (b) if S_ISREG(__statBuffer.st_mode) is false, give up trying
      to figure out available length and just return 0.

I think (b) is simple and reasonable enough. Running the following
program on Sun's JDK,

public class TestAvailable {
    public static void main(String[] args) throws Exception {
        System.out.println(System.in.available());
    }
}

I find Sun's System.in.available() often returns 0 if the standerd
input is a terminal or is piped to another process.

So here is my proposed patch.

--- libraries/clib/target/Linux/target_native_file.h.orig	Fri Aug  5 10:20:19 2005
+++ libraries/clib/target/Linux/target_native_file.h	Tue Aug 30 13:31:24 2005
@@ -138,18 +138,26 @@
         \
         length=0; \
         \
-        if ((KFSTAT(filedescriptor,&__statBuffer)==0) && S_ISREG(__statBuffer.st_mode)) \
+        if (KFSTAT(filedescriptor,&__statBuffer)==0) \
         { \
-	  int klseek_result; \
-          klseek_result=(KLSEEK(filedescriptor,0,SEEK_CUR, &__n));	\
-          if (klseek_result == 0) \
+          if (S_ISREG(__statBuffer.st_mode)) \
           { \
-            length=TARGET_NATIVE_MATH_INT_INT32_TO_INT64(__statBuffer.st_size-__n); \
-            result=TARGET_NATIVE_OK; \
+            int klseek_result; \
+            klseek_result=(KLSEEK(filedescriptor,0,SEEK_CUR, &__n));	\
+            if (klseek_result == 0) \
+            { \
+              length=TARGET_NATIVE_MATH_INT_INT32_TO_INT64(__statBuffer.st_size-__n); \
+              result=TARGET_NATIVE_OK; \
+            } \
+            else \
+            { \
+              result=TARGET_NATIVE_ERROR; \
+            } \
           } \
           else \
           { \
-            result=TARGET_NATIVE_ERROR; \
+              length=TARGET_NATIVE_MATH_INT_INT32_TO_INT64(0); \
+              result=TARGET_NATIVE_OK; \
           } \
         } \
         else \




More information about the kaffe mailing list