论坛首页 编程语言技术论坛

为什么InputStream.read()读取一个byte确返回一个int呢?

浏览 2942 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-05-09  

问题1:为什么InputStream.read()读取一个byte却返回一个int呢?

InputStream.read()返回一个unsigned byte [0 - 255],而java里面没有这个类型,所以用int接收。

byte的范围是[-128,127],所以如果read()返回的数在[128,255]的范围内时,则表示负数,即

 

(byte)128=-128

 

(byte)129=-127

 

(byte)255=-1

 

所以如果read()返回的是byte的话,那就会有负数。而"返回-1意味着结束",这个信息量用byte是无法表达的,所以必须用int。

 

问题2:如何把一个整数(int)正确保存到一个byte数组,并从里面正确的读出来?

先看代码再解释:

	public static int readInt(byte[] readBuffer, int offset) {
		int i = offset;
		//readBuffer[i]范围是[-128,127],转整形时必须是[0,255]
		int ch1 = readBuffer[i++] & 255;
		int ch2 = readBuffer[i++] & 255;
		int ch3 = readBuffer[i++] & 255;
		int ch4 = readBuffer[i++] & 255;
		return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
	}

	public static void writeInt(int v, byte[] writeBuffer, int offset) {
		int i = offset;
		//这里面会存在负数
		writeBuffer[i++] = (byte) (v >>> 24);
		writeBuffer[i++] = (byte) (v >>> 16);
		writeBuffer[i++] = (byte) (v >>> 8);
		writeBuffer[i++] = (byte) (v >>> 0);
	}

 

 

对于writeInt:v无符号右移>>>肯定是正数,即[0,255],用byte强转型后就可能负数[-128,127]。

 

对于readInt:readBuffer[i++]是有负数的,必须转成无符号整形,即&255变成[0,255]范围之后才可以用于组装int。 

 

延伸3:to be continue

 

 

   发表时间:2013-05-11  
细节很重要哇
0 请登录后投票
   发表时间:2013-05-14  
to be continue
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics