Coverage Report - org.crosswire.jsword.book.sword.state.ZVerseBackendState
 
Classes in this File Line Coverage Branch Coverage Complexity
ZVerseBackendState
0%
0/61
0%
0/14
1.636
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2013 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.book.sword.state;
 21  
 
 22  
 import java.io.File;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.io.RandomAccessFile;
 25  
 import java.net.URI;
 26  
 
 27  
 import org.crosswire.common.util.FileUtil;
 28  
 import org.crosswire.common.util.IOUtil;
 29  
 import org.crosswire.common.util.NetUtil;
 30  
 import org.crosswire.jsword.book.BookException;
 31  
 import org.crosswire.jsword.book.BookMetaData;
 32  
 import org.crosswire.jsword.book.sword.BlockType;
 33  
 import org.crosswire.jsword.book.sword.SwordConstants;
 34  
 import org.crosswire.jsword.book.sword.SwordUtil;
 35  
 import org.crosswire.jsword.versification.Testament;
 36  
 import org.slf4j.Logger;
 37  
 import org.slf4j.LoggerFactory;
 38  
 
 39  
 /**
 40  
  * Stores the random access files required for processing the passage request.
 41  
  * 
 42  
  * The caller is required to close to correctly free resources and avoid File
 43  
  * pointer leaks.
 44  
  * 
 45  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 46  
  * @author DM Smith
 47  
  */
 48  0
 public class ZVerseBackendState extends AbstractOpenFileState {
 49  
     /**
 50  
      * This is default package access for forcing the use of the
 51  
      * OpenFileStateManager to manage the creation. Not doing so may result in
 52  
      * new instances of OpenFileState being created for no reason, and as a
 53  
      * result, if they are released to the OpenFileStateManager by mistake this
 54  
      * would result in leakage
 55  
      * 
 56  
      * @param bookMetaData the appropriate metadata for the book
 57  
      */
 58  
     ZVerseBackendState(BookMetaData bookMetaData, BlockType blockType) throws BookException {
 59  0
         super(bookMetaData);
 60  0
         URI path = SwordUtil.getExpandedDataPath(bookMetaData);
 61  0
         String otAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_OT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath();
 62  0
         File otIdxFile = new File(otAllButLast + SUFFIX_INDEX);
 63  0
         File otTextFile = new File(otAllButLast + SUFFIX_TEXT);
 64  0
         File otCompFile = new File(otAllButLast + SUFFIX_COMP);
 65  
 
 66  0
         String ntAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_NT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath();
 67  0
         File ntIdxFile = new File(ntAllButLast + SUFFIX_INDEX);
 68  0
         File ntTextFile = new File(ntAllButLast + SUFFIX_TEXT);
 69  0
         File ntCompFile = new File(ntAllButLast + SUFFIX_COMP);
 70  
 
 71  
         // check whether exists to swallow any exception as before
 72  0
         if (otIdxFile.canRead()) {
 73  
             try {
 74  0
                 otCompRaf = new RandomAccessFile(otIdxFile, FileUtil.MODE_READ);
 75  0
                 otTextRaf = new RandomAccessFile(otTextFile, FileUtil.MODE_READ);
 76  0
                 otIdxRaf = new RandomAccessFile(otCompFile, FileUtil.MODE_READ);
 77  0
             } catch (FileNotFoundException ex) {
 78  
                 //failed to open the files, so close them now
 79  0
                 IOUtil.close(otCompRaf);
 80  0
                 IOUtil.close(otTextRaf);
 81  0
                 IOUtil.close(otIdxRaf);
 82  
 
 83  0
                 assert false : ex;
 84  0
                 LOGGER.error("Could not open OT", ex);
 85  0
             }
 86  
         }
 87  
 
 88  
         // why do swallow the exception and log. Can Books have one testament
 89  
         // without the other.
 90  0
         if (ntIdxFile.canRead()) {
 91  
             try {
 92  0
                 ntCompRaf = new RandomAccessFile(ntIdxFile, FileUtil.MODE_READ);
 93  0
                 ntTextRaf = new RandomAccessFile(ntTextFile, FileUtil.MODE_READ);
 94  0
                 ntIdxRaf = new RandomAccessFile(ntCompFile, FileUtil.MODE_READ);
 95  0
             } catch (FileNotFoundException ex) {
 96  
                 //failed to open the files, so close them now
 97  0
                 IOUtil.close(ntCompRaf);
 98  0
                 IOUtil.close(ntTextRaf);
 99  0
                 IOUtil.close(ntIdxRaf);
 100  
 
 101  0
                 assert false : ex;
 102  0
                 LOGGER.error("Could not open OT", ex);
 103  0
             }
 104  
         }
 105  0
     }
 106  
 
 107  
     public void releaseResources() {
 108  0
         IOUtil.close(ntCompRaf);
 109  0
         IOUtil.close(ntTextRaf);
 110  0
         IOUtil.close(ntIdxRaf);
 111  0
         IOUtil.close(otCompRaf);
 112  0
         IOUtil.close(otTextRaf);
 113  0
         IOUtil.close(otIdxRaf);
 114  0
         ntCompRaf = null;
 115  0
         ntTextRaf = null;
 116  0
         ntIdxRaf = null;
 117  0
         otCompRaf = null;
 118  0
         otTextRaf = null;
 119  0
         otIdxRaf = null;
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Get the compression file for the given testament.
 124  
      * 
 125  
      * @param testament the testament for the index
 126  
      * @return the index for the testament
 127  
      */
 128  
     public RandomAccessFile getCompRaf(Testament testament) {
 129  0
         return testament == Testament.NEW ? ntCompRaf : otCompRaf;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Get the text file for the given testament.
 134  
      * 
 135  
      * @param testament the testament for the index
 136  
      * @return the index for the testament
 137  
      */
 138  
     public RandomAccessFile getTextRaf(Testament testament) {
 139  0
         return testament == Testament.NEW ? ntTextRaf : otTextRaf;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Get the index file for the given testament.
 144  
      * 
 145  
      * @param testament the testament for the index
 146  
      * @return the index for the testament
 147  
      */
 148  
     public RandomAccessFile getIdxRaf(Testament testament) {
 149  0
         return testament == Testament.NEW ? ntIdxRaf : otIdxRaf;
 150  
     }
 151  
 
 152  
     /**
 153  
      * @return the lastTestament
 154  
      */
 155  
     public Testament getLastTestament() {
 156  0
         return lastTestament;
 157  
     }
 158  
 
 159  
     /**
 160  
      * @return the lastBlockNum
 161  
      */
 162  
     public long getLastBlockNum() {
 163  0
         return lastBlockNum;
 164  
     }
 165  
 
 166  
     /**
 167  
      * @return the lastUncompressed
 168  
      */
 169  
     public byte[] getLastUncompressed() {
 170  0
         return lastUncompressed;
 171  
     }
 172  
 
 173  
     /**
 174  
      * @param lastTestament
 175  
      *            the lastTestament to set
 176  
      */
 177  
     public void setLastTestament(Testament lastTestament) {
 178  0
         this.lastTestament = lastTestament;
 179  0
     }
 180  
 
 181  
     /**
 182  
      * @param lastBlockNum
 183  
      *            the lastBlockNum to set
 184  
      */
 185  
     public void setLastBlockNum(long lastBlockNum) {
 186  0
         this.lastBlockNum = lastBlockNum;
 187  0
     }
 188  
 
 189  
     /**
 190  
      * @param lastUncompressed
 191  
      *            the lastUncompressed to set
 192  
      */
 193  
     public void setLastUncompressed(byte[] lastUncompressed) {
 194  0
         this.lastUncompressed = lastUncompressed;
 195  0
     }
 196  
 
 197  
     private static final String SUFFIX_COMP = "v";
 198  
     private static final String SUFFIX_INDEX = "s";
 199  
     private static final String SUFFIX_PART1 = "z";
 200  
     private static final String SUFFIX_TEXT = "z";
 201  
 
 202  
     /**
 203  
      * The compressed random access files
 204  
      */
 205  
     private RandomAccessFile otCompRaf;
 206  
     private RandomAccessFile ntCompRaf;
 207  
 
 208  
     /**
 209  
      * The data random access files
 210  
      */
 211  
     private RandomAccessFile otTextRaf;
 212  
     private RandomAccessFile ntTextRaf;
 213  
 
 214  
     /**
 215  
      * The index random access files
 216  
      */
 217  
     private RandomAccessFile otIdxRaf;
 218  
     private RandomAccessFile ntIdxRaf;
 219  
     private Testament lastTestament;
 220  0
     private long lastBlockNum = -1;
 221  
     private byte[] lastUncompressed;
 222  
 
 223  
     /**
 224  
      * The log stream
 225  
      */
 226  0
     private static final Logger LOGGER = LoggerFactory.getLogger(ZVerseBackendState.class);
 227  
 }