A DESCRIPTION OF THE REQUEST :
If you call dataoutputstream.writeUTF(string) and that string happens to be null, you get a NPE. special measures have to be taken to check for null values. it would be nicer if writeUTF() handles null strings more gracefully.
In the example code below, if the String 'comment' happens to be null, the write1() method will throw a NPE. I would have to do something like write2() to account for null values.
JUSTIFICATION :
This will simplify user code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When implementing this RFE, care should be taken to differntiate between NULL and EMPTY "" strings... ie:
a readUTF() corresponding to a writeUF(null) should return null.
and a readUTF() corresponding to a writeUTF("") should return "".
---------- BEGIN SOURCE ----------
import java.io.*;
class Person {
int id;
String fname, mname, lname;
String comment;
void write1(DataOutputStream out) {
out.writeInt(id);
//...
out.writeUTF(comment);
}//write1
Person read1(DataInputStream in) {
id = in.readInt();
//...
comment = in.readUTF();
}//read1
public static final String NULL_HOLDER = "NuLl_HoLdEr";
void write2(DataOutputStream out) {
out.writeInt(id);
//...
if (comment == null)
out.writeUTF(NULL_HOLDER);
else
out.writeUTF(comment);
}//write2
Person read2(DataInputStream in) {
id = in.readInt();
//...
comment = in.readUTF();
if (NULL_HOLDER.equals(comment))
comment = null;
}//read2
}//class
---------- END SOURCE ----------
If you call dataoutputstream.writeUTF(string) and that string happens to be null, you get a NPE. special measures have to be taken to check for null values. it would be nicer if writeUTF() handles null strings more gracefully.
In the example code below, if the String 'comment' happens to be null, the write1() method will throw a NPE. I would have to do something like write2() to account for null values.
JUSTIFICATION :
This will simplify user code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When implementing this RFE, care should be taken to differntiate between NULL and EMPTY "" strings... ie:
a readUTF() corresponding to a writeUF(null) should return null.
and a readUTF() corresponding to a writeUTF("") should return "".
---------- BEGIN SOURCE ----------
import java.io.*;
class Person {
int id;
String fname, mname, lname;
String comment;
void write1(DataOutputStream out) {
out.writeInt(id);
//...
out.writeUTF(comment);
}//write1
Person read1(DataInputStream in) {
id = in.readInt();
//...
comment = in.readUTF();
}//read1
public static final String NULL_HOLDER = "NuLl_HoLdEr";
void write2(DataOutputStream out) {
out.writeInt(id);
//...
if (comment == null)
out.writeUTF(NULL_HOLDER);
else
out.writeUTF(comment);
}//write2
Person read2(DataInputStream in) {
id = in.readInt();
//...
comment = in.readUTF();
if (NULL_HOLDER.equals(comment))
comment = null;
}//read2
}//class
---------- END SOURCE ----------
- relates to
-
JDK-4343457 DataInputStream will not detect null (all zero) bytes
-
- Closed
-