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.DataInput;
020import java.io.DataOutput;
021import java.io.IOException;
022import java.io.InputStream;
023
024/**
025 * Provides random access over content.
026 */
027public interface RandomAccessContent extends DataOutput, DataInput
028{
029    /**
030     * Closes this random access file stream and releases any system resources associated with the stream.
031     * <p>
032     * A closed random access file cannot perform input or output operations and cannot be reopened.
033     * </p>
034     * <p>
035     * If this file has an associated channel then the channel is closed as well.
036     * <p>
037     *
038     * @throws IOException
039     *             if an I/O error occurs.
040     */
041    void close() throws IOException;
042
043    /**
044     * Returns the current offset in this file.
045     *
046     * @return the offset from the beginning of the file, in bytes, at which the next read or write occurs.
047     * @throws IOException
048     *             if an I/O error occurs.
049     */
050    long getFilePointer() throws IOException;
051
052    /**
053     * Get the input stream.
054     * <p>
055     * <b>Notice: If you use {@link #seek(long)} you have to re-get the InputStream</b>
056     * </p>
057     *
058     * @return the InputStream.
059     * @throws IOException
060     *             if an I/O error occurs.
061     */
062    InputStream getInputStream() throws IOException;
063
064    /**
065     * Returns the length of this file.
066     *
067     * @return the length of this file, measured in bytes.
068     * @throws IOException
069     *             if an I/O error occurs.
070     */
071    long length() throws IOException;
072
073    /**
074     * Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
075     * <p>
076     * The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change
077     * the file length. The file length will change only by writing after the offset has been set beyond the end of the
078     * file.
079     * </p>
080     * <p>
081     * <b>Notice: If you use {@link #getInputStream()} you have to re-get the InputStream after calling
082     * {@link #seek(long)}</b>
083     * </p>
084     *
085     * @param pos
086     *            the offset position, measured in bytes from the beginning of the file, at which to set the file
087     *            pointer.
088     * @throws IOException
089     *             if {@code pos} is less than {@code 0} or if an I/O error occurs.
090     */
091    void seek(long pos) throws IOException;
092
093    /**
094     * Sets the length of this content.
095     *
096     * <p>
097     * If the the {@code newLength} argument is smaller than {@link #length()}, the content is truncated.
098     * </p>
099     *
100     * <p>
101     * If the the {@code newLength} argument is greater than {@link #length()}, the content grows with undefined data.
102     * </p>
103     *
104     * @param newLength
105     *            The desired content length
106     * @exception IOException
107     *                If an I/O error occurs
108     * @since 2.1
109     */
110    void setLength(long newLength) throws IOException;
111
112}