| 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 io.airlift.slice.ChunkedSliceInput.BufferReference; |
| 17 |
import io.airlift.slice.ChunkedSliceInput.SliceLoader; |
| 18 |
|
| 19 |
import static com.google.common.base.Preconditions.checkPositionIndex; |
| 20 |
|
| 21 |
public class TestChunkedSliceInput |
| 22 |
extends AbstractSliceInputTest |
| 23 |
{ |
| 24 |
@Override |
| 25 |
protected SliceInput createSliceInput(Slice slice) |
| 26 |
{ |
| 27 |
return new ChunkedSliceInput(new SliceSliceLoader(slice), BUFFER_SIZE); |
| 28 |
} |
| 29 |
|
| 30 |
private static class SliceSliceLoader |
| 31 |
implements SliceLoader<BufferReference> |
| 32 |
{ |
| 33 |
private final Slice data; |
| 34 |
|
| 35 |
public SliceSliceLoader(Slice data) |
| 36 |
{ |
| 37 |
this.data = data; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public BufferReference createBuffer(int bufferSize) |
| 42 |
{ |
| 43 |
final Slice slice = Slices.allocate(bufferSize); |
| 44 |
return new BufferReference() { |
| 45 |
@Override |
| 46 |
public Slice getSlice() |
| 47 |
{ |
| 48 |
return slice; |
| 49 |
} |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public long getSize() |
| 55 |
{ |
| 56 |
return data.length(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void load(long position, BufferReference buffer, int length) |
| 61 |
{ |
| 62 |
checkPositionIndex((int) (position + length), (int) getSize()); |
| 63 |
this.data.getBytes((int) position, buffer.getSlice(), 0, length); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void close() |
| 68 |
{ |
| 69 |
} |
| 70 |
} |
| 71 |
} |