Java Byte Array Copy memcpy(Object to, Object from, int size)

Here you can find the source of memcpy(Object to, Object from, int size)

Description

MEMORY

License

Open Source License

Declaration


public static void memcpy(Object to, Object from, int size) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * The MIT License (MIT)//  w  w  w. j a  v a 2 s . c  o m
 * 
 * Copyright (c) 2016 Dalibor Drgo? <emptychannelmc@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class Main {
    protected static Unsafe unsafe;

    /*** MEMORY ***/

    public static void memcpy(Object to, Object from, int size) {
        System.arraycopy(from, 0, to, 0, size);
    }

    public static void memcpy(Object to, int posto, Object from, int posfrom, int size) {
        System.arraycopy(from, posfrom, to, posto, size);
    }

    public static void memcpy(long to, long from, long sz) {
        getUnsafe().copyMemory(from, to, sz);
    }

    public static Unsafe getUnsafe() {
        if (unsafe == null) {
            Class<?> clz = Unsafe.class;
            Field[] fields = clz.getDeclaredFields();
            for (int i = 0, n = fields.length; i < n; i++) {
                Field f = fields[i];
                if (!f.getType().equals(Unsafe.class)) {
                    continue;
                }
                try {
                    f.setAccessible(true);
                    Unsafe unf = (Unsafe) f.get(null);
                    if (unf != null) {
                        return (unsafe = unf);
                    }
                } catch (Throwable t) {
                }
            }
        }
        return unsafe;
    }
}

Related

  1. copyBytesToString(byte[] src, int arg1, int arg2)
  2. copyBytesWithin(byte[] bytes, int target, int start, int end)
  3. memcpy(byte[] dest, int doff, byte[] src, int soff, int len)
  4. memcpy(byte[] destBytes, byte[] srcBytes, int numBytes)
  5. memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length)