001/*
002 * Copyright 2012-2015 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.IOException;
020import java.io.InputStream;
021
022/**
023 * Interface that provides read-only random access to some underlying data.
024 * Implementations must allow concurrent reads in a thread-safe manner.
025 *
026 * @author Phillip Webb
027 */
028public interface RandomAccessData {
029
030        /**
031         * Returns an {@link InputStream} that can be used to read the underlying data. The
032         * caller is responsible close the underlying stream.
033         * @param access hint indicating how the underlying data should be accessed
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(ResourceAccess access) 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         * Returns the size of the data.
049         * @return the size
050         */
051        long getSize();
052
053        /**
054         * Lock modes for accessing the underlying resource.
055         */
056        enum ResourceAccess {
057
058                /**
059                 * Obtain access to the underlying resource once and keep it until the stream is
060                 * closed.
061                 */
062                ONCE,
063
064                /**
065                 * Obtain access to the underlying resource on each read, releasing it when done.
066                 */
067                PER_READ
068
069        }
070
071}