Java - Trimming trailing whitespace from a byte array
<p>I have byte arrays similar to this:</p>

<pre><code>[77, 83, 65, 80, 79, 67, 32, 32, 32, 32, 32, 32, 32]
</code></pre>

<p>roughly equal to</p>

<pre><code>[M , S, A, P, O, C,  ,  ,  ,  ,  ,  ,  ] when printed as chars.
</code></pre>

<p>Now I want to trim the trailing whitespace so it looks like:</p>

<pre><code>[77, 83, 65, 80, 79, 67]
</code></pre>

<p>Easiest way to do this?</p>

<p><strong>Edit</strong>: I don't want to deal with Strings because there is the possibility for non-printable bytes, and I can not afford to lose that data. It needs to be byte arrays :( Whenever I do convert to Strings, bytes like 01 (SOH) 02 (STX) etc are lost. </p>

<p><strong>Edit 2</strong>: <strong>Just to clarify. DO I lose data if I convert byte arrays to Strings? Now a little confused. What about if the bytes are of a different character set?</strong></p>
