Return bytes in Utf8 representation as an array of characters.
@param src        The array holding the bytes.
@param sindex     The start index from which bytes are converted.
@param len        The maximum number of bytes to convert.
347869
public static char[] utf2chars(byte[] src, int sindex, int len) {
        char[] dst = new char[len];
        int len1 = utf2chars(src, sindex, dst, 0, len);
        char[] result = new char[len1];
        System.arraycopy(dst, 0, result, 0, len1);
        return result;
    }
495677
public static byte[] decode(char[] src, int[] table, char pad) {
        int len = src.length;

        if (len == 0) return new byte[0];

        int padCount = (src[len - 1] == pad ? (src[len - 2] == pad ? 2 : 1) : 0);
        int bytes    = (len * 6 >> 3) - padCount;
        int blocks   = (bytes / 3) * 3;

        byte[] dst = new byte[bytes];
        int si = 0, di = 0;

        while (di < blocks) {
            int n = table[src[si++]] << 18 | table[src[si++]] << 12 | table[src[si++]] << 6 | table[src[si++]];
            dst[di++] = (byte) (n >> 16);
            dst[di++] = (byte) (n >>  8);
            dst[di++] = (byte) n;
        }

        if (di < bytes) {
            int n = 0;
            switch (len - si) {
                case 4: n |= table[src[si+3]];
                case 3: n |= table[src[si+2]] <<  6;
                case 2: n |= table[src[si+1]] << 12;
                case 1: n |= table[src[si]]   << 18;
            }
            for (int r = 16; di < bytes; r -= 8) {
                dst[di++] = (byte) (n >> r);
            }
        }

        return dst;
    }
1050399
static private void decodeCharacter(byte chunk, int characterIndex, byte[] bytes, int index) {
        int byteIndex = index + (characterIndex * 5) / 8;
        int offset = characterIndex % 8;
        switch (offset) {
            case 0:
                bytes[byteIndex] |= chunk << 3;
                break;
            case 1:
                bytes[byteIndex] |= chunk >>> 2;
                bytes[byteIndex + 1] |= chunk << 6;
                break;
            case 2:
                bytes[byteIndex] |= chunk << 1;
                break;
            case 3:
                bytes[byteIndex] |= chunk >>> 4;
                bytes[byteIndex + 1] |= chunk << 4;
                break;
            case 4:
                bytes[byteIndex] |= chunk >>> 1;
                bytes[byteIndex + 1] |= chunk << 7;
                break;
            case 5:
                bytes[byteIndex] |= chunk << 2;
                break;
            case 6:
                bytes[byteIndex] |= chunk >>> 3;
                bytes[byteIndex + 1] |= chunk << 5;
                break;
            case 7:
                bytes[byteIndex] |= chunk;
                break;
        }
    }
484958
public static <T extends Appendable> T toHexString(T dst, byte[] src) {
        return toHexString(dst, src, 0, src.length);
    }
1355858
public static void longToByteArray(long lValue, byte[] buffer, int nStartIndex) {
	buffer[nStartIndex] = (byte) (lValue >>> 56);
	buffer[nStartIndex + 1] = (byte) ((lValue >>> 48) & 0x0ff);
	buffer[nStartIndex + 2] = (byte) ((lValue >>> 40) & 0x0ff);
	buffer[nStartIndex + 3] = (byte) ((lValue >>> 32) & 0x0ff);
	buffer[nStartIndex + 4] = (byte) ((lValue >>> 24) & 0x0ff);
	buffer[nStartIndex + 5] = (byte) ((lValue >>> 16) & 0x0ff);
	buffer[nStartIndex + 6] = (byte) ((lValue >>> 8) & 0x0ff);
	buffer[nStartIndex + 7] = (byte) lValue;
    }
114168
static private void encodeBytes(byte previous, byte current, int byteIndex, StringBuilder output) {
        int chunk;
        int offset = byteIndex % 5;
        switch (offset) {
            case 0:
                chunk = ((current & 0xF8) >>> 3);
                output.append(lookupTable.charAt(chunk));
                break;
            case 1:
                chunk = (((previous & 0x07) << 2) | ((current & 0xC0) >>> 6));
                output.append(lookupTable.charAt(chunk));
                chunk = ((current & 0x3E) >>> 1);
                output.append(lookupTable.charAt(chunk));
                break;
            case 2:
                chunk = (((previous & 0x01) << 4) | ((current & 0xF0) >>> 4));
                output.append(lookupTable.charAt(chunk));
                break;
            case 3:
                chunk = (((previous & 0x0F) << 1) | ((current & 0x80) >>> 7));
                output.append(lookupTable.charAt(chunk));
                chunk = ((current & 0x7C) >>> 2);
                output.append(lookupTable.charAt(chunk));
                break;
            case 4:
                chunk = (((previous & 0x03) << 3) | ((current & 0xE0) >>> 5));
                output.append(lookupTable.charAt(chunk));
                chunk = (current & 0x1F);
                output.append(lookupTable.charAt(chunk));
                break;
        }
    }
