1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.lang.reflect.Field; |
23 | |
|
24 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_BOOLEAN; |
25 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_BYTE; |
26 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_CHAR; |
27 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_SHORT; |
28 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_INT; |
29 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_LONG; |
30 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_FLOAT; |
31 | |
import static org.apache.giraph.utils.ByteUtils.SIZE_OF_DOUBLE; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
@edu.umd.cs.findbugs.annotations.SuppressWarnings( |
38 | |
"RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") |
39 | |
public class UnsafeArrayReads extends UnsafeReads { |
40 | |
|
41 | |
private static final sun.misc.Unsafe UNSAFE; |
42 | |
static { |
43 | |
try { |
44 | 0 | Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); |
45 | 0 | field.setAccessible(true); |
46 | 0 | UNSAFE = (sun.misc.Unsafe) field.get(null); |
47 | |
|
48 | |
|
49 | 0 | } catch (Exception e) { |
50 | |
|
51 | 0 | throw new RuntimeException("UnsafeArrayReads: Failed to " + |
52 | |
"get unsafe", e); |
53 | 0 | } |
54 | |
} |
55 | |
|
56 | 0 | private static final long BYTE_ARRAY_OFFSET = |
57 | 0 | UNSAFE.arrayBaseOffset(byte[].class); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public UnsafeArrayReads(byte[] buf) { |
65 | 0 | super(buf.length); |
66 | 0 | this.buffer = buf; |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public UnsafeArrayReads(byte[] buf, int offset, int length) { |
77 | 0 | super(offset, length); |
78 | 0 | this.buffer = buf; |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public int available() { |
83 | 0 | return (int) (limit - position); |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public boolean endOfInput() { |
88 | 0 | return available() == 0; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
@Override |
93 | |
public int getPos() { |
94 | 0 | return (int) position; |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
public void readFully(byte[] b) throws IOException { |
99 | 0 | require(b.length); |
100 | 0 | System.arraycopy(buffer, (int) position, b, 0, b.length); |
101 | 0 | position += b.length; |
102 | 0 | } |
103 | |
|
104 | |
@Override |
105 | |
public void readFully(byte[] b, int off, int len) throws IOException { |
106 | 0 | require(len); |
107 | 0 | System.arraycopy(buffer, (int) position, b, off, len); |
108 | 0 | position += len; |
109 | 0 | } |
110 | |
|
111 | |
@Override |
112 | |
public boolean readBoolean() { |
113 | 0 | require(SIZE_OF_BOOLEAN); |
114 | 0 | boolean value = UNSAFE.getBoolean(buffer, |
115 | |
BYTE_ARRAY_OFFSET + position); |
116 | 0 | position += SIZE_OF_BOOLEAN; |
117 | 0 | return value; |
118 | |
} |
119 | |
|
120 | |
@Override |
121 | |
public byte readByte() { |
122 | 0 | require(SIZE_OF_BYTE); |
123 | 0 | byte value = UNSAFE.getByte(buffer, |
124 | |
BYTE_ARRAY_OFFSET + position); |
125 | 0 | position += SIZE_OF_BYTE; |
126 | 0 | return value; |
127 | |
} |
128 | |
|
129 | |
@Override |
130 | |
public int readUnsignedByte() throws IOException { |
131 | 0 | return (short) (readByte() & 0xFF); |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public short readShort() { |
136 | 0 | require(SIZE_OF_SHORT); |
137 | 0 | short value = UNSAFE.getShort(buffer, |
138 | |
BYTE_ARRAY_OFFSET + position); |
139 | 0 | position += SIZE_OF_SHORT; |
140 | 0 | return value; |
141 | |
} |
142 | |
|
143 | |
@Override |
144 | |
public int readUnsignedShort() throws IOException { |
145 | 0 | return readShort() & 0xFFFF; |
146 | |
} |
147 | |
|
148 | |
@Override |
149 | |
public char readChar() { |
150 | 0 | require(SIZE_OF_CHAR); |
151 | 0 | char value = UNSAFE.getChar(buffer, |
152 | |
BYTE_ARRAY_OFFSET + position); |
153 | 0 | position += SIZE_OF_CHAR; |
154 | 0 | return value; |
155 | |
} |
156 | |
|
157 | |
@Override |
158 | |
public int readInt() { |
159 | 0 | require(SIZE_OF_INT); |
160 | 0 | int value = UNSAFE.getInt(buffer, |
161 | |
BYTE_ARRAY_OFFSET + position); |
162 | 0 | position += SIZE_OF_INT; |
163 | 0 | return value; |
164 | |
} |
165 | |
|
166 | |
@Override |
167 | |
public long readLong() { |
168 | 0 | require(SIZE_OF_LONG); |
169 | 0 | long value = UNSAFE.getLong(buffer, |
170 | |
BYTE_ARRAY_OFFSET + position); |
171 | 0 | position += SIZE_OF_LONG; |
172 | 0 | return value; |
173 | |
} |
174 | |
|
175 | |
@Override |
176 | |
public float readFloat() { |
177 | 0 | require(SIZE_OF_FLOAT); |
178 | 0 | float value = UNSAFE.getFloat(buffer, |
179 | |
BYTE_ARRAY_OFFSET + position); |
180 | 0 | position += SIZE_OF_FLOAT; |
181 | 0 | return value; |
182 | |
} |
183 | |
|
184 | |
@Override |
185 | |
public double readDouble() { |
186 | 0 | require(SIZE_OF_DOUBLE); |
187 | 0 | double value = UNSAFE.getDouble(buffer, |
188 | |
BYTE_ARRAY_OFFSET + position); |
189 | 0 | position += SIZE_OF_DOUBLE; |
190 | 0 | return value; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static int getInt(byte[] buf, int pos) { |
201 | 0 | return UNSAFE.getInt(buf, |
202 | |
BYTE_ARRAY_OFFSET + pos); |
203 | |
} |
204 | |
} |