这个读文件为什么不可以呢?

来源:互联网 发布:淘宝店铺装修软件大全 编辑:程序博客网 时间:2024/04/30 17:53

 

项目中有个读Stream的问题,我用了最原始的办法,发现当InputStream中是xml可以读出,当是其他的时候就不可以了。虽然后来有了其他方案解决了,但是还是不知道原因,这里贴出来,想知道大家的看法,谢谢.

 

 try {
     Assert.notNull(file);
     InputStream in = file.getFileContent().getInputStream();
     List<Byte> cache = new ArrayList<Byte>();
     byte data = (byte) in.read();
     while (data != -1) {
      cache.add(data);
      data = (byte) in.read();
     }
     if (in != null) {
      in.close();
     }
     byte[] cache2 = new byte[cache.size()];
     for (int i = 0; i < cache.size(); i++) {
      cache2[i] = cache.get(i);
     }
     System.out.println("cache2:  " + cache2.length);
     output.write(cache2);
    } finally {
     if (output != null) {
      output.close();
     }
    }

 

 

===>>

 

 

  try {
     Assert.notNull(file);
     InputStream in = file.getFileContent().getInputStream();
     byte[] cache2 = new byte[in.available()];
     in.read(cache2);
     output.write(cache2);
     if (in != null) {
      in.close();
     }
    } finally {
     if (output != null) {
      output.close();
     }
    }