]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/util/UnsafeUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / util / UnsafeUtils.java
1 package net.jpountz.util;
2
3 /*
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 import static net.jpountz.util.Utils.NATIVE_BYTE_ORDER;
18
19 import java.lang.reflect.Field;
20 import java.nio.ByteOrder;
21
22 import sun.misc.Unsafe;
23
24 public enum UnsafeUtils {
25   ;
26
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;
34   
35   static {
36     try {
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");
52     }
53   }
54
55   public static void checkRange(byte[] buf, int off) {
56     SafeUtils.checkRange(buf, off);
57   }
58
59   public static void checkRange(byte[] buf, int off, int len) {
60     SafeUtils.checkRange(buf, off, len);
61   }
62
63   public static void checkLength(int len) {
64     SafeUtils.checkLength(len);
65   }
66
67   public static byte readByte(byte[] src, int srcOff) {
68     return UNSAFE.getByte(src, BYTE_ARRAY_OFFSET + BYTE_ARRAY_SCALE * srcOff);
69   }
70
71   public static void writeByte(byte[] src, int srcOff, byte value) {
72     UNSAFE.putByte(src, BYTE_ARRAY_OFFSET + BYTE_ARRAY_SCALE * srcOff, (byte) value);
73   }
74
75   public static void writeByte(byte[] src, int srcOff, int value) {
76     writeByte(src, srcOff, (byte) value);
77   }
78
79   public static long readLong(byte[] src, int srcOff) {
80     return UNSAFE.getLong(src, BYTE_ARRAY_OFFSET + srcOff);
81   }
82
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);
87     }
88     return i;
89   }
90
91   public static void writeLong(byte[] dest, int destOff, long value) {
92     UNSAFE.putLong(dest, BYTE_ARRAY_OFFSET + destOff, value);
93   }
94
95   public static int readInt(byte[] src, int srcOff) {
96     return UNSAFE.getInt(src, BYTE_ARRAY_OFFSET + srcOff);
97   }
98
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);
103     }
104     return i;
105   }
106
107   public static void writeInt(byte[] dest, int destOff, int value) {
108     UNSAFE.putInt(dest, BYTE_ARRAY_OFFSET + destOff, value);
109   }
110
111   public static short readShort(byte[] src, int srcOff) {
112     return UNSAFE.getShort(src, BYTE_ARRAY_OFFSET + srcOff);
113   }
114
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);
119     }
120     return s & 0xFFFF;
121   }
122
123   public static void writeShort(byte[] dest, int destOff, short value) {
124     UNSAFE.putShort(dest, BYTE_ARRAY_OFFSET + destOff, value);
125   }
126
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));
130   }
131
132   public static int readInt(int[] src, int srcOff) {
133     return UNSAFE.getInt(src, INT_ARRAY_OFFSET + INT_ARRAY_SCALE * srcOff);
134   }
135
136   public static void writeInt(int[] dest, int destOff, int value) {
137     UNSAFE.putInt(dest, INT_ARRAY_OFFSET + INT_ARRAY_SCALE * destOff, value);
138   }
139
140   public static int readShort(short[] src, int srcOff) {
141     return UNSAFE.getShort(src, SHORT_ARRAY_OFFSET + SHORT_ARRAY_SCALE * srcOff) & 0xFFFF;
142   }
143
144   public static void writeShort(short[] dest, int destOff, int value) {
145     UNSAFE.putShort(dest, SHORT_ARRAY_OFFSET + SHORT_ARRAY_SCALE * destOff, (short) value);
146   }
147 }