|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * 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 | +package org.elasticsearch.common.bytes; |
| 20 | + |
| 21 | +import org.apache.lucene.util.BytesRef; |
| 22 | +import org.apache.lucene.util.BytesRefIterator; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | + |
| 25 | +import java.io.EOFException; |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +/** |
| 29 | + * A StreamInput that reads off a {@link BytesRefIterator}. This is used to provide |
| 30 | + * generic stream access to {@link BytesReference} instances without materializing the |
| 31 | + * underlying bytes reference. |
| 32 | + */ |
| 33 | +final class BytesReferenceStreamInput extends StreamInput { |
| 34 | + private final BytesRefIterator iterator; |
| 35 | + private int sliceOffset; |
| 36 | + private BytesRef slice; |
| 37 | + private final int length; // the total size of the stream |
| 38 | + private int offset; // the current position of the stream |
| 39 | + |
| 40 | + public BytesReferenceStreamInput(BytesRefIterator iterator, final int length) throws IOException { |
| 41 | + this.iterator = iterator; |
| 42 | + this.slice = iterator.next(); |
| 43 | + this.length = length; |
| 44 | + this.offset = 0; |
| 45 | + this.sliceOffset = 0; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public byte readByte() throws IOException { |
| 50 | + if (offset >= length) { |
| 51 | + throw new EOFException(); |
| 52 | + } |
| 53 | + maybeNextSlice(); |
| 54 | + byte b = slice.bytes[slice.offset + (sliceOffset++)]; |
| 55 | + offset++; |
| 56 | + return b; |
| 57 | + } |
| 58 | + |
| 59 | + private void maybeNextSlice() throws IOException { |
| 60 | + while (sliceOffset == slice.length) { |
| 61 | + slice = iterator.next(); |
| 62 | + sliceOffset = 0; |
| 63 | + if (slice == null) { |
| 64 | + throw new EOFException(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void readBytes(byte[] b, int bOffset, int len) throws IOException { |
| 71 | + if (offset + len > length) { |
| 72 | + throw new IndexOutOfBoundsException("Cannot read " + len + " bytes from stream with length " + length + " at offset " + offset); |
| 73 | + } |
| 74 | + read(b, bOffset, len); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public int read() throws IOException { |
| 79 | + if (offset >= length) { |
| 80 | + return -1; |
| 81 | + } |
| 82 | + return Byte.toUnsignedInt(readByte()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int read(final byte[] b, final int bOffset, final int len) throws IOException { |
| 87 | + if (offset >= length) { |
| 88 | + return -1; |
| 89 | + } |
| 90 | + final int numBytesToCopy = Math.min(len, length - offset); |
| 91 | + int remaining = numBytesToCopy; // copy the full length or the remaining part |
| 92 | + int destOffset = bOffset; |
| 93 | + while (remaining > 0) { |
| 94 | + maybeNextSlice(); |
| 95 | + final int currentLen = Math.min(remaining, slice.length - sliceOffset); |
| 96 | + assert currentLen > 0 : "length has to be > 0 to make progress but was: " + currentLen; |
| 97 | + System.arraycopy(slice.bytes, slice.offset + sliceOffset, b, destOffset, currentLen); |
| 98 | + destOffset += currentLen; |
| 99 | + remaining -= currentLen; |
| 100 | + sliceOffset += currentLen; |
| 101 | + offset += currentLen; |
| 102 | + assert remaining >= 0 : "remaining: " + remaining; |
| 103 | + } |
| 104 | + return numBytesToCopy; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void close() throws IOException { |
| 109 | + // do nothing |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public int available() throws IOException { |
| 114 | + return length - offset; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public long skip(long n) throws IOException { |
| 119 | + final int skip = (int) Math.min(Integer.MAX_VALUE, n); |
| 120 | + final int numBytesSkipped = Math.min(skip, length - offset); |
| 121 | + int remaining = numBytesSkipped; |
| 122 | + while (remaining > 0) { |
| 123 | + maybeNextSlice(); |
| 124 | + int currentLen = Math.min(remaining, slice.length - (slice.offset + sliceOffset)); |
| 125 | + remaining -= currentLen; |
| 126 | + sliceOffset += currentLen; |
| 127 | + offset += currentLen; |
| 128 | + assert remaining >= 0 : "remaining: " + remaining; |
| 129 | + } |
| 130 | + return numBytesSkipped; |
| 131 | + } |
| 132 | + |
| 133 | + int getOffset() { |
| 134 | + return offset; |
| 135 | + } |
| 136 | +} |
0 commit comments