381677
protected String getCharsetFromBytes(byte buffer[], int len) 
	throws IOException {
		String charsetName = null;
	    UniversalDetector detector = new UniversalDetector(null);
		detector.handleData(buffer, 0, len);
		detector.dataEnd();
	    charsetName = detector.getDetectedCharset();
	    detector.reset();
	    if(isCharsetSupported(charsetName)) {
	    	return mapCharset(charsetName);
	    }
	    return null;
	}
1145969
public static byte[] toBytes(CharSequence seq) {
    try {
      return seq.toString().getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException ex) {
      throw new RuntimeException(ex); // shouldn't happen since UTF-8 is supported by all JVMs per spec
    }
  }
798380
public void append(byte[] utf8, int start, int len) {
        setCapacity(length + len, true);
        System.arraycopy(utf8, start, bytes, length, len);
        length += len;
    }
891048
static public byte[] expandByteArray(byte[] src, byte[] dst) {
        int srcLength = (null != src) ? src.length : 0;
        int dstLength = (null != dst) ? dst.length : 0;
        return expandByteArray(src, dst, 0, srcLength, 0, dstLength);
    }
635588
public static StringBuilder decode(CharSequence src, boolean[] caseFlags) 
                               throws StringPrepParseException{
        int srcLength = src.length();
        StringBuilder dest = new StringBuilder(src.length());
        int n, i, bias, basicLength, j, in, oldi, w, k, digit, t,
                destCPCount, firstSupplementaryIndex, cpLength;
        char b;

        /*
         * Handle the basic code points:
         * Let basicLength be the number of input code points
         * before the last delimiter, or 0 if there is none,
         * then copy the first basicLength code points to the output.
         *
         * The following loop iterates backward.
         */
        for(j=srcLength; j>0;) {
            if(src.charAt(--j)==DELIMITER) {
                break;
            }
        }
        basicLength=destCPCount=j;

        for(j=0; j<basicLength; ++j) {
            b=src.charAt(j);
            if(!isBasic(b)) {
                throw new StringPrepParseException("Illegal char found", StringPrepParseException.INVALID_CHAR_FOUND);
            }
            dest.append(b);

            if(caseFlags!=null && j<caseFlags.length) {
                caseFlags[j]=isBasicUpperCase(b);
            }
        }

        /* Initialize the state: */
        n=INITIAL_N;
        i=0;
        bias=INITIAL_BIAS;
        firstSupplementaryIndex=1000000000;

        /*
         * Main decoding loop:
         * Start just after the last delimiter if any
         * basic code points were copied; start at the beginning otherwise.
         */
        for(in=basicLength>0 ? basicLength+1 : 0; in<srcLength; /* no op */) {
            /*
             * in is the index of the next character to be consumed, and
             * destCPCount is the number of code points in the output array.
             *
             * Decode a generalized variable-length integer into delta,
             * which gets added to i.  The overflow checking is easier
             * if we increase i as we go, then subtract off its starting
             * value at the end to obtain delta.
             */
            for(oldi=i, w=1, k=BASE; /* no condition */; k+=BASE) {
                if(in>=srcLength) {
                    throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
                }

                digit=basicToDigit[src.charAt(in++) & 0xFF];
                if(digit<0) {
                    throw new StringPrepParseException("Invalid char found", StringPrepParseException.INVALID_CHAR_FOUND);
                }
                if(digit>(0x7fffffff-i)/w) {
                    /* integer overflow */
                    throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
                }

                i+=digit*w;
                t=k-bias;
                if(t<TMIN) {
                    t=TMIN;
                } else if(k>=(bias+TMAX)) {
                    t=TMAX;
                }
                if(digit<t) {
                    break;
                }

                if(w>0x7fffffff/(BASE-t)) {
                    /* integer overflow */
                    throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
                }
                w*=BASE-t;
            }

            /*
             * Modification from sample code:
             * Increments destCPCount here,
             * where needed instead of in for() loop tail.
             */
            ++destCPCount;
            bias=adaptBias(i-oldi, destCPCount, (oldi==0));

            /*
             * i was supposed to wrap around from (incremented) destCPCount to 0,
             * incrementing n each time, so we'll fix that now:
             */
            if(i/destCPCount>(0x7fffffff-n)) {
                /* integer overflow */
                throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
            }

            n+=i/destCPCount;
            i%=destCPCount;
            /* not needed for Punycode: */
            /* if (decode_digit(n) <= BASE) return punycode_invalid_input; */

            if(n>0x10ffff || isSurrogate(n)) {
                /* Unicode code point overflow */
                throw new StringPrepParseException("Illegal char found", StringPrepParseException.ILLEGAL_CHAR_FOUND);
            }

            /* Insert n at position i of the output: */
            cpLength=Character.charCount(n);
            int codeUnitIndex;

            /*
             * Handle indexes when supplementary code points are present.
             *
             * In almost all cases, there will be only BMP code points before i
             * and even in the entire string.
             * This is handled with the same efficiency as with UTF-32.
             *
             * Only the rare cases with supplementary code points are handled
             * more slowly - but not too bad since this is an insertion anyway.
             */
            if(i<=firstSupplementaryIndex) {
                codeUnitIndex=i;
                if(cpLength>1) {
                    firstSupplementaryIndex=codeUnitIndex;
                } else {
                    ++firstSupplementaryIndex;
                }
            } else {
                codeUnitIndex=dest.offsetByCodePoints(firstSupplementaryIndex, i-firstSupplementaryIndex);
            }

            /* use the UChar index codeUnitIndex instead of the code point index i */
            if(caseFlags!=null && (dest.length()+cpLength)<=caseFlags.length) {
                if(codeUnitIndex<dest.length()) {
                    System.arraycopy(caseFlags, codeUnitIndex,
                                     caseFlags, codeUnitIndex+cpLength,
                                     dest.length()-codeUnitIndex);
                }
                /* Case of last character determines uppercase flag: */
                caseFlags[codeUnitIndex]=isBasicUpperCase(src.charAt(in-1));
                if(cpLength==2) {
                    caseFlags[codeUnitIndex+1]=false;
                }
            }
            if(cpLength==1) {
                /* BMP, insert one code unit */
                dest.insert(codeUnitIndex, (char)n);
            } else {
                /* supplementary character, insert two code units */
                dest.insert(codeUnitIndex, UTF16.getLeadSurrogate(n));
                dest.insert(codeUnitIndex+1, UTF16.getTrailSurrogate(n));
            }
            ++i;
        }
        return dest;
    }
218823
public static byte[] unboxBytes(Object src, int srcPos, int len) {
        return unboxBytes(array(src), srcPos, len);
    }
1265729
def _long_to_bytes(n, length, byteorder):
    
    if byteorder == 'little':
        indexes = range(length)
    else:
        indexes = reversed(range(length))
    return bytearray((n >> i * 8) & 0xff for i in indexes)
969337
@Override
    public int encode ( byte[] dst, int dstIndex ) {
        int start = dstIndex;
        dst[ dstIndex ] = (byte) ( this.replaceIfExists ? 1 : 0 );
        dstIndex += 8; // 7 Reserved
        dstIndex += 8; // RootDirectory = 0

        byte[] nameBytes = this.fileName.getBytes(StandardCharsets.UTF_16LE);

        SMBUtil.writeInt4(nameBytes.length, dst, dstIndex);
        dstIndex += 4;

        System.arraycopy(nameBytes, 0, dst, dstIndex, nameBytes.length);
        dstIndex += nameBytes.length;

        return dstIndex - start;
    }
948720
public static String toHexString(byte[] src, int offset, int length) {
        return toHexString(new StringBuilder(length << 1), src, offset, length).toString();
    }
1167601
public static byte[] readAllBytes(URL src, int limit) throws IOException {
		byte[] read;

		try (InputStream srcStream = src.openStream()) {
			read = readAllBytes(srcStream, limit);
		}
		return read;
	}
1127386
public static <T extends Appendable> T toHexStringPadded(T dst, byte[] src) {
        return toHexStringPadded(dst, src, 0, src.length);
    }