开发者

Fedora Eclipse - null characters in io streams

I've run into a very strange problem in Eclipse, and I'm not sure if its a bug or if some settings are just wrong.

I'm using Fedora Eclipse on Fedora 14.

I first noticed this issue when opening a file with null characters in the eclipse text editor. After the first null character was reached, no further characters were displayed. Furthermore, if the file contained a new line after the null everything between the null and newline was not displayed, but resumed properly after the newline.

Example:

#A file with values: 
66 6F 6F 00 62 61 72

#Displays:
foo

#When it should read something like:
fooNULLbar

This wouldn't be more than a minor annoyance if it didn't extend to I/O when running a program in Eclipse. If I try to read the file in the previous example with an InputStream or Reader, it results in an end of file just before the null.

Example:

# File: test
66 6F 6F 00 62 61 72

# File: Test.java
import java.io.File;
import java.io.FileInputStream;

public class Test {
    public static void main( String[] args ) {
        try {
            File file = new File( "test" );

            FileI开发者_JAVA技巧nputStream in = new FileInputStream( file );

            int b;
            while ( ( b = in.read() ) != -1 ) {
            System.out.print( (char) b );
            }

            in.close();
        } catch ( Exception e ) {
            System.err.println( e.getMessage() );
        }
    }
}

# Produces output to Eclipse console:
foo

# While when run from command line, it produces:
fooNULLbar

When I open these files or run these programs outside of Eclipse, everything works correctly including programs compiled in Eclipse.

I came across this while Googling the issue, but I'm not sure if it's the same issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=283231

Thanks in advance for all help.


I think you are interpreting the output incorrectly, i.e. how the string is rendered on screen. Because any normal string will look strange if you use some strange font. A regular unicode string will look something different in one locale but not the other. Even a properly rendered unicode string containing, for e.g. Devnagari characters, will look strange to someone who has never seen those before. Hence the null character probably will look strange. This may be because it is not a character that you want to display in the first place. Also, null character is not the string terminator (as you might be thinking due to C/C++). Java does handle it properly.

As a simple test, just try to print '\0' in the Java program and see what output you get, 'null' or something strange. :)

From the Java Doc:

print

public void print(String s)

Print a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method. Parameters:

s - The String to be printed

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