读源码之旅 java.io包

对于下图,研究了一下常用的InputStream,ByteArrayInputStream,BufferedInputStream,FileIputStream,ObjectInputStream,DataInputStream 以及相对应的OutStream类。

[img]http://dl.iteye.com/upload/attachment/199829/9c90b2c0-738a-3efe-acd7-be86171c779e.jpg[/img]

看完还是有一些收获的:
1、对其整体结构更清晰了一些,基本上在什么场合想用哪一个流心里都比较有数了。

2、明白了为什么一些类在write后,还要flush一下。举一个例子,看一下BufferedOutputStream里面的几个方法就明白
public synchronized void write(int b) throws IOException {
if (count >= buf.length) {
flushBuffer();
}
buf[count++] = (byte)b; //这里的write作用是把数组放入buf数组,作当缓冲
}
public synchronized void flush() throws IOException {
flushBuffer(); //写入数据
out.flush(); //调用包装的类的flush方法
}
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}

3、有一些Stream关闭不关闭都是一样的,例如像 ByteArrayInputStream

4、ByteArrayInputStream类有如下方法:
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}


注意这里的 0xff,搜索一些资料
[quote]0xFF is hexadecimal, you can Wikipedia that part.
FF is a representation of
00000000 00000000 00000000 11111111
(a 32-bit integer)
& means bit-wise "and", and so when you use it on two ints, each pair of bits from those two ints is and-ed and the result is placed in the resultant int:
Example (showing 16 bits only)

0101 1100 1010 1100
&0000 0000 1111 1111
-----------------
=0000 0000 1010 1100[/quote]

[quote]Bytes are signed in Java. In binary 0x00 is 0, 0x01 is 1 and so on but all 1s (ie 0xFF) is -1, oxFE is -2 and so on. See Two's complement, which is the binary encoding mechanism used.
http://en.wikipedia.org/wiki/Two's_complement[/quote]

5、比较复杂的类:ObjectOutputStream,要花多一些时间去看

6、研究RandomAccessFile类,可以复习一下按位运算符。
运算符 符号
按位与 &
按位左移 <<
按位取反 ~
按位或 |
按位右移 >>
按位异或 ^
无符号右移 >>>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值