001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    package com.imagero.uio.bio.content;
033    
034    import java.io.IOException;
035    
036    /**
037     *
038     * @author Andrey Kuznetsov
039     */
040    public abstract class Content {
041    
042        /**
043         * Load stream content to specified buffer
044         * @param offset stream offset
045         * @param buffer byte array
046         * @return how much bytes were loaded
047         * @throws java.io.IOException
048         */
049        public final int load(long offset, byte[] buffer) throws IOException {
050            return load(offset, 0, buffer);
051        }
052    
053        /**
054         * Load stream content to specified buffer
055         * @param offset stream offset
056         * @param bpos buffer position
057         * @param buffer byte array
058         * @return how much bytes were loaded
059         * @throws java.io.IOException
060         */
061        public abstract int load(long offset, int bpos, byte[] buffer) throws IOException;
062    
063        /**
064         * Save buffer content to stream.
065         * Not always supported.
066         * @param offset stream offset
067         * @param bpos buffer position
068         * @param buffer byte array
069         * @param length how much bytes should be saved
070         * @throws java.io.IOException
071         */
072        public abstract void save(long offset, int bpos, byte[] buffer, int length) throws IOException;
073    
074        /**
075         * Get stream length. Not always known.
076         * @return
077         * @throws java.io.IOException
078         */
079        public abstract long length() throws IOException;
080    
081        /**
082         * close stream
083         */
084        public abstract void close();
085    
086        /**
087         * Determine if data may be reloaded or not.
088         * For example: data from InputStream cannot be reloaded,
089         * however if content uses file or memory based cache then it is possible to reload data.
090         * @return true if data can be reloaded.
091         */
092        public abstract boolean canReload();
093    
094        public abstract boolean writable();
095    
096        protected void finalize() throws Throwable {
097            super.finalize();
098            close();
099        }
100    
101    
102    }