<p>What is the best way to verify/test that a text string is serialized to a byte array with a certain encoding?</p>

<p>In my case, I want to verify that an XML structure is serialized to a byte array with the UTF-8 encoding which is of variable character length. As an example, my current ugly procedure is to inject a character known to require two bytes into the structure before serializing, then replacing the two-byte character with an ASCII character and comparing the serialized array lengths. This should yield two serialized arrays where the array containing the two-byte characters should have length +1.</p>

<p>Plus if the solution is elegant for Java. 
I can't think of any elegant way to seek for a byte sequence in a byte array. (Could be used to seek for a known byte sequence representing the desired character representation in UTF-8.)</p>

[{'answer_id': 7688, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 3, 'selected': True, 'text': "<p>Perhaps you could deserialise the byte array using a known encoding and ensure that (a) it doesn't throw any exceptions, and (b) deserialises to the original string. It seems that from your description of the scenario, you may not have the original string readily available. Might there be a way to create it?</p>\n"}, {'answer_id': 7698, 'author': 'Hugo', 'author_id': 972, 'author_profile': 'https://Stackoverflow.com/users/972', 'pm_score': 0, 'selected': False, 'text': "<p>That's good.\nYou're right, I don't have the original string, since I'm testing a module that creates an XML document as a byte array. I didn't think about deserializing to a String with an expected encoding. That will do the trick.</p>\n"}]
['https://Stackoverflow.com/questions/7681', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/972/']