| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.sys; |
| 18 |
|
| 19 |
import uk.me.parabola.log.Logger; |
| 20 |
|
| 21 |
import java.nio.ByteBuffer; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class BlockTable { |
| 40 |
private static final Logger log = Logger.getLogger(BlockTable.class); |
| 41 |
|
| 42 |
|
| 43 |
private static final int BLOCKS_TABLE_START = 0x20; |
| 44 |
private static final int ENTRY_SIZE = 512; |
| 45 |
|
| 46 |
private static final int TABLE_SIZE = (ENTRY_SIZE - BLOCKS_TABLE_START)/2; |
| 47 |
|
| 48 |
|
| 49 |
private int curroff; |
| 50 |
private final List<char[]> blocks; |
| 51 |
private char[] currTable; |
| 52 |
|
| 53 |
BlockTable() { |
| 54 |
blocks = new ArrayList<>(200); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public void writeTable(ByteBuffer buf, int n) { |
| 64 |
char[] cbuf = blocks.get(n); |
| 65 |
log.debug("block with length", cbuf.length); |
| 66 |
for (char c : cbuf) { |
| 67 |
buf.putChar(c); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public void readTable(ByteBuffer buf) { |
| 77 |
buf.position(BLOCKS_TABLE_START); |
| 78 |
buf.limit(ENTRY_SIZE); |
| 79 |
|
| 80 |
char[] cbuf = newTable(); |
| 81 |
for (int i = 0; i < cbuf.length; i++) { |
| 82 |
char c = buf.getChar(); |
| 83 |
cbuf[i] = c; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void addBlock(int n) { |
| 93 |
char[] thisTable = currTable; |
| 94 |
if (curroff >= TABLE_SIZE || currTable == null) |
| 95 |
thisTable = newTable(); |
| 96 |
|
| 97 |
thisTable[curroff++] = (char) n; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public int physFromLogical(int lblock) { |
| 107 |
int blockNum = lblock / TABLE_SIZE; |
| 108 |
int offset = lblock - blockNum * TABLE_SIZE; |
| 109 |
if (blockNum >= blocks.size()) |
| 110 |
return 0xffff; |
| 111 |
|
| 112 |
char[] cbuf = blocks.get(blockNum); |
| 113 |
return cbuf[offset]; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public int getNBlockTables() { |
| 123 |
return blocks.size(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private char[] newTable() { |
| 132 |
char[] table = new char[TABLE_SIZE]; |
| 133 |
Arrays.fill(table, (char) 0xffff); |
| 134 |
|
| 135 |
curroff = 0; |
| 136 |
blocks.add(table); |
| 137 |
currTable = table; |
| 138 |
|
| 139 |
return table; |
| 140 |
} |
| 141 |
} |