]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/util/SafeUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / util / SafeUtils.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 java.nio.ByteOrder;
18
19 public enum SafeUtils {
20   ;
21
22   public static void checkRange(byte[] buf, int off) {
23     if (off < 0 || off >= buf.length) {
24       throw new ArrayIndexOutOfBoundsException(off);
25     }
26   }
27
28   public static void checkRange(byte[] buf, int off, int len) {
29     checkLength(len);
30     if (len > 0) {
31       checkRange(buf, off);
32       checkRange(buf, off + len - 1);
33     }
34   }
35
36   public static void checkLength(int len) {
37     if (len < 0) {
38       throw new IllegalArgumentException("lengths must be >= 0");
39     }
40   }
41
42   public static byte readByte(byte[] buf, int i) {
43     return buf[i];
44   }
45
46   public static int readIntBE(byte[] buf, int i) {
47     return ((buf[i] & 0xFF) << 24) | ((buf[i+1] & 0xFF) << 16) | ((buf[i+2] & 0xFF) << 8) | (buf[i+3] & 0xFF);
48   }
49
50   public static int readIntLE(byte[] buf, int i) {
51     return (buf[i] & 0xFF) | ((buf[i+1] & 0xFF) << 8) | ((buf[i+2] & 0xFF) << 16) | ((buf[i+3] & 0xFF) << 24);
52   }
53
54   public static int readInt(byte[] buf, int i) {
55     if (Utils.NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
56       return readIntBE(buf, i);
57     } else {
58       return readIntLE(buf, i);
59     }
60   }
61
62   public static long readLongLE(byte[] buf, int i) {
63     return (buf[i] & 0xFFL) | ((buf[i+1] & 0xFFL) << 8) | ((buf[i+2] & 0xFFL) << 16) | ((buf[i+3] & 0xFFL) << 24)
64          | ((buf[i+4] & 0xFFL) << 32) | ((buf[i+5] & 0xFFL) << 40) | ((buf[i+6] & 0xFFL) << 48) | ((buf[i+7] & 0xFFL) << 56);
65   }
66
67   public static void writeShortLE(byte[] buf, int off, int v) {
68     buf[off++] = (byte) v;
69     buf[off++] = (byte) (v >>> 8);
70   }
71
72   public static void writeInt(int[] buf, int off, int v) {
73     buf[off] = v;
74   }
75
76   public static int readInt(int[] buf, int off) {
77     return buf[off];
78   }
79
80   public static void writeByte(byte[] dest, int off, int i) {
81     dest[off] = (byte) i;
82   }
83
84   public static void writeShort(short[] buf, int off, int v) {
85     buf[off] = (short) v;
86   }
87
88   public static int readShortLE(byte[] buf, int i) {
89     return (buf[i] & 0xFF) | ((buf[i+1] & 0xFF) << 8);
90   }
91
92   public static int readShort(short[] buf, int off) {
93     return buf[off] & 0xFFFF;
94   }
95 }