Java Byte Array to String by Charset getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset, @Nonnull Charset charset)

Here you can find the source of getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset, @Nonnull Charset charset)

Description

get String

License

Mozilla Public License

Declaration

@Nullable
    public static String getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset,
            @Nonnull Charset charset) 

Method Source Code

//package com.java2s;
/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****/*from  w w  w  . j av a 2  s . com*/
 *
 * Version: MPL 2.0
 *
 * echocat Jomon, Copyright (c) 2012-2013 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.charset.Charset;

public class Main {
    public static final int INTEGER_CHUNK_SIZE = 4;

    @Nullable
    public static String getString(@Nonnull byte[] b, @Nonnegative int bufferSize, @Nonnegative int offset,
            @Nonnull Charset charset) {
        final int chunkSize = getStringChunkSizeFor(bufferSize);
        checkBufferEnd(b, offset, chunkSize);
        final int byteCount = getInteger(b, offset);
        final String result;
        if (byteCount >= 0) {
            result = new String(b, offset + INTEGER_CHUNK_SIZE, byteCount, charset);
        } else {
            result = null;
        }
        return result;
    }

    @Nonnegative
    public static int getStringChunkSizeFor(@Nonnegative int bufferSize) {
        return INTEGER_CHUNK_SIZE + bufferSize;
    }

    public static void checkBufferEnd(@Nonnull byte[] b, @Nonnegative int offset, @Nonnegative int length) {
        checkOffset(offset);
        final int end = offset + length;
        if (b.length < end) {
            throw new IndexOutOfBoundsException(
                    "Byte buffer (" + b.length + ") is smaller then expected read end at " + end + ".");
        }
    }

    public static int getInteger(@Nonnull byte[] b, @Nonnegative int offset) {
        checkBufferEnd(b, offset, INTEGER_CHUNK_SIZE);
        return ((b[offset + 3] & 0xFF)) + ((b[offset + 2] & 0xFF) << 8) + ((b[offset + 1] & 0xFF) << 16)
                + ((b[offset]) << 24);
    }

    public static void checkOffset(int offset) {
        if (offset < 0) {
            throw new IndexOutOfBoundsException("Negative offset: " + offset);
        }
    }
}

Related

  1. getContentAsString(byte[] content, Charset charset)
  2. getMaxBytes(String string, int maxBytes, Charset charSet)
  3. getNumberOfBytesPerCharacter(final String charsetName)
  4. getPrefixWithMaxBytes(String string, int maxBytes, Charset charSet)
  5. getRowId(final byte[] rowId, final Charset charset, final boolean base64encodeValues)
  6. getString(byte[] bytes, String charset)
  7. getString(byte[] bytesIn, Charset cs)
  8. identify(byte[] bytes, CharsetDecoder decoder)
  9. inflate(byte[] bytes, Charset encoding)