| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | public class ZLDBackend extends RawLDBackend<ZLDBackendState> { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public ZLDBackend(SwordBookMetaData sbmd) { |
| 46 | 0 | super(sbmd, 4); |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
@Override |
| 53 | |
public ZLDBackendState initState() throws BookException { |
| 54 | 0 | return OpenFileStateManager.instance().getZLDBackendState(getBookMetaData()); |
| 55 | |
} |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException { |
| 134 | 0 | is.defaultReadObject(); |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
|
| 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 | |
|
| 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 | |
|
| 190 | 0 | e.printStackTrace(); |
| 191 | 0 | } catch (BookException e) { |
| 192 | |
|
| 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 | |
|
| 205 | |
|
| 206 | 0 | private static final Logger log = LoggerFactory.getLogger(ZLDBackend.class); |
| 207 | |
private static final long serialVersionUID = 3536098410391064446L; |
| 208 | |
} |