Java ByteBuffer Decode bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars)

Here you can find the source of bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars)

Description

Convert byte buffer's content into characters.

License

Open Source License

Declaration

public static CharBuffer bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.util.Arrays;

public class Main {
    /**/*from  w w w.j a  va  2s.  c o m*/
     * Convert byte buffer's content into characters. The input buffer's bytes are not
     * consumed (mark is set and reset).
     */
    public static CharBuffer bytesToChars(CharsetDecoder decoder, ByteBuffer bytes, CharBuffer chars) {
        assert decoder.malformedInputAction() == CodingErrorAction.REPORT;

        chars = clearAndEnsureCapacity(chars, (int) (bytes.remaining() * decoder.maxCharsPerByte()));

        bytes.mark();
        decoder.reset();
        CoderResult cr = decoder.decode(bytes, chars, true);
        if (cr.isError()) {
            bytes.reset();
            try {
                cr.throwException();
            } catch (CharacterCodingException e) {
                throw new RuntimeException("Input cannot be mapped to bytes using encoding "
                        + decoder.charset().name() + ": " + Arrays.toString(toArray(bytes)), e);
            }
        }

        assert cr.isUnderflow(); // This should be guaranteed by ensuring max. capacity.
        cr = decoder.flush(chars);
        assert cr.isUnderflow();

        chars.flip();
        bytes.reset();

        return chars;
    }

    /**
     * Ensure the buffer's capacity is large enough to hold a given number
     * of elements. If the input buffer is not large enough, a new buffer is allocated
     * and returned.
     * 
     * @param elements The required number of elements to be appended to the buffer.
     * 
     * @param buffer
     *          The buffer to check or <code>null</code> if a new buffer should be
     *          allocated.
     *
     * @return Returns the same buffer or a new buffer with the given capacity. 
     */
    public static ByteBuffer clearAndEnsureCapacity(ByteBuffer buffer, int elements) {
        if (buffer == null || buffer.capacity() < elements) {
            buffer = ByteBuffer.allocate(elements);
        } else {
            buffer.clear();
        }
        return buffer;
    }

    /**
     * Ensure the buffer's capacity is large enough to hold a given number
     * of elements. If the input buffer is not large enough, a new buffer is allocated
     * and returned.
     * 
     * @param elements The required number of elements to be appended to the buffer.
     * 
     * @param buffer
     *          The buffer to check or <code>null</code> if a new buffer should be
     *          allocated.
     *
     * @return Returns the same buffer or a new buffer with the given capacity. 
     */
    public static CharBuffer clearAndEnsureCapacity(CharBuffer buffer, int elements) {
        if (buffer == null || buffer.capacity() < elements) {
            buffer = CharBuffer.allocate(elements);
        } else {
            buffer.clear();
        }
        return buffer;
    }

    /**
     * @param buffer The buffer to convert to a string.
     * @param charset The charset to use when converting bytes to characters.
     * @return A string representation of buffer's content.
     */
    public static String toString(ByteBuffer buffer, Charset charset) {
        buffer = buffer.slice();
        byte[] buf = new byte[buffer.remaining()];
        buffer.get(buf);
        return new String(buf, charset);
    }

    public static String toString(CharBuffer buffer) {
        buffer = buffer.slice();
        char[] buf = new char[buffer.remaining()];
        buffer.get(buf);
        return new String(buf);
    }

    /**
     * @param buffer The buffer to read from.
     * @return Returns the remaining bytes from the buffer copied to an array.
     */
    public static byte[] toArray(ByteBuffer buffer) {
        byte[] dst = new byte[buffer.remaining()];
        buffer.mark();
        buffer.get(dst);
        buffer.reset();
        return dst;
    }
}

Related

  1. decode(byte firstByte, ByteBuffer buffer, String charset)
  2. decode(ByteBuffer bb)
  3. decode(ByteBuffer bb, CharsetDecoder decoder)
  4. decode(Charset charset, ByteBuffer buffer)