| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GenBookBackendState |
|
| 2.6666666666666665;2.667 |
| 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.IOException; | |
| 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.Reporter; | |
| 30 | import org.crosswire.jsword.JSMsg; | |
| 31 | import org.crosswire.jsword.book.BookException; | |
| 32 | import org.crosswire.jsword.book.BookMetaData; | |
| 33 | import org.crosswire.jsword.book.sword.SwordUtil; | |
| 34 | import org.slf4j.Logger; | |
| 35 | import org.slf4j.LoggerFactory; | |
| 36 | ||
| 37 | /** | |
| 38 | * Stores the random access files required for processing the passage request | |
| 39 | * | |
| 40 | * The caller is required to close to correctly free resources and avoid File | |
| 41 | * pointer leaks | |
| 42 | * | |
| 43 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
| 44 | * @author DM Smith | |
| 45 | */ | |
| 46 | public class GenBookBackendState extends AbstractOpenFileState { | |
| 47 | /** | |
| 48 | * This is default package access for forcing the use of the | |
| 49 | * OpenFileStateManager to manage the creation. Not doing so may result in | |
| 50 | * new instances of OpenFileState being created for no reason, and as a | |
| 51 | * result, if they are released to the OpenFileStateManager by mistake this | |
| 52 | * would result in leakage | |
| 53 | * | |
| 54 | * @param bookMetaData the appropriate metadata for the book | |
| 55 | */ | |
| 56 | GenBookBackendState(BookMetaData bookMetaData) { | |
| 57 | 0 | super(bookMetaData); |
| 58 | 0 | URI path = null; |
| 59 | try { | |
| 60 | 0 | path = SwordUtil.getExpandedDataPath(bookMetaData); |
| 61 | 0 | } catch (BookException e) { |
| 62 | 0 | Reporter.informUser(this, e); |
| 63 | 0 | return; |
| 64 | 0 | } |
| 65 | ||
| 66 | 0 | bdtFile = new File(path.getPath() + EXTENSION_BDT); |
| 67 | ||
| 68 | 0 | if (!bdtFile.canRead()) { |
| 69 | // TRANSLATOR: Common error condition: The file could not be read. | |
| 70 | // There can be many reasons. | |
| 71 | // {0} is a placeholder for the file. | |
| 72 | 0 | Reporter.informUser(this, new BookException(JSMsg.gettext("Error reading {0}", bdtFile.getAbsolutePath()))); |
| 73 | 0 | return; |
| 74 | } | |
| 75 | ||
| 76 | try { | |
| 77 | 0 | bdtRaf = new RandomAccessFile(bdtFile, FileUtil.MODE_READ); |
| 78 | 0 | } catch (IOException ex) { |
| 79 | //failed to open the files, so close them now | |
| 80 | 0 | IOUtil.close(bdtRaf); |
| 81 | ||
| 82 | 0 | LOGGER.error("failed to open files", ex); |
| 83 | 0 | bdtRaf = null; |
| 84 | 0 | } |
| 85 | 0 | } |
| 86 | ||
| 87 | public void releaseResources() { | |
| 88 | 0 | IOUtil.close(bdtRaf); |
| 89 | 0 | bdtRaf = null; |
| 90 | 0 | } |
| 91 | ||
| 92 | /** | |
| 93 | * @return the bdtRaf | |
| 94 | */ | |
| 95 | public RandomAccessFile getBdtRaf() { | |
| 96 | 0 | return bdtRaf; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Raw GenBook file extensions | |
| 101 | */ | |
| 102 | private static final String EXTENSION_BDT = ".bdt"; | |
| 103 | ||
| 104 | /** | |
| 105 | * The raw data file | |
| 106 | */ | |
| 107 | private File bdtFile; | |
| 108 | ||
| 109 | /** | |
| 110 | * The random access file for the raw data | |
| 111 | */ | |
| 112 | private RandomAccessFile bdtRaf; | |
| 113 | ||
| 114 | /** | |
| 115 | * The log stream | |
| 116 | */ | |
| 117 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(GenBookBackendState.class); |
| 118 | } |