Java String Pad Left leftPadByteArray(byte[] source, int size)

Here you can find the source of leftPadByteArray(byte[] source, int size)

Description

Left pad byte array to a byte array with given size.

License

Open Source License

Parameter

Parameter Description
source Source array
size The final byte array size

Return

The new byte arrray or null if the source array is to large

Declaration

public static byte[] leftPadByteArray(byte[] source, int size) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w .j av a 2s.  c o  m*/
 * Copyright (c) 2010-2017 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Left pad byte array to a byte array with given size.
     *
     * @param source Source array
     * @param size The final byte array size
     * @return The new byte arrray or <code>null</code> if the source array is to large
     */
    public static byte[] leftPadByteArray(byte[] source, int size) {
        byte[] bs = new byte[size];

        if (size < source.length) {
            return null;
        }

        int startPos = size - source.length;
        System.arraycopy(source, 0, bs, startPos, source.length);

        return bs;
    }
}

Related

  1. leftPad(String text, int size)
  2. leftPad(String toPad, int numPads)
  3. leftPad(String value, int makeLength, char paddingCharacter)
  4. leftPad(String value, int size, String pad)
  5. leftPad(StringBuilder pStringBuilder, int pLength, char pChar)
  6. leftPadded(String src, int len)
  7. leftPaddedString(String string, int length, char paddingChar)
  8. leftPadding(final StringBuffer strBuf, final int bufLen, final char fill)
  9. leftPadding(String in, int count, char pad)