Java Integer Pad Zero zeroPad(final byte[] data, final int blockSize)

Here you can find the source of zeroPad(final byte[] data, final int blockSize)

Description

zero Pad

License

Open Source License

Declaration

public static byte[] zeroPad(final byte[] data, final int blockSize) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2017 JCrypTool Team and Contributors
 *
 * 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 {
    public static byte[] zeroPad(final byte[] data, final int blockSize) {
        if (data == null) {
            throw new IllegalArgumentException(
                    "Please provide some data to pad");
        }/*from  w  ww.  ja v  a  2 s.  c om*/

        if (blockSize <= 0 || blockSize % Byte.SIZE != 0) {
            throw new IllegalArgumentException(
                    "Blocksize must be a possitive integer N where N % 8 = 0");
        }

        final int blockSizeBytes = blockSize / Byte.SIZE;

        // lets make padding data that is already sized correctly *very* fast
        if (data.length % blockSizeBytes == 0) {
            return data;
        }

        // -1 not really needed, because % blockSizeBytes has already returned, but whatever
        final int blocks = (data.length - 1) / blockSizeBytes + 1;

        // create the new, padded byte array containing the calculated number of blocks
        final byte[] newData = new byte[blocks * blockSizeBytes];
        System.arraycopy(data, 0, newData, 0, data.length);
        return newData;
    }
}

Related

  1. zeroPad(final int amount, final String in)
  2. zeroPad(final int length, final byte[] bytes)
  3. zeropad(final int num, final int size)
  4. zeroPad(int i, int len)