JAVA CERTIFICATION QUESTION: Java’s text blocks and platform-specific line separators
Windows and UNIX-like systems use different line separators. Will this affect hashcodes of strings?
You are working on a distributed application that exchanges sensitive text information between different clients and servers running on Windows and Linux platforms.
In the past, key messages were encoded in a String, like this:
You have proposed to update the program to use the new multiline text block feature, thereby encoding the message in a String as follows:
The data will be transferred over the network like this:
When a message is sent, the receiver calculates the hashcode of the received string and compares it to a locally stored hashcode.
A colleague mentions that the Windows platform's native encoding of text files, and therefore the Java source code files, represents line endings using CR-LF (0x0D, 0x0A) but the Linux platform uses LF (0x0A).
The colleague expresses concern that the presence or absence of the 0x0D byte will cause the hashcodes to differ, and therefore cause the security checks to fail.
What will you do? Choose one.
A. Use the String.split(System.getProperty("line.separator")) method to break the text blocks
into multiple String fields representing the separate lines, and then reassemble these lines into a single String again using a line separator that will be common across platforms.
B. Use the String.indent(0) method to normalize the line-ending characters before transmitting the text.
C. Use the String.stripIndent() method to normalize the line-ending characters before transmitting the text.
D. Politely ignore your colleague because your proposal will work correctly.
·ꓷ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ ·uoᴉsnʅɔuoꓛ