Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Institute for Pervasive Computing, ETH Zurich and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * 
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *    http://www.eclipse.org/org/documents/edl-v10.html.
 * 
 * Contributors:
 *    Matthias Kovatsch - creator and main architect
 *    Stefan Jucker - DTLS implementation
 ******************************************************************************/

import java.util.Arrays;

public class Main {
    /**
     * Adds a padding to the given array, such that a new array with the given
     * length is generated.
     * 
     * @param array
     *            the array to be padded.
     * @param value
     *            the padding value.
     * @param newLength
     *            the new length of the padded array.
     * @return the array padded with the given value.
     */
    public static byte[] padArray(byte[] array, byte value, int newLength) {
        int length = array.length;
        int paddingLength = newLength - length;

        if (paddingLength < 1) {
            return array;
        } else {
            byte[] padding = new byte[paddingLength];
            Arrays.fill(padding, value);

            return concatenate(array, padding);
        }

    }

    /**
     * Concatenates two byte arrays.
     * 
     * @param a
     *            the first array.
     * @param b
     *            the second array.
     * @return the concatenated array.
     */
    public static byte[] concatenate(byte[] a, byte[] b) {
        int lengthA = a.length;
        int lengthB = b.length;

        byte[] concat = new byte[lengthA + lengthB];

        System.arraycopy(a, 0, concat, 0, lengthA);
        System.arraycopy(b, 0, concat, lengthA, lengthB);

        return concat;
    }
}