1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.tools.jaotc.jnilibelf; 25 26 import jdk.internal.misc.Unsafe; 27 28 import static jdk.tools.jaotc.jnilibelf.UnsafeAccess.UNSAFE; 29 30 public class Pointer { 31 32 private final long address; 33 34 public Pointer(long val) { 35 address = val; 36 } 37 38 /** 39 * Put (i.e., copy) content of byte array at consecutive addresses beginning at this Pointer. 40 * 41 * @param src source byte array 42 */ 43 public void put(byte[] src) { 44 UNSAFE.copyMemory(src, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, address, src.length); 45 } 46 47 /** 48 * Get (i.e., copy) content at this Pointer to the given byte array. 49 * 50 * @param dst destination byte array 51 */ 52 public void get(byte[] dst) { 53 UNSAFE.copyMemory(null, address, dst, Unsafe.ARRAY_BYTE_BASE_OFFSET, dst.length); 54 } 55 56 /** 57 * Read {@code readSize} number of bytes to copy them starting at {@code startIndex} of 58 * {@code byteArray} 59 * 60 * @param byteArray target array to copy bytes 61 * @param readSize number of bytes to copy 62 * @param startIndex index of the array to start copy at 63 */ 64 public void copyBytesTo(byte[] byteArray, int readSize, int startIndex) { 65 long end = (long)startIndex + (long)readSize; 66 if (end > byteArray.length) { 67 throw new IllegalArgumentException("writing beyond array bounds"); 68 } 69 UNSAFE.copyMemory(null, address, byteArray, Unsafe.ARRAY_BYTE_BASE_OFFSET+startIndex, readSize); 70 } 71 72 }