1 package net.jpountz.util;
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import static net.jpountz.util.Utils.NATIVE_BYTE_ORDER;
19 import java.lang.reflect.Field;
20 import java.nio.ByteOrder;
22 import sun.misc.Unsafe;
24 public enum UnsafeUtils {
27 private static final Unsafe UNSAFE;
28 private static final long BYTE_ARRAY_OFFSET;
29 private static final int BYTE_ARRAY_SCALE;
30 private static final long INT_ARRAY_OFFSET;
31 private static final int INT_ARRAY_SCALE;
32 private static final long SHORT_ARRAY_OFFSET;
33 private static final int SHORT_ARRAY_SCALE;
37 Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
38 theUnsafe.setAccessible(true);
39 UNSAFE = (Unsafe) theUnsafe.get(null);
40 BYTE_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
41 BYTE_ARRAY_SCALE = UNSAFE.arrayIndexScale(byte[].class);
42 INT_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(int[].class);
43 INT_ARRAY_SCALE = UNSAFE.arrayIndexScale(int[].class);
44 SHORT_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(short[].class);
45 SHORT_ARRAY_SCALE = UNSAFE.arrayIndexScale(short[].class);
46 } catch (IllegalAccessException e) {
47 throw new ExceptionInInitializerError("Cannot access Unsafe");
48 } catch (NoSuchFieldException e) {
49 throw new ExceptionInInitializerError("Cannot access Unsafe");
50 } catch (SecurityException e) {
51 throw new ExceptionInInitializerError("Cannot access Unsafe");
55 public static void checkRange(byte[] buf, int off) {
56 SafeUtils.checkRange(buf, off);
59 public static void checkRange(byte[] buf, int off, int len) {
60 SafeUtils.checkRange(buf, off, len);
63 public static void checkLength(int len) {
64 SafeUtils.checkLength(len);
67 public static byte readByte(byte[] src, int srcOff) {
68 return UNSAFE.getByte(src, BYTE_ARRAY_OFFSET + BYTE_ARRAY_SCALE * srcOff);
71 public static void writeByte(byte[] src, int srcOff, byte value) {
72 UNSAFE.putByte(src, BYTE_ARRAY_OFFSET + BYTE_ARRAY_SCALE * srcOff, (byte) value);
75 public static void writeByte(byte[] src, int srcOff, int value) {
76 writeByte(src, srcOff, (byte) value);
79 public static long readLong(byte[] src, int srcOff) {
80 return UNSAFE.getLong(src, BYTE_ARRAY_OFFSET + srcOff);
83 public static long readLongLE(byte[] src, int srcOff) {
84 long i = readLong(src, srcOff);
85 if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
86 i = Long.reverseBytes(i);
91 public static void writeLong(byte[] dest, int destOff, long value) {
92 UNSAFE.putLong(dest, BYTE_ARRAY_OFFSET + destOff, value);
95 public static int readInt(byte[] src, int srcOff) {
96 return UNSAFE.getInt(src, BYTE_ARRAY_OFFSET + srcOff);
99 public static int readIntLE(byte[] src, int srcOff) {
100 int i = readInt(src, srcOff);
101 if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
102 i = Integer.reverseBytes(i);
107 public static void writeInt(byte[] dest, int destOff, int value) {
108 UNSAFE.putInt(dest, BYTE_ARRAY_OFFSET + destOff, value);
111 public static short readShort(byte[] src, int srcOff) {
112 return UNSAFE.getShort(src, BYTE_ARRAY_OFFSET + srcOff);
115 public static int readShortLE(byte[] src, int srcOff) {
116 short s = readShort(src, srcOff);
117 if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
118 s = Short.reverseBytes(s);
123 public static void writeShort(byte[] dest, int destOff, short value) {
124 UNSAFE.putShort(dest, BYTE_ARRAY_OFFSET + destOff, value);
127 public static void writeShortLE(byte[] buf, int off, int v) {
128 writeByte(buf, off, (byte) v);
129 writeByte(buf, off + 1, (byte) (v >>> 8));
132 public static int readInt(int[] src, int srcOff) {
133 return UNSAFE.getInt(src, INT_ARRAY_OFFSET + INT_ARRAY_SCALE * srcOff);
136 public static void writeInt(int[] dest, int destOff, int value) {
137 UNSAFE.putInt(dest, INT_ARRAY_OFFSET + INT_ARRAY_SCALE * destOff, value);
140 public static int readShort(short[] src, int srcOff) {
141 return UNSAFE.getShort(src, SHORT_ARRAY_OFFSET + SHORT_ARRAY_SCALE * srcOff) & 0xFFFF;
144 public static void writeShort(short[] dest, int destOff, int value) {
145 UNSAFE.putShort(dest, SHORT_ARRAY_OFFSET + SHORT_ARRAY_SCALE * destOff, (short) value);