Coverage Report - org.crosswire.jsword.book.sword.ZLDBackend
 
Classes in this File Line Coverage Branch Coverage Complexity
ZLDBackend
0%
0/90
0%
0/20
4.4
 
 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, 2005 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.book.sword;
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.io.ObjectInputStream;
 24  
 
 25  
 import org.crosswire.common.compress.CompressorType;
 26  
 import org.crosswire.jsword.book.BookException;
 27  
 import org.crosswire.jsword.book.sword.state.OpenFileStateManager;
 28  
 import org.crosswire.jsword.book.sword.state.RawLDBackendState;
 29  
 import org.crosswire.jsword.book.sword.state.ZLDBackendState;
 30  
 import org.slf4j.Logger;
 31  
 import org.slf4j.LoggerFactory;
 32  
 
 33  
 /**
 34  
  * An extension of RawLDBackend to read Z format files.
 35  
  * 
 36  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 37  
  * @author Joe Walker
 38  
  * @author DM Smith
 39  
  */
 40  0
 public class ZLDBackend extends RawLDBackend<ZLDBackendState> {
 41  
     /**
 42  
      * Simple ctor
 43  
      * @param sbmd 
 44  
      */
 45  
     public ZLDBackend(SwordBookMetaData sbmd) {
 46  0
         super(sbmd, 4);
 47  0
     }
 48  
 
 49  
     /* (non-Javadoc)
 50  
      * @see org.crosswire.jsword.book.sword.RawLDBackend#initState()
 51  
      */
 52  
     @Override
 53  
     public ZLDBackendState initState() throws BookException {
 54  0
         return OpenFileStateManager.instance().getZLDBackendState(getBookMetaData());
 55  
     }
 56  
 
 57  
     /* (non-Javadoc)
 58  
      * @see org.crosswire.jsword.book.sword.RawLDBackend#getEntry(org.crosswire.jsword.book.sword.state.RawLDBackendState, org.crosswire.jsword.book.sword.DataEntry)
 59  
      */
 60  
     @Override
 61  
     protected DataEntry getEntry(RawLDBackendState fileState, DataEntry entry) {
 62  0
         ZLDBackendState state = null;
 63  0
         if (fileState instanceof ZLDBackendState) {
 64  0
             state = (ZLDBackendState) fileState;
 65  
         } else {
 66  
             //something went terribly wrong
 67  0
             log.error("Backend State was not of type ZLDBackendState. Ignoring this entry and exiting.");
 68  0
             return new DataEntry(entry.getName(), new byte[0], entry.getCharset());
 69  
         }
 70  
 
 71  0
         DataIndex blockIndex = entry.getBlockIndex();
 72  0
         long blockNum = blockIndex.getOffset();
 73  0
         int blockEntry = blockIndex.getSize();
 74  
 
 75  
         // Can we get the data from the cache
 76  0
         byte[] uncompressed = null;
 77  0
         if (blockNum == state.getLastBlockNum()) {
 78  0
             uncompressed = state.getLastUncompressed();
 79  
         } else {
 80  
             byte[] temp;
 81  
             try {
 82  0
                 temp = SwordUtil.readRAF(state.getZdxRaf(), blockNum * ZDX_ENTRY_SIZE, ZDX_ENTRY_SIZE);
 83  0
                 if (temp == null || temp.length == 0) {
 84  0
                     return new DataEntry(entry.getName(), new byte[0], entry.getCharset());
 85  
                 }
 86  
 
 87  0
                 int blockStart = SwordUtil.decodeLittleEndian32(temp, 0);
 88  0
                 int blockSize = SwordUtil.decodeLittleEndian32(temp, 4);
 89  
 
 90  0
                 temp = SwordUtil.readRAF(state.getZdtRaf(), blockStart, blockSize);
 91  
 
 92  0
                 decipher(temp);
 93  
 
 94  0
                 String compressType = getBookMetaData().getProperty(SwordBookMetaData.KEY_COMPRESS_TYPE);
 95  0
                 uncompressed = CompressorType.fromString(compressType).getCompressor(temp).uncompress().toByteArray();
 96  
 
 97  
                 // cache the uncompressed data for next time
 98  0
                 state.setLastBlockNum(blockNum);
 99  0
                 state.setLastUncompressed(uncompressed);
 100  0
             } catch (IOException e) {
 101  0
                 return new DataEntry(entry.getName(), new byte[0], entry.getCharset());
 102  0
             }
 103  
         }
 104  
 
 105  
         // get the "entry" from this block.
 106  0
         int entryCount = SwordUtil.decodeLittleEndian32(uncompressed, 0);
 107  0
         if (blockEntry >= entryCount) {
 108  0
             return new DataEntry(entry.getName(), new byte[0], entry.getCharset());
 109  
         }
 110  
 
 111  0
         int entryOffset = BLOCK_ENTRY_COUNT + (BLOCK_ENTRY_SIZE * blockEntry);
 112  0
         int entryStart = SwordUtil.decodeLittleEndian32(uncompressed, entryOffset);
 113  0
         int entrySize = SwordUtil.decodeLittleEndian32(uncompressed, entryOffset + 4);
 114  
         // Note: the actual entry is '\0' terminated
 115  0
         int nullTerminator = SwordUtil.findByte(uncompressed, entryStart, (byte) 0x00);
 116  0
         if (nullTerminator - entryStart + 1 == entrySize) {
 117  0
             entrySize -= 1;
 118  
         }
 119  0
         byte[] entryBytes = new byte[entrySize];
 120  0
         System.arraycopy(uncompressed, entryStart, entryBytes, 0, entrySize);
 121  0
         DataEntry finalEntry = new DataEntry(entry.getName(), entryBytes, getBookMetaData().getBookCharset());
 122  
 
 123  0
         return finalEntry;
 124  
     }
 125  
 
 126  
     /**
 127  
      * Serialization support.
 128  
      * 
 129  
      * @param is
 130  
      * @throws IOException
 131  
      * @throws ClassNotFoundException
 132  
      */
 133  
     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
 134  0
         is.defaultReadObject();
 135  0
     }
 136  
     /** 
 137  
      * Experimental code.
 138  
      */
 139  
     @Override
 140  
     public void dumpIdxRaf() {
 141  0
         RawLDBackendState state = null;
 142  0
         long end = -1;
 143  
         try {
 144  0
             state = initState();
 145  0
             end = getCardinality();
 146  0
             StringBuilder buf = new StringBuilder();
 147  0
             System.out.println("index\toffset\tsize\tkey\tvalue");
 148  0
             for (long i = 0; i < end; ++i) {
 149  0
                 DataIndex index = getIndex(state, i);
 150  0
                 int offset = index.getOffset();
 151  0
                 int size   = index.getSize();
 152  0
                 buf.setLength(0);
 153  0
                 buf.append(i);
 154  0
                 buf.append('\t');
 155  0
                 buf.append(offset);
 156  0
                 buf.append('\t');
 157  0
                 buf.append(size);
 158  0
                 if (size > 0) {
 159  
                     // Now read the data file for this key using the offset and size
 160  0
                     byte[] data = SwordUtil.readRAF(state.getDatRaf(), offset, size);
 161  0
                     DataEntry blockEntry = new DataEntry(Long.toString(i), data, getBookMetaData().getBookCharset());
 162  0
                     DataIndex block = blockEntry.getBlockIndex();
 163  0
                     DataEntry dataEntry = getEntry(state, blockEntry);
 164  0
                     String key = blockEntry.getKey();
 165  0
                     buf.append('\t');
 166  0
                     buf.append(key);
 167  0
                     buf.append('\t');
 168  0
                     buf.append(block.getOffset());
 169  0
                     buf.append('\t');
 170  0
                     buf.append(block.getSize());
 171  
                     String raw;
 172  0
                     buf.append('\t');
 173  0
                     if (dataEntry.isLinkEntry()) {
 174  0
                         raw = dataEntry.getLinkTarget();
 175  0
                         buf.append("Linked to: ").append(raw.replace('\n', ' '));
 176  
                     } else {
 177  0
                         raw = getRawText(dataEntry);
 178  0
                         if (raw.length() > 43) {
 179  0
                             buf.append(raw.substring(0, 40).replace('\n', ' '));
 180  0
                             buf.append("...");
 181  
                         } else {
 182  0
                             buf.append(raw);
 183  
                         }
 184  
                     }
 185  
                 }
 186  0
                 System.out.println(buf.toString());
 187  
             }
 188  0
         } catch (IOException e) {
 189  
             // TODO Auto-generated catch block
 190  0
             e.printStackTrace();
 191  0
         } catch (BookException e) {
 192  
             // TODO Auto-generated catch block
 193  0
             e.printStackTrace();
 194  
         } finally {
 195  0
             OpenFileStateManager.instance().release(state);
 196  0
         }
 197  0
     }
 198  
 
 199  
     private static final int ZDX_ENTRY_SIZE = 8;
 200  
     private static final int BLOCK_ENTRY_COUNT = 4;
 201  
     private static final int BLOCK_ENTRY_SIZE = 8;
 202  
 
 203  
     /**
 204  
      * The log stream
 205  
      */
 206  0
     private static final Logger log = LoggerFactory.getLogger(ZLDBackend.class);
 207  
     private static final long serialVersionUID = 3536098410391064446L;
 208  
 }