ImageOutputStream (Java Platform SE 7 ) (original) (raw)

Writes two bytes of length information to the output stream in network byte order, followed by themodified UTF-8 representation of every character in the string s. If s is null, aNullPointerException is thrown. Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character.

If a character c is in the range\u0001 through \u007f, it is represented by one byte:

(byte)c

If a character c is \u0000 or is in the range \u0080 through\u07ff, then it is represented by two bytes, to be written in the order shown:


 (byte)(0xc0 | (0x1f & (c >> 6)))
 (byte)(0x80 | (0x3f & c))
 

If a character c is in the range\u0800 through uffff, then it is represented by three bytes, to be written in the order shown:


 (byte)(0xe0 | (0x0f & (c >> 12)))
 (byte)(0x80 | (0x3f & (c >> 6)))
 (byte)(0x80 | (0x3f & c))
 

First, the total number of bytes needed to represent all the characters of s is calculated. If this number is larger than 65535, then aUTFDataFormatException is thrown. Otherwise, this length is written to the output stream in exactly the manner of the writeShort method; after this, the one-, two-, or three-byte representation of each character in the strings is written.

The current byte order setting is ignored.

If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write.

Note: This method should not be used in the implementation of image formats that use standard UTF-8, because the modified UTF-8 used here is incompatible with standard UTF-8.