| 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 java.io.IOException; |
| 17 |
import java.io.OutputStream; |
| 18 |
import java.nio.charset.Charset; |
| 19 |
|
| 20 |
import static io.airlift.slice.Preconditions.checkNotNull; |
| 21 |
import static io.airlift.slice.Preconditions.checkPositionIndex; |
| 22 |
import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE; |
| 23 |
import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT; |
| 24 |
import static io.airlift.slice.SizeOf.SIZE_OF_INT; |
| 25 |
import static io.airlift.slice.SizeOf.SIZE_OF_LONG; |
| 26 |
import static io.airlift.slice.SizeOf.SIZE_OF_SHORT; |
| 27 |
|
| 28 |
@SuppressWarnings("JavaDoc") // IDEA-81310 |
| 29 |
public final class BasicSliceInput |
| 30 |
extends FixedLengthSliceInput |
| 31 |
{ |
| 32 |
private final Slice slice; |
| 33 |
private int position; |
| 34 |
|
| 35 |
public BasicSliceInput(Slice slice) |
| 36 |
{ |
| 37 |
this.slice = checkNotNull(slice, "slice is null"); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public long length() |
| 42 |
{ |
| 43 |
return slice.length(); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public long position() |
| 48 |
{ |
| 49 |
return position; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void setPosition(long position) |
| 54 |
{ |
| 55 |
checkPositionIndex(position, slice.length()); |
| 56 |
this.position = (int) position; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public boolean isReadable() |
| 61 |
{ |
| 62 |
return position < slice.length(); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public int available() |
| 67 |
{ |
| 68 |
return slice.length() - position; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean readBoolean() |
| 73 |
{ |
| 74 |
return readByte() != 0; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public int read() |
| 79 |
{ |
| 80 |
if (position >= slice.length()) { |
| 81 |
return -1; |
| 82 |
} |
| 83 |
int result = slice.getByte(position) & 0xFF; |
| 84 |
position++; |
| 85 |
return result; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public byte readByte() |
| 90 |
{ |
| 91 |
int value = read(); |
| 92 |
if (value == -1) { |
| 93 |
throw new IndexOutOfBoundsException(); |
| 94 |
} |
| 95 |
return (byte) value; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public int readUnsignedByte() |
| 100 |
{ |
| 101 |
return readByte() & 0xFF; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public short readShort() |
| 106 |
{ |
| 107 |
short v = slice.getShort(position); |
| 108 |
position += SIZE_OF_SHORT; |
| 109 |
return v; |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public int readUnsignedShort() |
| 114 |
{ |
| 115 |
return readShort() & 0xFFFF; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public int readInt() |
| 120 |
{ |
| 121 |
int v = slice.getInt(position); |
| 122 |
position += SIZE_OF_INT; |
| 123 |
return v; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public long readLong() |
| 128 |
{ |
| 129 |
long v = slice.getLong(position); |
| 130 |
position += SIZE_OF_LONG; |
| 131 |
return v; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public float readFloat() |
| 136 |
{ |
| 137 |
float v = slice.getFloat(position); |
| 138 |
position += SIZE_OF_FLOAT; |
| 139 |
return v; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public double readDouble() |
| 144 |
{ |
| 145 |
double v = slice.getDouble(position); |
| 146 |
position += SIZE_OF_DOUBLE; |
| 147 |
return v; |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public Slice readSlice(int length) |
| 152 |
{ |
| 153 |
if (length == 0) { |
| 154 |
return Slices.EMPTY_SLICE; |
| 155 |
} |
| 156 |
Slice newSlice = slice.slice(position, length); |
| 157 |
position += length; |
| 158 |
return newSlice; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public int read(byte[] destination, int destinationIndex, int length) |
| 163 |
{ |
| 164 |
if (length == 0) { |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
length = Math.min(length, available()); |
| 169 |
if (length == 0) { |
| 170 |
return -1; |
| 171 |
} |
| 172 |
readBytes(destination, destinationIndex, length); |
| 173 |
return length; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void readBytes(byte[] destination, int destinationIndex, int length) |
| 178 |
{ |
| 179 |
slice.getBytes(position, destination, destinationIndex, length); |
| 180 |
position += length; |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void readBytes(Slice destination, int destinationIndex, int length) |
| 185 |
{ |
| 186 |
slice.getBytes(position, destination, destinationIndex, length); |
| 187 |
position += length; |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void readBytes(OutputStream out, int length) |
| 192 |
throws IOException |
| 193 |
{ |
| 194 |
slice.getBytes(position, out, length); |
| 195 |
position += length; |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public long skip(long length) |
| 200 |
{ |
| 201 |
length = Math.min(length, available()); |
| 202 |
position += length; |
| 203 |
return length; |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public int skipBytes(int length) |
| 208 |
{ |
| 209 |
length = Math.min(length, available()); |
| 210 |
position += length; |
| 211 |
return length; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns a slice of this buffer's readable bytes. Modifying the content |
| 216 |
* of the returned buffer or this buffer affects each other's content |
| 217 |
* while they maintain separate indexes and marks. This method is |
| 218 |
* identical to {@code buf.slice(buf.position(), buf.available()())}. |
| 219 |
* This method does not modify {@code position} or {@code writerIndex} of |
| 220 |
* this buffer. |
| 221 |
*/ |
| 222 |
public Slice slice() |
| 223 |
{ |
| 224 |
return slice.slice(position, slice.length() - position); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Decodes this buffer's readable bytes into a string with the specified |
| 229 |
* character set name. This method is identical to |
| 230 |
* {@code buf.toString(buf.position(), buf.available()(), charsetName)}. |
| 231 |
* This method does not modify {@code position} or {@code writerIndex} of |
| 232 |
* this buffer. |
| 233 |
*/ |
| 234 |
public String toString(Charset charset) |
| 235 |
{ |
| 236 |
return slice.toString(position, available(), charset); |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public String toString() |
| 241 |
{ |
| 242 |
StringBuilder builder = new StringBuilder("BasicSliceInput{"); |
| 243 |
builder.append("position=").append(position); |
| 244 |
builder.append(", capacity=").append(slice.length()); |
| 245 |
builder.append('}'); |
| 246 |
return builder.toString(); |
| 247 |
} |
| 248 |
} |