Java ByteBuffer Decode decodeUTF8(ByteBuffer utf8Data)

Here you can find the source of decodeUTF8(ByteBuffer utf8Data)

Description

Convenience method for decoding a UTF-8 ByteBuffer into a Java String .

License

Open Source License

Parameter

Parameter Description
utf8Data the UTF-8 encoded string.

Return

a containing the contents of 'utf8Data'.

Declaration

public static String decodeUTF8(ByteBuffer utf8Data) 

Method Source Code


//package com.java2s;
/*-/*from  ww w  .j  a  v a  2 s . c  o  m*/
 * jFUSE - FUSE bindings for Java
 * Copyright (C) 2008-2009  Erik Larsson <erik82@kth.se>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

public class Main {
    private static final Charset utf8Charset = Charset.forName("UTF-8");

    /**
     * Convenience method for decoding a UTF-8 byte array into a Java
     * {@link String}.
     *
     * @param utf8Data the UTF-8 encoded string.
     * @return a {@link String} containing the contents of 'utf8Data'.
     */
    public static String decodeUTF8(byte[] utf8Data) {
        try {
            // TODO: Decode properly and deal with errors.
            return new String(utf8Data, 0, utf8Data.length, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 charset not found! This should not happen...", e);
        }
    }

    /**
     * Convenience method for decoding a UTF-8 ByteBuffer into a Java
     * {@link String}.
     *
     * @param utf8Data the UTF-8 encoded string.
     * @return a {@link String} containing the contents of 'utf8Data'.
     */
    public static String decodeUTF8(ByteBuffer utf8Data) {
        try {
            return utf8Charset.newDecoder().decode(utf8Data).toString();
        } catch (CharacterCodingException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Related

  1. decodeThrowing(Charset charset, ByteBuffer in)
  2. decodeTime(byte firstByte, ByteBuffer buffer)
  3. decodeTimeWithoutSign(byte firstByte, ByteBuffer buffer)
  4. decodeToString(ByteBuffer shaderInfoLogBuffer, IntBuffer size)
  5. decodeUtf8(ByteBuffer bytes)
  6. decodeUTF8(final ByteBuffer buf)
  7. fillBuf(InputStream in, ByteBuffer bytes, CharBuffer chars, CharsetDecoder decoder)
  8. readInputStreamReader(InputStream in, ByteBuffer bytes, CharBuffer chars, CharsetDecoder decoder, Object lock)