【问题标题】:IntelliJ "result of inputstream.read is ignored" - how to fix?IntelliJ“inputstream.read 的结果被忽略” - 如何解决?
【发布时间】:2016-10-30 00:48:23
【问题描述】:

我正在努力修复我的应用程序中的一些潜在错误。我正在使用 Sonar 来评估我的代码。我的问题是这样的:

private Cipher readKey(InputStream re) throws Exception {
    byte[] encodedKey = new byte[decryptBuferSize];
    re.read(encodedKey); //Check the return value of the "read" call to see how many bytes were read. (the issue I get from Sonar)


    byte[] key = keyDcipher.doFinal(encodedKey);
    Cipher dcipher = ConverterUtils.getAesCipher();
    dcipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
    return dcipher;
}

这是否意味着字节数组为空?为什么会被忽略? 我从来没有使用过字节,所以我想知道这个问题到底意味着什么以及如何解决它。感谢您的帮助!

【问题讨论】:

    标签: java intellij-idea bytearray inputstream


    【解决方案1】:

    这是否意味着字节数组为空?

    不 - 这不是错误

    看看 read(byte[]) 方法的定义:

    public abstract class InputStream extends Object implements Closeable {
    
         /**
         * Reads up to {@code byteCount} bytes from this stream and stores them in
         * the byte array {@code buffer} starting at {@code byteOffset}.
         * Returns the number of bytes actually read or -1 if the end of the stream
         * has been reached.
         *
         * @throws IndexOutOfBoundsException
         *   if {@code byteOffset < 0 || byteCount < 0 || byteOffset + byteCount > buffer.length}.
         * @throws IOException
         *             if the stream is closed or another IOException occurs.
         */
        public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
    
           ....
        }
    
    }
    

    那么 IDE 表示什么?您省略了 read 方法的结果 - 这是实际读取的字节数,如果流结束则为 -1已到达。

    如何解决?

    如果你关心有多少字节被读取到字节缓冲区:

      // define variable to hold count of read bytes returned by method 
      int no_bytes_read = re.read(encodedKey);
    

    你为什么要关心???

    1. 因为当您从流中读取时,您通常将缓冲区作为参数传递,特别是当您不知道流所携带的数据大小或您想按部分读取时(在这种情况下,您传递的字节数组为 decryptedBuferSize 大小-> 新字节[decryptBuferSize]) 。
    2. 一开始字节缓冲区(字节数组)是空的(用零填充)
    3. 方法 read() / read(byte[]) 正在从流中读取一个或多个字节
    4. 要知道有多少字节被“从流映射/读取到缓冲区”,您必须获得 read(byte[]) 方法的结果,这很有用,因为您不需要检查缓冲区的内容。
    5. 在某些时候你需要从缓冲区中获取数据/然后你需要知道缓冲区中数据的开始和结束偏移量

    例如:

     // on left define byte array   =  //  on right reserve new byte array of size 10 bytes 
      byte[] buffer =  new byte[10];
      // [00 00 00 00 00 00 00 00 00 00]  <- array in memory 
      int we_have_read = read(buffer);     // assume we have read 3 bytes 
      // [22 ff a2 00 00 00 00 00 00 00]  <- array after read 
    
     have we reached the end of steram or not ?  do we need still to read ? 
    
     we_have_read  ? what is the value of this variable ? 3 or -1 ? 
    
     if 3 ? do we need still read ? 
     or -1 ? what to do ? 
    

    我鼓励您阅读有关 ionio api 的更多信息

    http://tutorials.jenkov.com/java-nio/nio-vs-io.html

    https://blogs.oracle.com/slc/entry/javanio_vs_javaio

    http://www.skill-guru.com/blog/2010/11/14/java-nio-vs-java-io-which-one-to-use/

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 2016-07-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多