Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2012-2015 Codenvy, S.A. * 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 * * Contributors: * Codenvy, S.A. - initial API and implementation *******************************************************************************/ import java.nio.charset.Charset; import static java.lang.System.arraycopy; public class Main { public static final Charset UTF_8 = Charset.forName("utf-8"); /** * <pre> * New content schema: * * [0 - pos) + content + [pos, src.elementLength) * </pre> * * @param src * source array * @param pos * start position for content insertion * @param content * content which will be inserted from {@param anchor} * @return new content */ public static byte[] insertInto(byte[] src, int pos, String content) { final byte[] contentSrc = content.getBytes(UTF_8); final byte[] newSrc = new byte[src.length + contentSrc.length]; arraycopy(src, 0, newSrc, 0, pos); arraycopy(contentSrc, 0, newSrc, pos, contentSrc.length); arraycopy(src, pos, newSrc, pos + contentSrc.length, src.length - pos); return newSrc; } }