001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2;
018
019import java.io.Closeable;
020import java.io.DataInput;
021import java.io.DataOutput;
022import java.io.IOException;
023import java.io.InputStream;
024
025/**
026 * Provides random access over content.
027 */
028public interface RandomAccessContent extends DataOutput, DataInput, Closeable {
029
030    /**
031     * Closes this random access file stream and releases any system resources associated with the stream.
032     * <p>
033     * A closed random access file cannot perform input or output operations and cannot be reopened.
034     * </p>
035     * <p>
036     * If this file has an associated channel then the channel is closed as well.
037     * </p>
038     *
039     * @throws IOException if an I/O error occurs.
040     */
041    @Override
042    void close() throws IOException;
043
044    /**
045     * Returns the current offset in this file.
046     *
047     * @return the offset from the beginning of the file, in bytes, at which the next read or write occurs.
048     * @throws IOException if an I/O error occurs.
049     */
050    long getFilePointer() throws IOException;
051
052    /**
053     * Gets the input stream.
054     * <p>
055     * <strong>Notice: If you use {@link #seek(long)} you have to re-get the InputStream</strong>
056     * </p>
057     *
058     * @return the InputStream.
059     * @throws IOException if an I/O error occurs.
060     */
061    InputStream getInputStream() throws IOException;
062
063    /**
064     * Returns the length of this file.
065     *
066     * @return the length of this file, measured in bytes.
067     * @throws IOException if an I/O error occurs.
068     */
069    long length() throws IOException;
070
071    /**
072     * Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
073     * <p>
074     * The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change
075     * the file length. The file length will change only by writing after the offset has been set beyond the end of the
076     * file.
077     * </p>
078     * <p>
079     * <b>Notice: If you use {@link #getInputStream()} you have to re-get the InputStream after calling
080     * {@link #seek(long)}</b>
081     * </p>
082     *
083     * @param pos the offset position, measured in bytes from the beginning of the file, at which to set the file
084     *            pointer.
085     * @throws IOException if {@code pos} is less than {@code 0} or if an I/O error occurs.
086     */
087    void seek(long pos) throws IOException;
088
089    /**
090     * Sets the length of this content.
091     *
092     * <p>
093     * If the {@code newLength} argument is smaller than {@link #length()}, the content is truncated.
094     * </p>
095     *
096     * <p>
097     * If the {@code newLength} argument is greater than {@link #length()}, the content grows with undefined data.
098     * </p>
099     *
100     * @param newLength The desired content length
101     * @throws IOException If an I/O error occurs
102     * @since 2.1
103     */
104    void setLength(long newLength) throws IOException;
105
106}