title: What is CharsetDecoder.decode(ByteBuffer, CharBuffer, endOfInput)
body: <p>I have a problem with <code>CharsetDecoder</code> class.</p>
  
  <p>First example of code (which works):</p>
  
  <pre><code>    final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
      final ByteBuffer b = ByteBuffer.allocate(3);
      final byte[] tab = new byte[]{(byte)-30, (byte)-126, (byte)-84}; //char €
      for (int i=0; i&lt;tab.length; i++){
          b.put(tab, i, 1);
      }
      try {
          b.flip();
          System.out.println("a" + dec.decode(b).toString() + "a");
      } catch (CharacterCodingException e1) {
          e1.printStackTrace();
      }
  </code></pre>
  
  <p>The result is <code>a€a</code></p>
  
  <p>But when i execute this code:</p>
  
  <pre><code>    final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
      final CharBuffer chars = CharBuffer.allocate(3);
      final byte[] tab = new byte[]{(byte)-30, (byte)-126, (byte)-84}; //char €
      for (int i=0; i&lt;tab.length; i++){
          ByteBuffer buffer = ByteBuffer.wrap(tab, i, 1);
          dec.decode(buffer, chars, i == 2);
      }
      dec.flush(chars);
      System.out.println("a" + chars.toString() + "a");
  </code></pre>
  
  <p>The result is <code>a</code></p>
  
  <p>Why is not the same result?</p>
  
  <p>How to use the method <code>decode(ByteBuffer, CharBuffer, endOfInput)</code> of class <code>CharsetDecoder</code> in order to retrieve the result <code>a€a</code> ?</p>
  
  <p><strong>-- EDIT --</strong></p>
  
  <p>So with code of Jesper I do that. It's no perfect but works with a <code>step</code> = 1, 2 and 3</p>
  
  <pre><code>final CharsetDecoder dec = Charset.forName("UTF-8").newDecoder();
      final CharBuffer chars = CharBuffer.allocate(6);
      final byte[] tab = new byte[]{(byte)97, (byte)-30, (byte)-126, (byte)-84, (byte)97, (byte)97}; //char €
  
      final ByteBuffer buffer = ByteBuffer.allocate(10);
  
      final int step = 3;
      for (int i = 0; i &lt; tab.length; i++) {
          // Add the next byte to the buffer
          buffer.put(tab, i, step);
          i+=step-1;
  
          // Remember the current position
          final int pos = buffer.position();
          int l=chars.position();
  
          // Try to decode
          buffer.flip();
          final CoderResult result = dec.decode(buffer, chars, i &gt;= tab.length -1);
          System.out.println(result);
  
          if (result.isUnderflow() &amp;&amp; chars.position() == l) {
              // Underflow, prepare the buffer for more writing
              buffer.position(pos);
          }else{
              if (buffer.position() == buffer.limit()){
                  //ByteBuffer decoded
                  buffer.clear();
                  buffer.position(0);
              }else{
                  //a part of ByteBuffer is decoded. We keep only bytes which are not decoded
                  final byte[] b = buffer.array();
                  final int f = buffer.position();
                  final int g = buffer.limit() - buffer.position();
                  buffer.clear();
                  buffer.position(0);
                  buffer.put(b, f, g);
              }
          }
          buffer.limit(buffer.capacity());
      }
  
      dec.flush(chars);
      chars.flip();
  
      System.out.println(chars.toString());
  </code></pre>