001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.loader.data;
018
019import java.io.EOFException;
020import java.io.IOException;
021import java.io.InputStream;
022
023/**
024 * Interface that provides read-only random access to some underlying data.
025 * Implementations must allow concurrent reads in a thread-safe manner.
026 *
027 * @author Phillip Webb
028 */
029public interface RandomAccessData {
030
031        /**
032         * Returns an {@link InputStream} that can be used to read the underlying data. The
033         * caller is responsible close the underlying stream.
034         * @return a new input stream that can be used to read the underlying data.
035         * @throws IOException if the stream cannot be opened
036         */
037        InputStream getInputStream() throws IOException;
038
039        /**
040         * Returns a new {@link RandomAccessData} for a specific subsection of this data.
041         * @param offset the offset of the subsection
042         * @param length the length of the subsection
043         * @return the subsection data
044         */
045        RandomAccessData getSubsection(long offset, long length);
046
047        /**
048         * Reads all the data and returns it as a byte array.
049         * @return the data
050         * @throws IOException if the data cannot be read
051         */
052        byte[] read() throws IOException;
053
054        /**
055         * Reads the {@code length} bytes of data starting at the given {@code offset}.
056         * @param offset the offset from which data should be read
057         * @param length the number of bytes to be read
058         * @return the data
059         * @throws IOException if the data cannot be read
060         * @throws IndexOutOfBoundsException if offset is beyond the end of the file or
061         * subsection
062         * @throws EOFException if offset plus length is greater than the length of the file
063         * or subsection
064         */
065        byte[] read(long offset, long length) throws IOException;
066
067        /**
068         * Returns the size of the data.
069         * @return the size
070         */
071        long getSize();
072
073}