|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.io; |
| 21 | + |
| 22 | +import java.io.EOFException; |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +public abstract class ByteBufferInputStream extends SeekableInputStream { |
| 29 | + |
| 30 | + public static ByteBufferInputStream wrap(ByteBuffer... buffers) { |
| 31 | + if (buffers.length == 1) { |
| 32 | + return new SingleBufferInputStream(buffers[0]); |
| 33 | + } else { |
| 34 | + return new MultiBufferInputStream(Arrays.asList(buffers)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static ByteBufferInputStream wrap(List<ByteBuffer> buffers) { |
| 39 | + if (buffers.size() == 1) { |
| 40 | + return new SingleBufferInputStream(buffers.get(0)); |
| 41 | + } else { |
| 42 | + return new MultiBufferInputStream(buffers); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public long position() throws IOException { |
| 47 | + return getPos(); |
| 48 | + } |
| 49 | + |
| 50 | + public void skipFully(long n) throws IOException { |
| 51 | + long skipped = skip(n); |
| 52 | + if (skipped < n) { |
| 53 | + throw new EOFException( |
| 54 | + "Not enough bytes to skip: " + skipped + " < " + n); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public abstract int read(ByteBuffer out); |
| 59 | + |
| 60 | + public abstract ByteBuffer slice(int length) throws EOFException; |
| 61 | + |
| 62 | + public abstract List<ByteBuffer> sliceBuffers(long length) throws EOFException; |
| 63 | + |
| 64 | + public ByteBufferInputStream sliceStream(long length) throws EOFException { |
| 65 | + return ByteBufferInputStream.wrap(sliceBuffers(length)); |
| 66 | + } |
| 67 | + |
| 68 | + public abstract List<ByteBuffer> remainingBuffers(); |
| 69 | + |
| 70 | + public ByteBufferInputStream remainingStream() { |
| 71 | + return ByteBufferInputStream.wrap(remainingBuffers()); |
| 72 | + } |
| 73 | +} |
0 commit comments