Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
The contracts of the read methods for {{InputStream}} and {{Reader}} and their subclasses are complicated.  According to the Java API \[[API 2006|AA. Bibliography#API 06]\] for the class {{InputStream}}, the {{read(byte\[\] b, int off, int len)}} method provides the following behavior:

The default implementation of this method blocks until the requested amount of input data len has been read, end of file is detected, or an exception is thrown. Subclasses are encouraged to provide a more efficient implementation of this method.

Wiki Markup
However, the {{read(byte\[\])}} method states that it

reads Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. The number of bytes read is, at most, equal to the length of b.

The read() methods return as soon as they find available input data. As a result, these methods can stop reading data before the array is filled because there might not be enough the available data available may be insufficient to fill the array.

Ignoring the result returned by the read() methods is a violation of rule EXP00-J. Do not ignore values returned by methods. Security issues can arise even when return values are considered because the default behavior of the read() methods lacks any guarantee that the entire buffer array is filled. Consequently, when using read() to fill an array, the program must check the return value of read(), and must handle the case where the array is only partially filled. In such cases, the program may try to fill the rest of the array, or work only with the subset of the array that was filled, or throw an exception.

...

This noncompliant code example attempts to read 1024 bytes encoded in UTF-8 from an InputStream and to return them as a String. It explicitly specifies the encoding to build the string, in compliance with rule IDS13-J. Use compatible encodings on both sides of file or network IO.

...

This compliant solution reads all the desired bytes into its buffer, accounting for the total number of bytes read and adjusting the remaining bytes' offset, consequently ensuring that the required data is read in full. It also avoids splitting multibyte encoded characters across buffers by deferring construction of the result string until the data has been fully read. (See rule IDS10-J. Do not assume every character in a string is the same size for more information.)

Code Block
bgColor#ccccff
public static String readBytes(InputStream in) throws IOException {
  int offset = 0;
  int bytesRead = 0;
  byte[] data = new byte[1024];
  while ((bytesRead = in.read(data, offset, data.length - offset))
    != -1) {
    offset += bytesRead;
    if (offset >= data.length) {
      break;
    }
  }
  String str = new String(data, "UTF-8");
  return str;
}

...

The no-argument and one-argument readFully() methods of the DataInputStream class guarantee that they read all of the requested data or throw an exception. These methods throw EOFException if they detect the end of input before the required number of bytes have been read; they throw IOException if some other inputI/output O error occurs.

Code Block
bgColor#ccccff
public static String readBytes(FileInputStream fis)
                               throws IOException {
  byte[] data = new byte[1024];
  DataInputStream dis = new DataInputStream(fis);
  dis.readFully(data);
  String str = new String(data, "UTF-8");
  return str;
}

...

MITRE CWE

CWE-135. Incorrect Calculation calculation of Multi-Byte String Length multi-byte string length

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="26b53724193dfcb8-2ba87352-4ed0461e-a787a17d-d657383970d8caf4e58d1b4a"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

Class InputStream, DataInputStream

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cf7925e8366d4de2-5fb48d8b-49534d84-9deabfd9-9358d7549c10d647a97e0d77"><ac:plain-text-body><![CDATA[

[[Chess 2007

AA. Bibliography#Chess 07]]

8.1, Handling Errors with Return Codes

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8085277a4fa01ba9-0afcb0bf-446b48ad-b40da496-214db0e54c693787cbc14ab8"><ac:plain-text-body><![CDATA[

[[Harold 1999

AA. Bibliography#Harold 99]]

Chapter 7: , Data Streams, Reading Byte Arrays

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="822e62594e30c56f-aef0cc83-45134590-9b45a3bd-096c6c73d1784539cb70f0ac"><ac:plain-text-body><![CDATA[

[[Phillips 2005

AA. Bibliography#Phillips 05]]

 

]]></ac:plain-text-body></ac:structured-macro>

...