| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import sun.nio.ch.DirectBuffer; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.io.FileNotFoundException; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.RandomAccessFile; |
| 22 |
import java.nio.ByteBuffer; |
| 23 |
import java.nio.MappedByteBuffer; |
| 24 |
import java.nio.channels.FileChannel; |
| 25 |
import java.nio.channels.FileChannel.MapMode; |
| 26 |
import java.nio.charset.Charset; |
| 27 |
|
| 28 |
import static io.airlift.slice.Preconditions.checkNotNull; |
| 29 |
import static io.airlift.slice.Preconditions.checkPositionIndexes; |
| 30 |
import static java.nio.charset.StandardCharsets.UTF_8; |
| 31 |
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET; |
| 32 |
|
| 33 |
public final class Slices |
| 34 |
{ |
| 35 |
/** |
| 36 |
* A slice with size {@code 0}. |
| 37 |
*/ |
| 38 |
public static final Slice EMPTY_SLICE = new Slice(); |
| 39 |
|
| 40 |
private static final int SLICE_ALLOC_THRESHOLD = 524_288; // 2^19 |
| 41 |
private static final double SLICE_ALLOW_SKEW = 1.25; // must be > 1! |
| 42 |
|
| 43 |
private Slices() {} |
| 44 |
|
| 45 |
public static Slice ensureSize(Slice existingSlice, int minWritableBytes) |
| 46 |
{ |
| 47 |
if (existingSlice == null) { |
| 48 |
return allocate(minWritableBytes); |
| 49 |
} |
| 50 |
|
| 51 |
if (minWritableBytes <= existingSlice.length()) { |
| 52 |
return existingSlice; |
| 53 |
} |
| 54 |
|
| 55 |
int newCapacity; |
| 56 |
if (existingSlice.length() == 0) { |
| 57 |
newCapacity = 1; |
| 58 |
} |
| 59 |
else { |
| 60 |
newCapacity = existingSlice.length(); |
| 61 |
} |
| 62 |
int minNewCapacity = minWritableBytes; |
| 63 |
while (newCapacity < minNewCapacity) { |
| 64 |
if (newCapacity < SLICE_ALLOC_THRESHOLD) { |
| 65 |
newCapacity <<= 1; |
| 66 |
} |
| 67 |
else { |
| 68 |
newCapacity *= SLICE_ALLOW_SKEW; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
Slice newSlice = allocate(newCapacity); |
| 73 |
newSlice.setBytes(0, existingSlice, 0, existingSlice.length()); |
| 74 |
return newSlice; |
| 75 |
} |
| 76 |
|
| 77 |
public static Slice allocate(int capacity) |
| 78 |
{ |
| 79 |
if (capacity == 0) { |
| 80 |
return EMPTY_SLICE; |
| 81 |
} |
| 82 |
return new Slice(new byte[capacity]); |
| 83 |
} |
| 84 |
|
| 85 |
public static Slice allocateDirect(int capacity) |
| 86 |
{ |
| 87 |
if (capacity == 0) { |
| 88 |
return EMPTY_SLICE; |
| 89 |
} |
| 90 |
return wrappedBuffer(ByteBuffer.allocateDirect(capacity)); |
| 91 |
} |
| 92 |
|
| 93 |
public static Slice copyOf(Slice slice) |
| 94 |
{ |
| 95 |
return copyOf(slice, 0, slice.length()); |
| 96 |
} |
| 97 |
|
| 98 |
public static Slice copyOf(Slice slice, int offset, int length) |
| 99 |
{ |
| 100 |
checkPositionIndexes(offset, offset + length, slice.length()); |
| 101 |
|
| 102 |
Slice copy = Slices.allocate(length); |
| 103 |
copy.setBytes(0, slice, offset, length); |
| 104 |
|
| 105 |
return copy; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Wrap the visible portion of a {@link java.nio.ByteBuffer}. |
| 110 |
*/ |
| 111 |
public static Slice wrappedBuffer(ByteBuffer buffer) |
| 112 |
{ |
| 113 |
if (buffer instanceof DirectBuffer) { |
| 114 |
DirectBuffer direct = (DirectBuffer) buffer; |
| 115 |
return new Slice(null, direct.address() + buffer.position(), buffer.limit() - buffer.position(), buffer.capacity(), direct); |
| 116 |
} |
| 117 |
|
| 118 |
if (buffer.hasArray()) { |
| 119 |
int address = ARRAY_BYTE_BASE_OFFSET + buffer.arrayOffset() + buffer.position(); |
| 120 |
return new Slice(buffer.array(), address, buffer.limit() - buffer.position(), buffer.array().length, null); |
| 121 |
} |
| 122 |
|
| 123 |
throw new IllegalArgumentException("cannot wrap " + buffer.getClass().getName()); |
| 124 |
} |
| 125 |
|
| 126 |
public static Slice wrappedBuffer(byte... array) |
| 127 |
{ |
| 128 |
if (array.length == 0) { |
| 129 |
return EMPTY_SLICE; |
| 130 |
} |
| 131 |
return new Slice(array); |
| 132 |
} |
| 133 |
|
| 134 |
public static Slice wrappedBuffer(byte[] array, int offset, int length) |
| 135 |
{ |
| 136 |
if (length == 0) { |
| 137 |
return EMPTY_SLICE; |
| 138 |
} |
| 139 |
return new Slice(array, offset, length); |
| 140 |
} |
| 141 |
|
| 142 |
public static Slice wrappedBooleanArray(boolean... array) |
| 143 |
{ |
| 144 |
return wrappedBooleanArray(array, 0, array.length); |
| 145 |
} |
| 146 |
|
| 147 |
public static Slice wrappedBooleanArray(boolean[] array, int offset, int length) |
| 148 |
{ |
| 149 |
if (length == 0) { |
| 150 |
return EMPTY_SLICE; |
| 151 |
} |
| 152 |
return new Slice(array, offset, length); |
| 153 |
} |
| 154 |
|
| 155 |
public static Slice wrappedShortArray(short... array) |
| 156 |
{ |
| 157 |
return wrappedShortArray(array, 0, array.length); |
| 158 |
} |
| 159 |
|
| 160 |
public static Slice wrappedShortArray(short[] array, int offset, int length) |
| 161 |
{ |
| 162 |
if (length == 0) { |
| 163 |
return EMPTY_SLICE; |
| 164 |
} |
| 165 |
return new Slice(array, offset, length); |
| 166 |
} |
| 167 |
|
| 168 |
public static Slice wrappedIntArray(int... array) |
| 169 |
{ |
| 170 |
return wrappedIntArray(array, 0, array.length); |
| 171 |
} |
| 172 |
|
| 173 |
public static Slice wrappedIntArray(int[] array, int offset, int length) |
| 174 |
{ |
| 175 |
if (length == 0) { |
| 176 |
return EMPTY_SLICE; |
| 177 |
} |
| 178 |
return new Slice(array, offset, length); |
| 179 |
} |
| 180 |
|
| 181 |
public static Slice wrappedLongArray(long... array) |
| 182 |
{ |
| 183 |
return wrappedLongArray(array, 0, array.length); |
| 184 |
} |
| 185 |
|
| 186 |
public static Slice wrappedLongArray(long[] array, int offset, int length) |
| 187 |
{ |
| 188 |
if (length == 0) { |
| 189 |
return EMPTY_SLICE; |
| 190 |
} |
| 191 |
return new Slice(array, offset, length); |
| 192 |
} |
| 193 |
|
| 194 |
public static Slice wrappedFloatArray(float... array) |
| 195 |
{ |
| 196 |
return wrappedFloatArray(array, 0, array.length); |
| 197 |
} |
| 198 |
|
| 199 |
public static Slice wrappedFloatArray(float[] array, int offset, int length) |
| 200 |
{ |
| 201 |
if (length == 0) { |
| 202 |
return EMPTY_SLICE; |
| 203 |
} |
| 204 |
return new Slice(array, offset, length); |
| 205 |
} |
| 206 |
|
| 207 |
public static Slice wrappedDoubleArray(double... array) |
| 208 |
{ |
| 209 |
return wrappedDoubleArray(array, 0, array.length); |
| 210 |
} |
| 211 |
|
| 212 |
public static Slice wrappedDoubleArray(double[] array, int offset, int length) |
| 213 |
{ |
| 214 |
if (length == 0) { |
| 215 |
return EMPTY_SLICE; |
| 216 |
} |
| 217 |
return new Slice(array, offset, length); |
| 218 |
} |
| 219 |
|
| 220 |
public static Slice copiedBuffer(String string, Charset charset) |
| 221 |
{ |
| 222 |
checkNotNull(string, "string is null"); |
| 223 |
checkNotNull(charset, "charset is null"); |
| 224 |
|
| 225 |
return wrappedBuffer(string.getBytes(charset)); |
| 226 |
} |
| 227 |
|
| 228 |
public static Slice utf8Slice(String string) |
| 229 |
{ |
| 230 |
return copiedBuffer(string, UTF_8); |
| 231 |
} |
| 232 |
|
| 233 |
public static Slice mapFileReadOnly(File file) |
| 234 |
throws IOException |
| 235 |
{ |
| 236 |
checkNotNull(file, "file is null"); |
| 237 |
|
| 238 |
if (!file.exists()) { |
| 239 |
throw new FileNotFoundException(file.toString()); |
| 240 |
} |
| 241 |
|
| 242 |
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); |
| 243 |
FileChannel channel = randomAccessFile.getChannel()) { |
| 244 |
MappedByteBuffer byteBuffer = channel.map(MapMode.READ_ONLY, 0, file.length()); |
| 245 |
return wrappedBuffer(byteBuffer); |
| 246 |
} |
| 247 |
} |
| 248 |
} |