day19/MyBufferedInputStream.java

import java.io.*;
class MyBufferedInputStream 
{
	private InputStream in;
	private int pos=0,count=0;
	private byte[] buf = new byte[1024];

	//一次读一个字节,从缓冲区(字节数组)获取。
	MyBufferedInputStream(InputStream in)
	{
		this.in = in;
	}
	public int myRead() throws IOException
	{
		//通过in对象读取硬盘上的数据,并存储buf中。
		if(count==0)
		{
			count = in.read(buf);
			if(count<0)
				return -1;
			pos=0;
			byte b = buf[pos];
			pos++;
			count--;
			return b&255;//不和255作与运算,就会出现拷贝的文件是0字节。
						 //因为要取四个8位中的有效位:低8位 00000000 00000000 00000000 11111111	255	
						 //就算类型提升前面补的是1,也可以保证有效位低8位是255.
		}
		else if(count>0)
		{
			byte b = buf[pos];
			pos++;
			count--;
			return b&0xff;
		}
		
		return -1;
	}

	public void myClose()throws IOException
	{
		in.close();
	}
}


/*
11111111111110000000000000000011111111110100101011001


byte类型是一个8位,int是四个8位。
byte:-1 ---->int:-1
1111-1111	---> byte型的-1
1111-1111 1111-1111 1111-1111 1111-1111	  ---> int型的-1


返回值类型是Int类型,类型提升了。
00000000 00000000 00000000 11111111		255	
11111111 11111111 11111111 11111111		-1

11111111 --->提升了一个int类型,那不还是-1吗?是-1的原因是因为在8个1前面补的是1导致的。
那么我只要在前面补0,既可以保留原字节数据不变,又可以避免-1的出现。
怎么补0呢?

 11111111 11111111 11111111 11111111
&00000000 00000000 00000000 11111111
------------------------------------
 00000000 00000000 00000000 11111111


*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值