---
title: "为什么InputStream.read()读取一个byte确返回一个int呢？"
author: "OpenMind"
date: "2013-05-09T16:26:13Z"
tags: []
categories: ["编程语言"]
slug: "inputstream-read-byte-int"
draft: false
lastmod: "2013-05-09T16:50:54Z"
id: 1866096
url: "https://openmind.iteye.com/blog/1866096"
---

**问题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无符号右移&gt;&gt;&gt;肯定是正数，即\[0,255]，用byte强转型后就可能负数\[-128,127]。

 

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

 

**延伸3:**

 OutputStream.write(int b)：相当与write(b&255),

如果你write(-2)，因为-2&255=254，那么相应的r = InputStream.read()返回的是254，需要(int) (byte) r才能获取你想要的值（(byte)254=-2）

 

结论：可以认为OutputStream和InputStream里面都是unsigned byte。