Java Zero Format zero_pad(byte[] original, int block_size)

Here you can find the source of zero_pad(byte[] original, int block_size)

Description

Return a new array equal to original except zero-padded to an integral mulitple of blocks.

License

Open Source License

Parameter

Parameter Description
original the array of <code>byte</code>s to be padded
block_size the size of the blocks

Return

an array whose size divides block_size exactly. The array is either original itself, or a copy whose first original.length bytes are equal to original.

Declaration

public static byte[] zero_pad(byte[] original, int block_size) 

Method Source Code

//package com.java2s;
/*/*from w  w w . ja  v a2s  .c  om*/
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library 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 Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

public class Main {
    /**
     * Return a new array equal to original except zero-padded to an integral
     * mulitple of blocks.  If the original is already an integral multiple of
     * blocks, just return it.
     *
     * @param original the array of <code>byte</code>s to be padded
     * @param block_size the size of the blocks
     *
     * @return an array whose size divides <code>block_size</code> exactly. The
     *          array is either <code>original</code> itself, or a copy whose
     *          first <code>original.length</code> bytes are equal to
     *          <code>original</code>.
     */
    public static byte[] zero_pad(byte[] original, int block_size) {
        if ((original.length % block_size) == 0) {
            return original;
        }

        byte[] result = new byte[round_up(original.length, block_size)];
        memcpy(result, 0, original, 0, original.length);

        // Unnecessary - jvm sets bytes to 0.
        // memclr (result, original.length, result.length - original.length);
        return result;
    }

    /**
     * Round a number up to a given multiple.
     *
     * @param value the number to be rounded
     * @param multiple the number to which to be rounded
     *
     * @return the smallest <code>int</code> greater than or equal to
     *          <code>value</code> which divides <code>multiple</code> exactly.
     */
    public static int round_up(int value, int multiple) {
        return (((value - 1) / multiple) + 1) * multiple;
    }

    /**
     * Copy contents of one array of <code>bytes</code> into another. If either
     * array is <code>null</code>, simply return the <code>length</code>
     * parameter directly.
     *
     * @param dst the array to write, or <code>null</code>
     * @param dst_offset the start offset in <code>dst</code>
     * @param src the array to read, or <code>null</code>
     * @param src_offset the start offset in <code>src</code>
     * @param length the number of <code>byte</code>s to copy.
     *
     * @return DOCUMENT ME!
     */
    public static int memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length) {
        if ((dst != null) && (src != null)) {
            if (dst.length < (dst_offset + length)) {
                croak("dst.length = " + dst.length + ", but " + "dst_offset = " + dst_offset + " and length = "
                        + length + ".");
            }

            if (src.length < (src_offset + length)) {
                croak("src.length = " + src.length + ", but " + "src_offset = " + src_offset + " and length = "
                        + length + ".");
            }

            for (int i = 0; i < length; ++i, ++dst_offset, ++src_offset)
                dst[dst_offset] = src[src_offset];
        }

        return length;
    }

    /**
     * DOCUMENT ME!
     *
     * @param msg DOCUMENT ME!
     */
    private static void croak(String msg) {
        // throw new java.AssertionViolatedException(msg);
    }
}

Related

  1. zero(byte[] bytes, int off, int len)
  2. zero(byte[]... arrays)
  3. zero(double[] zero)
  4. zero(int bits)
  5. zero(int x)
  6. zeroAlign(String x, int count)
  7. zeroBasedTemplateStart(final int[] actions)
  8. zeroBlock(byte[] block, int off, int len)
  9. zeroConvert(String src)