简体   繁体   English

IntelliJ“ inputstream.read的结果被忽略”-如何解决?

[英]IntelliJ “result of inputstream.read is ignored” - how to fix?

I'm working on fixing some potential bugs in my application. 我正在修复应用程序中的一些潜在错误。 I'm using Sonar to evaluate my code. 我正在使用Sonar评估我的代码。 My issue is this: 我的问题是这样的:

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;
}

Does this mean that the byte array was empty? 这是否意味着字节数组为空? Why is it being ignored? 为什么它被忽略? I've never worked with bytes so I was wondering what exactly this issue means and how I can address it. 我从未使用过字节,因此我想知道这个问题的确切含义以及如何解决。 I appreciate your assistance! 感谢您的协助!

Does this mean that the byte array was empty? 这是否意味着字节数组为空?

NO - this is no't an error 否-这不是错误

look at read(byte[]) method from definition of: 从以下定义看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 {

       ....
    }

}

so what the IDE indicating? 那么IDE指示什么呢? is that you omitting the result of read method - which is the number of bytes actually read or -1 if the end of the stream has been reached. 您省略了read方法的结果 -这是实际读取的字节数,如果已到达流的末尾,则为-1。

How to fix? 怎么修?

if u care how many bytes were read to byte buffer: 如果您关心将多少字节读取到字节缓冲区:

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

why you should care ??? 为什么你应该关心???

  1. because when you read from stream you are passing as parameter often a buffer specially when you don't know the size of data being carried by stream or you want to read by portions (in this case you passing byte array of decryptedBuferSize size - > new byte[decryptBuferSize] ) . 因为当您从流中读取数据时,通常是作为缓冲区传递参数,特别是当您不知道流所承载的数据大小或要按部分读取数据时(在这种情况下,您传递的是byteed数组decryptedBuferSize size-> new byte [decryptBuferSize] )。
  2. at the beginning the byte buffer (byte array) is empty (filled with zeros) 在开始时,字节缓冲区(字节数组)为空(用零填充)
  3. method read() / read(byte[]) are reading one or many bytes from stream 方法read()/ read(byte [])从流中读取一个或多个字节
  4. to be aware how many bytes were "mapped/read from stream to buffer" you have to get the result of read(byte[]) method, this is useful because you don't need to check the content of buffer. 要知道“映射/从流到缓冲区读取/读取了多少个字节”,您必须获取read(byte [])方法的结果,这很有用,因为您无需检查缓冲区的内容。
  5. still at some point you need get the data from buffer / then you need to know the beginning and end offset of the data in buffer 仍然需要从缓冲区中获取数据/然后您需要知道缓冲区中数据的开始和结束偏移量

for example: 例如:

 // 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 ? 

i encourage you to read more about io and nio api 我鼓励您阅读有关ionio api的更多信息

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

https://blogs.oracle.com/slc/entry/javanio_vs_javaio 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/ http://www.skill-guru.com/blog/2010/11/14/java-nio-vs-java-io-which-one-to-use/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM