pad byte array from Left - Java java.lang

Java examples for java.lang:byte Array

Description

pad byte array from Left

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] array = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int length = 2;
        System.out.println(java.util.Arrays
                .toString(padLeft(array, length)));
    }//from w  w w .ja  v  a2  s .  c o m

    public static byte[] padLeft(byte[] array, int length) {

        if (array.length >= length) {
            return array;
        }

        byte[] buffer = new byte[length];
        System.arraycopy(array, 0, buffer, buffer.length - array.length,
                array.length);
        return buffer;

    }
}

Related Tutorials