Java ByteBuffer to String getString(ByteBuffer buffer, String charFormat)

Here you can find the source of getString(ByteBuffer buffer, String charFormat)

Description

get String

License

Open Source License

Declaration

private static String getString(ByteBuffer buffer, String charFormat) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 <Project SWG>
 * /*from  w  w w .  j  av  a2  s . com*/
 * This File is part of NGECore2.
 * 
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Using NGEngine to work with NGECore2 is making a combined work based on NGEngine. 
 * Therefore all terms and conditions of the GNU Lesser General Public License cover the combination.
 ******************************************************************************/

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    private static String getString(ByteBuffer buffer, String charFormat) {
        String result;
        int length;

        if (charFormat == "UTF-16LE") {
            length = buffer.order(ByteOrder.LITTLE_ENDIAN).getInt();
        } else {
            length = buffer.order(ByteOrder.LITTLE_ENDIAN).getShort();
        }

        int bufferPosition = buffer.position();

        try {
            result = new String(buffer.array(), bufferPosition, length, charFormat);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }

        buffer.position(bufferPosition + length);

        return result;
    }

    private static byte[] getString(String string, String charFormat) {
        ByteBuffer result;
        int length = 2 + string.length();

        if (charFormat == "UTF-16LE") {
            result = ByteBuffer.allocate(length * 2).order(ByteOrder.LITTLE_ENDIAN);
            result.putInt(string.length());
        } else {
            result = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
            result.putShort((short) string.length());
        }

        try {
            result.put(string.getBytes(charFormat));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return new byte[] {};
        }

        return result.array();
    }
}

Related

  1. getString(ByteBuffer buffer)
  2. getString(ByteBuffer buffer, int length)
  3. getString(ByteBuffer buffer, int offset, int length, String encoding)
  4. getString(ByteBuffer buffer, int size)
  5. getString(ByteBuffer buffer, int size)
  6. getString(ByteBuffer byteBuffer, int size)
  7. getString(ByteBuffer in, int maxLength)
  8. getString(final ByteBuffer buffer)
  9. getString(final ByteBuffer buffer, final int offset, final int length, final Charset encoding)