Example usage for org.apache.lucene.store IndexInput slice

List of usage examples for org.apache.lucene.store IndexInput slice

Introduction

In this page you can find the example usage for org.apache.lucene.store IndexInput slice.

Prototype

public abstract IndexInput slice(String sliceDescription, long offset, long length) throws IOException;

Source Link

Document

Creates a slice of this index input, with the given description, offset, and length.

Usage

From source file:org.elasticsearch.common.lucene.store.ByteArrayIndexInputTests.java

License:Apache License

private byte[] randomReadAndSlice(IndexInput indexInput, int length) throws IOException {
    int readPos = (int) indexInput.getFilePointer();
    byte[] output = new byte[length];
    while (readPos < length) {
        switch (randomIntBetween(0, 3)) {
        case 0:/*from w ww.jav a  2 s  .c o m*/
            // Read by one byte at a time
            output[readPos++] = indexInput.readByte();
            break;
        case 1:
            // Read several bytes into target
            int len = randomIntBetween(1, length - readPos);
            indexInput.readBytes(output, readPos, len);
            readPos += len;
            break;
        case 2:
            // Read several bytes into 0-offset target
            len = randomIntBetween(1, length - readPos);
            byte[] temp = new byte[len];
            indexInput.readBytes(temp, 0, len);
            System.arraycopy(temp, 0, output, readPos, len);
            readPos += len;
            break;
        case 3:
            // Read using slice
            len = randomIntBetween(1, length - readPos);
            IndexInput slice = indexInput
                    .slice("slice (" + readPos + ", " + len + ") of " + indexInput.toString(), readPos, len);
            temp = randomReadAndSlice(slice, len);
            // assert that position in the original input didn't change
            assertEquals(readPos, indexInput.getFilePointer());
            System.arraycopy(temp, 0, output, readPos, len);
            readPos += len;
            indexInput.seek(readPos);
            assertEquals(readPos, indexInput.getFilePointer());
            break;
        default:
            fail();
        }
        assertEquals((long) readPos, indexInput.getFilePointer());
    }
    return output;
}