Java Byte Array to String by Charset getMaxBytes(String string, int maxBytes, Charset charSet)

Here you can find the source of getMaxBytes(String string, int maxBytes, Charset charSet)

Description

Returns the first maxBytes of string encoded using the encoder of charSet

License

Open Source License

Parameter

Parameter Description
string whose prefix bytes to return
maxBytes the maximum number of bytes to return
charSet the char set used for encoding the characters into bytes

Exception

Parameter Description
CharacterCodingException if the char set's encoder could nothandle the characters in the string

Return

the array of bytes of length <= maxBytes

Declaration

static byte[] getMaxBytes(String string, int maxBytes, Charset charSet) throws CharacterCodingException 

Method Source Code

//package com.java2s;
/*//from   w w w  .  j a  v  a2 s  .  c  om
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

public class Main {
    /**
     * Returns the first <code>maxBytes</code> of <code>string</code> encoded 
     * using the encoder of <code>charSet</code>
     * @param string whose prefix bytes to return
     * @param maxBytes the maximum number of bytes to return
     * @param charSet the char set used for encoding the characters into bytes
     * @return the array of bytes of length <= maxBytes
     * @throws CharacterCodingException if the char set's encoder could not
     * handle the characters in the string
     */
    static byte[] getMaxBytes(String string, int maxBytes, Charset charSet) throws CharacterCodingException {
        byte[] bytes = new byte[maxBytes];
        ByteBuffer out = ByteBuffer.wrap(bytes);
        CharBuffer in = CharBuffer.wrap(string.toCharArray());
        CharsetEncoder encoder = charSet.newEncoder();
        CoderResult cr = encoder.encode(in, out, true);
        encoder.flush(out);
        if (cr.isError()) {
            cr.throwException();
        }
        byte[] result = new byte[out.position()];
        System.arraycopy(bytes, 0, result, 0, result.length);
        return result;
    }
}

Related

  1. getBytesByCharset(String str, Charset charset)
  2. getBytesUnchecked(String string, String charsetName)
  3. getBytesUnchecked(String string, String charsetName)
  4. getChars(byte[] data, String charset)
  5. getContentAsString(byte[] content, Charset charset)
  6. getNumberOfBytesPerCharacter(final String charsetName)
  7. getPrefixWithMaxBytes(String string, int maxBytes, Charset charSet)
  8. getRowId(final byte[] rowId, final Charset charset, final boolean base64encodeValues)
  9. getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset, @Nonnull Charset charset)