Java Utililty Methods DataOutputStream Write String

List of utility methods to do DataOutputStream Write String

Description

The list of methods to do DataOutputStream Write String are organized into topic(s).

Method

voidwriteString(DataOutputStream buf, String value)
write String
if (value.contains("\0")) {
    throw new IllegalArgumentException("Null characters are not allowed in null-terminated strings.");
buf.writeBytes(value);
buf.writeByte((byte) 0);
voidwriteString(DataOutputStream os, String s)
write String to DataOutputStream motivation: DataInputStream.readUTF can't print lines larger than USHORTMAX
if (s == null) {
    os.writeInt(-1);
} else {
    byte array[] = s.getBytes();
    os.writeInt(array.length);
    os.write(array);
voidwriteString(DataOutputStream os, String str)
Writes string into DataOutputStream.
if (str != null) {
    os.writeInt(str.length());
    os.writeChars(str);
} else {
    os.writeInt(0);
voidwriteString(DataOutputStream out, String s)
write String
boolean isNull = s == null;
out.writeBoolean(isNull);
if (!isNull) {
    out.writeUTF(s);
voidwriteString(DataOutputStream out, String str)
Writes a string to the buffer.
int len = str.length();
if (len >= 65536) {
    throw new IllegalArgumentException("String too long.");
out.writeShort(len);
for (int i = 0; i < len; ++i) {
    out.writeChar(str.charAt(i));
voidwriteString(DataOutputStream out, String str)
write String
byte[] b = str.getBytes();
out.writeInt(b.length);
out.write(b);
voidwriteString(DataOutputStream out, String str)
Writes a string to a DataInputStream.
out.writeShort(str.length());
for (char letter : str.toCharArray()) {
    out.writeChar(letter);
voidwriteString(DataOutputStream out, String text)
Writes a string to the given output stream and emits an additional short to transfer if text is null.
if (null == text) {
    out.writeShort(0);
} else {
    out.writeShort(1);
    out.writeUTF(text);
voidwriteString(DataOutputStream out, String theString)
Write a string.
if (theString == null) {
    out.writeInt(-1);
} else {
    int len = theString.length();
    out.writeInt(len);
    writeCharsOfString(out, theString);
voidwriteString(DataOutputStream out, String val)
write String
out.writeUTF(val);