This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

DataOutputStream

  • Located in Java. IO. FilterOutputStream DataOutputStream
  • Writes all types of data, as well as the binary form of a String, machine-independent, starting at the top
  • Any DataInputStream on any machine can read the written data
  • All methods begin with write. Such as writeByte (), writeFloat ()

PrintStream

  • Located in Java. IO. FilterOutputStream PrintStream
  • The original purpose was to print all the basic data types as well as strings in a visual format
  • PrintStream is different from DataOutputStream: the purpose of PrintStream is to put data elements into the “stream”, so that the DataInputStream can be portable to reconstruct the data elements

Binary write

Write the string in binary

  • A string is essentially a sequence of chars, which is char[]
  • By iterating over each char, we are done writing a string

Char in binary

  • English letters have AN ASCII code that converts each character into its own number
  • Unicode code:Mapping between characters and encodings, using2The bytes represent all characters
    • The Unicode character encoding standard is a fixed length character encoding scheme that includes characters from almost all the languages currently in use in the world
  • Unicode uses two encodings depending on the data type to be encoded:
    • 16:
      • The default encoding format is 16 bits. That is, each character is 16 bits (2 bytes wide)
      • It is usually shown as U+ HHHH, where HHHH is the hexadecimal code point of the character
    • eight
  • The Unicode standard provides an extension mechanism that allows the encoding of more than a million characters:
    • An extended or supplementary character is encoded using a pair of high-order and low-order substitution characters
    • The first high-order substitution character: has values between U+D800 and U+DBFF
    • Second low order substitution character: has values between U+DC00 and U+DFFF

WriteChar source

  • So each char is a number between 0 and 65535
  • DataOutputStream. WriteChar source code:
public final void writeChar(String s) throws IOException {
	int len = s.length();

	for (int i = 0; i < len; i++) {
		int v = s.charAt(i);
		out.write((v >>> 8) & 0xFF);
		out.write((v >>> 0) & 0xFF);
	}
	incCount(len * 2);
}
Copy the code

WriteShort source

  • In contrast to writeChar,writeShort does not require a string traversal
  • DataOutputStream. WriteShort source code:
public final void writeShort(int v) {
	out.write((v >>> 8) & 0xFF);
	out.write((v >>> 0) & 0xFF);
	incCount(2);
}
Copy the code

0xFF

  • Binary write problem:
    • Why do I use unsigned right?
    • &0xff doesn’t change the size of the number, so why use &0xff?
  • 0xFF is 255 in hexadecimal, which is 1111 in binary
  • & is an AND AND operation. If both are 1, it is 1; otherwise, it is 0

The displacement computation

  • Left shift: <<. Right complement 0
  • Signed right shift:>>. Left complement sign bit
    • Left complement 1 if the sign bit is 1
    • Left complement is 0 if the sign bit is 0
  • Unsigned right shift: >>>. Left complement 0

Principle of binary write

  • The binary writing mode is as follows: Write the high 8 bits first, and then write the low 8 bits
  • Example:The unicode code for the written short character is 3
    • First get the high 8 bits of the source code of 3:
      • 00000000000000000011 >>> 8 Gets 0000000000000000000000000000
      • And then &0xff to get the final result 0000,0000
    • Then get the lower 8 bits of the source code of 3:
      • 00000000000000000011 >>> 0 gets 000000000000000000000011
      • Then &0xff to get the final result 0000,0011

conclusion

  • &0xff is the computer equivalent of scissors. When the number of bits on both sides of the &operator is the same, it does not change the size of the number. It just cuts off a byte of 8 bits
  • Similarly: &0x0f gives a length of 4 bits