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}} classes and their subclasses are complicated with regard to filling byte or character arrays.  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:

...

Wiki Markup
However, the {{read(byte\[\])}} method:

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.

...

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 character encoding used to build the string, in compliance with rule IDS13-J. Use compatible encodings on both sides of file or network IO.

...

The programmer's misunderstanding of the general contract of the read() method can result in failure to read the intended data in full. It is possible that the data is less than 1024 bytes long , with and that additional data is available from the InputStreaminput stream.

Compliant Solution (Multiple Calls to read())

...

The no-argument and one-argument readFully() methods of the DataInputStream class guarantee that they read either all of the requested data is read or throw an exception is thrown. 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 I/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;
}

Risk Assessment

Failure to comply with this rule Incorrect use of the read() method can result in the wrong number of bytes being read or character sequences being interpreted incorrectly.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5aaa0dbba9e9b30b-3e630be2-4e6c49fc-8bf1b651-c344434ab6471d1b6d7fa4a0"><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="7497e68534a3dedd-5e0d1dc8-4284494c-b9e1b56b-cea7320b0c585b69c2f11134"><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="9071c983df0bb5a6-c9f87c84-42a94249-b9a6b8f1-a4d82146ec96b5a52dc6ef9e"><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="ae29bfff48cbed51-bed4cc30-491347e0-b8948b09-26f4493a37d4e002ac6a31a8"><ac:plain-text-body><![CDATA[

[[Phillips 2005

AA. Bibliography#Phillips 05]]

 

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

...