Byte array is a valid UTF8 encoded String in Java but not in Python
<p>When I run the following in Python 2.7.6, I get an exception:</p>

<pre><code>import base64
some_bytes = b"\x80\x02\x03"
print ("base 64 of the bytes:")
print (base64.b64encode(some_bytes))
try:
    print (some_bytes.decode("utf-8"))
except Exception as e:
    print(e)
</code></pre>

<p>The output:</p>

<pre><code>base 64 of the bytes:
gAID
'utf8' codec can't decode byte 0x80 in position 0: invalid start byte
</code></pre>

<p>So in Python 2.7.6 the bytes represented as <code>gAID</code> are not a valid UTF8.</p>

<p>When I try it in Java 8 (HotSpot 1.8.0_74), using this code:</p>

<pre><code>java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
byte[] bytes = decoder.decode("gAID");
Charset charset = Charset.forName("UTF8");
String s = new String(bytes, charset);
</code></pre>

<p>I don't get any exception.</p>

<p>How so? Why is the same byte array is valid in Java and invalid in Python, using UTF8 decoding?</p>

<p>It's not valid UTF8. <a href="https://en.wikipedia.org/wiki/UTF-8" rel="nofollow">https://en.wikipedia.org/wiki/UTF-8</a></p>

<p>Bytes between 0x80 and 0xBF cannot be the first byte of a multi-byte character. They can only be the second byte or later.</p>

<p>Java replaces bytes which cannot be decoded with a <code>?</code> rather than throw an exception.</p>

java python utf-8 character-encoding