Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Answer by Adamski is a good one and describes the steps in an encoding operation when using the general encode method (that takes a byte buffer as one of the inputs)</p> <p>However, the method in question (in this discussion) is a variant of encode - <strong>encode(CharBuffer in)</strong>. This is a <strong>convenience method that implements the entire encoding operation</strong>. (Please see java docs reference in P.S.)</p> <p>As per the docs, <strong>This method should therefore not be invoked if an encoding operation is already in progress</strong> (which is what is happening in ZenBlender's code -- using static encoder/decoder in a multi threaded environment).</p> <p>Personally, I like to use <em>convenience</em> methods (over the more general encode/decode methods) as they take away the burden by performing all the steps under the covers. </p> <p>ZenBlender and Adamski have already suggested multiple ways options to safely do this in their comments. Listing them all here:</p> <ul> <li>Create a new encoder/decoder object when needed for each operation (not efficient as it could lead to a large number of objects). OR,</li> <li>Use a ThreadLocal to avoid creating new encoder/decoder for each operation. OR,</li> <li>Synchronize the entire encoding/decoding operation (this might not be preferred unless sacrificing some concurrency is ok for your program) </li> </ul> <p>P.S.</p> <p>java docs references:</p> <ol> <li>Encode (convenience) method: <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/CharsetEncoder.html#encode%28java.nio.CharBuffer%29">http://docs.oracle.com/javase/6/docs/api/java/nio/charset/CharsetEncoder.html#encode%28java.nio.CharBuffer%29</a></li> <li>General encode method: <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/CharsetEncoder.html#encode%28java.nio.CharBuffer,%20java.nio.ByteBuffer,%20boolean%29">http://docs.oracle.com/javase/6/docs/api/java/nio/charset/CharsetEncoder.html#encode%28java.nio.CharBuffer,%20java.nio.ByteBuffer,%20boolean%29</a></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload