Here you can find the source of decodeUTF8(ByteBuffer utf8Data)
Parameter | Description |
---|---|
utf8Data | the UTF-8 encoded string. |
public static String decodeUTF8(ByteBuffer utf8Data)
//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; } } }