DataInputStream类源码设计错误了吗

    我是一个Java初学者,请各位前辈详解。
    昨天在学习Java 流这部分时候,看jdk源代码,个人感觉sun公司在设计DataOutputStream这个类是不恰当甚至是一个BUG。
    DataInputStream中一个方法readInt(),源代码是
    public final int readInt() throws IOException {
        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();
        return ((ch1 << 24) + (ch2 << 16) + (ch3 <<8) + (ch4 << 0));
   }

    其中ch1,ch2,ch3,ch4可能为负数,那样就会抛出异常。
    假入存入的数据为一个正整数,那么ch2<<16 ,ch3 <<8 ,ch4<<0就可能出现负数,如此相加就会出现数据读出的不正确情况。
    这是我的验证移位操作的代码
    byte b = (-1);
    System.out.println(b);
    int i = b;
    i = i << 8;
    System.out.println(i);

    输出结果为 : -1  和  -256
    在DataOutputStream中方法writerInt(),源代码是
    public final void writeInt(int v) throws IOException {
        out.write((v >>> 24) & 0xFF);
        out.write((v >>> 16) & 0xFF);
        out.write((v >>> 8) & 0xFF);
        out.write((v >>>  0) & 0xFF);
        incCount(4);
  }

   两者进行比较我百思不得其解,所以就个人斗胆认为是JDK的源代码出现失误。
   个人建议对readInt()的代码应该修改为如下代码
   public final int readInt() throws IOException {
byte[] b = new byte[4];
for(int i = 0; i<4 ;i++){
b[i] = in.read();
}
        return new BigInteger(b).intValue();
   }

    此外DataInputStream中的readLong(),readShort()等等代码具有这方面的失误。请各位前辈详解,对于一个初学者在这方面是很迷茫的。
    如果对我建议请发邮件给我,我的邮箱为:weizhilee@live.com
    请前辈们原谅在我这里斗胆狂言, 晚生在这里谢谢了。

 

[quote] 其中ch1,ch2,ch3,ch4可能为负数,那样就会抛出异常。[/quote]

in.read的方法,读取的值是 0-255的,是不会有负数的,除非到到末尾。
[quote]
假入存入的数据为一个正整数,那么ch2<<16 ,ch3 <<8 ,ch4<<0就可能出现负数,[/quote]

这是移位运算,是将 ch2乘以 2的16次方 , 是不会为负数的。

关于移位:参考我的博客:
[url]
http://xiaolongfeixiang.iteye.com/blog/648700[/url]

PS: 读源码勇气可嘉,能质疑更有魄力,加油!!

果真是初学者啊

看看read()的api说明
[code="java"]
int java.io.InputStream.read() throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
[/code]
如果文件流结束了会返回-1, 否则返回0-255的int值

public final int readInt() throws IOException这个方法其中ch1,ch2,ch3,ch4可能为负数,那样就会抛出异常。所以在后面的移位操作中不可能出现负的整数,而一个int型的整数占有四个字节,左移24,16,8,0位也不会变为负数。