Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 - left) + content + (right, src.elementLength)
     * </pre>
     *
     * @param src
     *         source array
     * @param left
     *         left anchor - not included to result
     * @param right
     *         right anchor - not included to result
     * @param content
     *         content which will be inserted between left and right
     * @return new content
     */
    public static byte[] insertBetween(byte[] src, int left, int right, String content) {
        final byte[] contentSrc = content.getBytes(UTF_8);
        final byte[] newSrc = new byte[left + src.length - right + contentSrc.length - 1];
        arraycopy(src, 0, newSrc, 0, left);
        arraycopy(contentSrc, 0, newSrc, left, contentSrc.length);
        arraycopy(src, right + 1, newSrc, left + contentSrc.length, src.length - right - 1);
        return newSrc;
    }
}