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 com.imagero.uio.impl.RandomAccessFileX;
035    import com.imagero.uio.io.IOutils;
036    
037    import java.io.RandomAccessFile;
038    import java.io.File;
039    import java.io.IOException;
040    import java.io.EOFException;
041    
042    /**
043     * Date: 05.01.2008
044     *
045     * @author Andrey Kuznetsov
046     */
047    public class RandomAccessFileContent extends Content {
048        private RandomAccessFile raf;
049    
050        public RandomAccessFileContent(File f) throws IOException {
051            this(f, getMode(f));
052        }
053    
054        public RandomAccessFileContent(File f, String mode) throws IOException {
055            this(new RandomAccessFileX(f, mode));
056        }
057    
058        static String getMode(File f) {
059            if (!f.exists() || f.canWrite()) {
060                return "rw";
061            } else {
062                return "r";
063            }
064        }
065    
066        public RandomAccessFileContent(RandomAccessFile raf) {
067            this.raf = raf;
068        }
069    
070        public int load(long offset, int bpos, byte[] b) throws IOException {
071            long max = raf.length() - offset;
072            int len = (int) Math.min(max, b.length - bpos);
073            if (len > 0) {
074                raf.seek(offset);
075                raf.readFully(b, bpos, len);
076                return len;
077            }
078            throw new EOFException();
079    //            return 0;
080        }
081    
082        public boolean canReload() {
083            return true;
084        }
085    
086        public void close() {
087            IOutils.closeStream(raf);
088        }
089    
090        public boolean writable() {
091            return true;
092        }
093    
094        public void save(long offset, int bpos, byte[] buffer, int length) throws IOException {
095            raf.seek(offset);
096            try {
097                raf.write(buffer, bpos, length);
098            } catch (IndexOutOfBoundsException ex) {
099                ex.printStackTrace();
100            }
101        }
102    
103        public long length() throws IOException {
104            return raf.length();
105        }
106    
107        protected void finalize() throws Throwable {
108            super.finalize();
109            raf = null;
110        }
111    }