【问题标题】:InputStream.available() and reading file compeletly notes from oracleInputStream.available() 并从 oracle 读取文件完全注释
【发布时间】:2014-08-29 14:18:14
【问题描述】:

根据:

请注意,虽然 InputStream 的某些实现会返回 流中的总字节数,很多不会。它从来不是 正确使用此方法的返回值分配缓冲区 旨在保存此流中的所有数据。

来自:

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29

还有这个笔记

In particular, code of the form

int n = in.available();
byte buf = new byte[n];
in.read(buf); 

is not guaranteed to read all of the remaining bytes from the given input stream.

http://docs.oracle.com/javase/8/docs/technotes/guides/io/troubleshooting.html

这是否意味着使用below function 导致无法完全读取文件?

/**
 * Reads a file from /raw/res/ and returns it as a byte array
 * @param res Resources instance for Mosembro
 * @param resourceId ID of resource (ex: R.raw.resource_name)
 * @return byte[] if successful, null otherwise
 */
public static byte[] readRawByteArray(Resources res, int resourceId)
{
    InputStream is = null;
    byte[] raw = new byte[] {};
    try {
        is = res.openRawResource(resourceId);
        raw = new byte[is.available()];
        is.read(raw);
    }
    catch (IOException e) {
        e.printStackTrace();
        raw = null;
    }
    finally {
        try {
            is.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    return raw;
}

【问题讨论】:

    标签: java android file-io inputstream bufferedreader


    【解决方案1】:

    available() 返回可以读取的字节数没有阻塞。该数字(可以为零)与文件的总长度之间没有必要的相关性。

    【讨论】:

      【解决方案2】:

      是的,它不一定会全部阅读。喜欢RandomAccessFile.read(byte[]) 而不是RandomAccessFile.readFully(byte[])。此外,代码实际上在物理上读取了 0 个字节。

      如果它是像文件系统这样的慢速设备,它可能只读取第一个块。

      原理: 该文件正在被底层系统软件读取,通常 缓冲,所以你有几个块已经在内存中,并且 有时已经进一步阅读。软件异步读取 块,如果尝试读取的内容超出系统的范围,则 块 已经读过了。

      所以一般来说,软件中有一个块的读取循环,并且在读取时定期读取操作块,直到物理读取足够缓冲。

      要实现非阻塞,您需要这样做:

      InputStream is = res.openRawResource(resourceId);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      for (;;) {
          // Read bytes until no longer available:
          for (;;) {
              int n = is.available();
              if (n == 0) {
                  break;
              }
              byte[] part = new byte[n];
              int nread = is.read(part);
              assert nread == n;
              baos.write(part, 0, nread);
          }
          // Still a probably blocking read:
          byte[] part = new byte[128];
          int nread = is.read(part);
          if (nread <= 0) {
              break; // End of file
          }
          baos.write(part, 0, nread);
      }
      return baos.toByteArray();
      

      现在,在您复制该代码之前,立即执行阻塞读取循环。我看不到使用available() 的优势,除非您可以在读取其余数据的同时对部分数据进行操作。

      【讨论】:

      • +1 表示您的答案,但the code actually physically reads 0 bytes. 我不这么认为
      • 而且我没有看到我的问题与阅读阻塞或非阻塞有任何联系。
      • 我没有说明的一件事:available() 返回可以在没有阻塞的情况下读取的字节数(= 已经缓冲)。所以available() 可以在任何时候产生 0,即在一开始。我的论点:一个人可以做阻塞读取,而不会受到惩罚。只有在做更多的工作时,比如在循环中读取 N 个文件available() 可能是值得的。 我希望有人过来,想把这个案子表述得更清楚一点。
      • @mmlooloo 连接是 hat 是可用()返回的定义。将其用作流的总长度是滥用它。
      猜你喜欢
      • 2019-12-06
      • 2015-03-07
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多