黑马程序员技术交流社区

标题: 自定义字节流缓冲区错误 [打印本页]

作者: 张振纲    时间: 2012-8-10 22:41
标题: 自定义字节流缓冲区错误
  1. import java.io.*;
  2. class MyBufferedInputStream
  3. {
  4.         private InputStream is;
  5.         private int count=0;
  6.         private int n=0;
  7.         private byte[] date = new byte[1024];
  8.         MyBufferedInputStream(InputStream is)
  9.         {
  10.                 this.is=is;
  11.         }

  12.         public int myRead() throws IOException
  13.         {
  14.                 if(count==0) //如果数据被取空了就重新取数据
  15.                 {
  16.                         count=is.read(date); //从文件中读出数据并且存入字节数组中
  17.                         if (count==-1)
  18.                                 return -1;
  19.                         byte b = date[n]; //从数组中取出一个字节
  20.                         count--;
  21.                         n++;
  22.                         return b&255;  //返回这个字节

  23.                        
  24.                 }
  25.                 if (count>0) //如果有数据,就继续取
  26.                 {
  27.                        
  28.                         byte b = date[n]; //从数组中取出一个字节
  29.                         count--;
  30.                         n++;
  31.                         return b&255;  //返回这个字节
  32.                 }
  33.                 else
  34.                         return -1;
  35.         }

  36.         public void myClose() throws IOException
  37.         {
  38.                 is.close();
  39.         }
  40. }
  41. class Buffered
  42. {
  43.         public static void main(String[] args)  throws IOException
  44.         {
  45.                 FileInputStream fis = new FileInputStream("6PU02J4D2IIQ0031.jpg");
  46.                 FileOutputStream fos = new FileOutputStream("111copy.jpg");
  47.                
  48.                 MyBufferedInputStream mbis = new MyBufferedInputStream(fis);
  49.                
  50.                 int c = 0;

  51.                 while ((c = mbis.myRead())!=-1)  //判断流中是否还有数据
  52.                 {
  53.                         fos.write(c);
  54.                 }
  55.        
  56.                

  57.                 fos.close();

  58.                 mbis.myClose();

  59.         }
  60. }
复制代码
为什么会发现数组越界异常
拷贝文本的时候没有问题,拷贝图片为什么会出现问题
二楼上图
作者: 张振纲    时间: 2012-8-10 22:42

这是报错图
作者: 张振纲    时间: 2012-8-10 22:42
这是要拷贝的图片

6PU02J4D2IIQ0031.jpg (171.76 KB, 下载次数: 5)

6PU02J4D2IIQ0031.jpg

作者: 张振纲    时间: 2012-8-10 23:11
为什么没人知道呢?
作者: 杨文宇    时间: 2012-8-10 23:19
import java.io.*;
class MyBufferedInputStream
{
        private InputStream is;
        private int count=0;
        private int n=0;
        private byte[] date = new byte[2];
        MyBufferedInputStream(InputStream is)
        {
                this.is=is;
        }

        public int myRead() throws IOException
        {
                if(count==0) //如果数据被取空了就重新取数据
                {
                        n = 0;//n需要重置为0,如果不重置为0,一直加上去,肯定越界啊
                        count=is.read(date); //从文件中读出数据并且存入字节数组中
                        if (count==-1){
                                return -1;
                        }
                        byte b = date[n]; //从数组中取出一个字节,这时候应该又从角标0开始读取数据
                        count--;
                        n++;
                        return b&255;  //返回这个字节


                }
                else  if (count>0) //如果有数据,就继续取
                {

                        byte b = date[n]; //从数组中取出一个字节
                        count--;
                        n++;
                        return b&255;  //返回这个字节
                }
                else
                        return -1;
        }

        public void myClose() throws IOException
        {
                is.close();
        }
}
public class Test
{
        public static void main(String[] args)  throws IOException
        {
                FileInputStream fis = new FileInputStream("F:\\11.jpg");
                FileOutputStream fos = new FileOutputStream("E:\\22.jpg");

                MyBufferedInputStream mbis = new MyBufferedInputStream(fis);

                int c = 0;

                while ((c = mbis.myRead())!=-1)  //判断流中是否还有数据
                {
                        fos.write(c);
                }



                fos.close();

                mbis.myClose();

        }
}

作者: 张振纲    时间: 2012-8-10 23:47
杨文宇 发表于 2012-8-10 23:19
import java.io.*;
class MyBufferedInputStream
{

太感谢了,终于找到原因了
作者: 杨文宇    时间: 2012-8-10 23:49
张振纲 发表于 2012-8-10 23:47
太感谢了,终于找到原因了

没事,应该的,那些小错误有的时候还是比较难找的:handshake




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2