Java Byte Array to String bytesToString(byte[] arr, int pos)

Here you can find the source of bytesToString(byte[] arr, int pos)

Description

Reads a string from a byte array.

License

Open Source License

Parameter

Parameter Description
arr the byte array to read the string from.
pos the position of the string's first byte.

Exception

Parameter Description
IllegalArgumentException if the position is out of the range of the byte array.

Return

the read string.

Declaration

public static String bytesToString(byte[] arr, int pos)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*  w  ww  .  jav a 2s.co  m*/
 * Process Sketcher is a simple flowchart making software.
 * Copyright (C) 2015  Jonathon Prehn, Kevin Prehn
 * 
 * This file is a part of Process Sketcher.
 * 
 * Process Sketcher 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.
 * 
 * Process Sketcher 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 Process Sketcher.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Reads a string from a byte array.
     * @param arr the byte array to read the string from.
     * @param pos the position of the string's first byte.
     * @return the read string.
     * @throws IllegalArgumentException if the position is out of the 
     * range of the byte array.
     */
    public static String bytesToString(byte[] arr, int pos)
            throws IllegalArgumentException {
        checkBounds(arr, pos);
        int charCount = bytesToInt(arr, pos);
        String str = "";
        for (int i = 0; i < charCount; i++) {
            str += bytesToChar(arr, pos + 4 + (i * 2));
        }
        return str;
    }

    /**
     * Checks the position to the bounds of the byte array.
     * @param arr the byte array
     * @param pos the position in the byte array
     * @throws IllegalArgumentException thrown if the position is out of the 
     * byte array's bounds.
     */
    private static void checkBounds(byte[] arr, int pos)
            throws IllegalArgumentException {
        if (pos < 0 || pos >= arr.length) {
            throw new IllegalArgumentException(
                    "Position is out of bounds of "
                            + "the byte array: position is " + pos
                            + " while the byte"
                            + " array only accepts 0 to "
                            + (arr.length - 1));
        }
    }

    /**
     * Interprets an int from 4 bytes from the given byte array.
     *
     * @param arr the byte array.
     * @param pos the position of the first byte of the int.
     * @return the interpreted int.
     * @throws IllegalArgumentException if the position is out of the 
     * range of the byte array.
     */
    public static int bytesToInt(byte[] arr, int pos)
            throws IllegalArgumentException {
        checkBounds(arr, pos);
        return (((int) arr[pos] << 24) & 0xFF000000)
                | (((int) arr[pos + 1] << 16) & 0x00FF0000)
                | (((int) arr[pos + 2] << 8) & 0x0000FF00)
                | (((int) arr[pos + 3]) & 0x000000FF);
    }

    /**
     * Reads a character from a byte array.
     *
     * @param arr the array to read the character from.
     * @param pos the position of the character's first byte.
     * @return the read character.
     * @throws IllegalArgumentException if the position is out of the 
     * range of the byte array.
     */
    public static char bytesToChar(byte[] arr, int pos)
            throws IllegalArgumentException {
        checkBounds(arr, pos);
        return (char) ((((short) arr[pos] << 8) & 0xFF00) | ((short) arr[pos + 1] & 0x00FF));
    }
}

Related

  1. bytesToStr(byte[] bytes)
  2. bytesToStr(byte[] input)
  3. bytesToStrHex(byte[] bytes)
  4. bytesToString(byte... bytes)
  5. bytesToString(byte[] arr)
  6. bytesToString(byte[] b)
  7. bytesToString(byte[] buffer, int index, int length)
  8. bytesToString(byte[] bytes)
  9. bytesToString(byte[] bytes)