import java.io.IOException;
import java.io.InputStream;public class myOwnStream1
{
public static void main(String[] args) throws Exception
{
byte[] b = new byte[16];
for (int i = 0; i < b.length; i++)
{
b[i] = (byte) i;
} MyByteArrayInuputStream mbais = new MyByteArrayInuputStream(b); while (true)
{
int c = mbais.read();
if (c < 0)
{
break;
}
System.out.print(c + " ");
} mbais.close();
}
}class MyByteArrayInuputStream extends InputStream
{
protected byte[] data; protected int ptr = 0; public MyByteArrayInuputStream(byte[] b)
{
this.data = b;
} @Override
public int read() throws IOException
{
return (ptr < data.length) ? (data[ptr++]) : -1;
// return (ptr < data.length) ? (data[ptr+1]) : -1;
}}
如题,read()里面注释掉那句,自己写时候随手一个+1,没想到造成死循环,下标一直是0inputstreamread()
ptr+1指针都不动,一直读那一块还不死循环啊!ptr值都不变的!
ptr+1 //ptr不会变化,一直都是0,所以read()永远不会返回-1,即造成死循环
明白了,没加到自身上面去。