Here you can find the source of zeroPadArray(byte[] source, int size)
Parameter | Description |
---|---|
source | The source array. |
size | The final array size. |
public static byte[] zeroPadArray(byte[] source, int size)
//package com.java2s; /*/*from w ww .j av a 2s .c om*/ * Copyright (c) Fabio Falcinelli 2016. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Pad a byte array with zeroes. If Source array length exceed size, then truncate it. * * @param source The source array. * @param size The final array size. * @return The array of size elements eventually zero-padded. */ public static byte[] zeroPadArray(byte[] source, int size) { byte[] data = new byte[size]; for (int i = 0; i < data.length; i++) { if (i < source.length) data[i] = source[i]; else data[i] = 0; } return data; } }