Java tutorial
//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 ******************************************************************************/ public class Main { /** * Truncates the given array to the request length. * * @param array * the array to be truncated. * @param newLength * the new length in bytes. * @return the truncated array. */ public static byte[] truncate(byte[] array, int newLength) { if (array.length < newLength) { return array; } else { byte[] truncated = new byte[newLength]; System.arraycopy(array, 0, truncated, 0, newLength); return truncated; } } }