New code style. No functional changes.

This commit is contained in:
Harald Kuhr
2011-02-17 12:36:40 +01:00
parent 191643a36c
commit 43cc440e67
60 changed files with 1671 additions and 1665 deletions
@@ -60,28 +60,28 @@ public final class CompoundDocument {
};
public static final int HEADER_SIZE = 512;
private final DataInput mInput;
private final DataInput input;
private UUID mUID;
private UUID uUID;
private int mSectorSize;
private int mShortSectorSize;
private int sectorSize;
private int shortSectorSize;
private int mDirectorySId;
private int directorySId;
private int mMinStreamSize;
private int minStreamSize;
private int mShortSATSID;
private int mShortSATSize;
private int shortSATSId;
private int shortSATSize;
// Master Sector Allocation Table
private int[] mMasterSAT;
private int[] mSAT;
private int[] mShortSAT;
private int[] masterSAT;
private int[] SAT;
private int[] shortSAT;
private Entry mRootEntry;
private SIdChain mShortStreamSIdChain;
private SIdChain mDirectorySIdChain;
private Entry rootEntry;
private SIdChain shortStreamSIdChain;
private SIdChain directorySIdChain;
private static final int END_OF_CHAIN_SID = -2;
private static final int FREE_SID = -1;
@@ -97,7 +97,7 @@ public final class CompoundDocument {
* @throws IOException if an I/O exception occurs while reading the header
*/
public CompoundDocument(final File pFile) throws IOException {
mInput = new LittleEndianRandomAccessFile(FileUtil.resolve(pFile), "r");
input = new LittleEndianRandomAccessFile(FileUtil.resolve(pFile), "r");
// TODO: Might be better to read header on first read operation?!
// OTOH: It's also good to be fail-fast, so at least we should make
@@ -118,7 +118,7 @@ public final class CompoundDocument {
// For testing only, consider exposing later
CompoundDocument(final SeekableInputStream pInput) throws IOException {
mInput = new SeekableLittleEndianDataInputStream(pInput);
input = new SeekableLittleEndianDataInputStream(pInput);
// TODO: Might be better to read header on first read operation?!
// OTOH: It's also good to be fail-fast, so at least we should make
@@ -134,7 +134,7 @@ public final class CompoundDocument {
* @throws IOException if an I/O exception occurs while reading the header
*/
public CompoundDocument(final ImageInputStream pInput) throws IOException {
mInput = pInput;
input = pInput;
// TODO: Might be better to read header on first read operation?!
// OTOH: It's also good to be fail-fast, so at least we should make
@@ -210,74 +210,76 @@ public final class CompoundDocument {
}
private void readHeader() throws IOException {
if (mMasterSAT != null) {
if (masterSAT != null) {
return;
}
if (!canRead(mInput, false)) {
if (!canRead(input, false)) {
throw new CorruptDocumentException("Not an OLE 2 Compound Document");
}
// UID (seems to be all 0s)
mUID = new UUID(mInput.readLong(), mInput.readLong());
uUID = new UUID(input.readLong(), input.readLong());
/*int version = */mInput.readUnsignedShort();
/*int version = */
input.readUnsignedShort();
//System.out.println("version: " + version);
/*int revision = */mInput.readUnsignedShort();
/*int revision = */
input.readUnsignedShort();
//System.out.println("revision: " + revision);
int byteOrder = mInput.readUnsignedShort();
int byteOrder = input.readUnsignedShort();
if (byteOrder != 0xfffe) {
// Reversed, as I'm allready reading little-endian
throw new CorruptDocumentException("Cannot read big endian OLE 2 Compound Documents");
}
mSectorSize = 1 << mInput.readUnsignedShort();
//System.out.println("sectorSize: " + mSectorSize + " bytes");
mShortSectorSize = 1 << mInput.readUnsignedShort();
//System.out.println("shortSectorSize: " + mShortSectorSize + " bytes");
sectorSize = 1 << input.readUnsignedShort();
//System.out.println("sectorSize: " + sectorSize + " bytes");
shortSectorSize = 1 << input.readUnsignedShort();
//System.out.println("shortSectorSize: " + shortSectorSize + " bytes");
// Reserved
if (mInput.skipBytes(10) != 10) {
if (input.skipBytes(10) != 10) {
throw new CorruptDocumentException();
}
int SATSize = mInput.readInt();
int SATSize = input.readInt();
//System.out.println("normalSATSize: " + mSATSize);
mDirectorySId = mInput.readInt();
//System.out.println("directorySId: " + mDirectorySId);
directorySId = input.readInt();
//System.out.println("directorySId: " + directorySId);
// Reserved
if (mInput.skipBytes(4) != 4) {
if (input.skipBytes(4) != 4) {
throw new CorruptDocumentException();
}
mMinStreamSize = mInput.readInt();
//System.out.println("minStreamSize: " + mMinStreamSize + " bytes");
minStreamSize = input.readInt();
//System.out.println("minStreamSize: " + minStreamSize + " bytes");
mShortSATSID = mInput.readInt();
//System.out.println("shortSATSID: " + mShortSATSID);
mShortSATSize = mInput.readInt();
//System.out.println("shortSATSize: " + mShortSATSize);
int masterSATSId = mInput.readInt();
shortSATSId = input.readInt();
//System.out.println("shortSATSId: " + shortSATSId);
shortSATSize = input.readInt();
//System.out.println("shortSATSize: " + shortSATSize);
int masterSATSId = input.readInt();
//System.out.println("masterSATSId: " + mMasterSATSID);
int masterSATSize = mInput.readInt();
int masterSATSize = input.readInt();
//System.out.println("masterSATSize: " + mMasterSATSize);
// Read masterSAT: 436 bytes, containing up to 109 SIDs
//System.out.println("MSAT:");
mMasterSAT = new int[SATSize];
masterSAT = new int[SATSize];
final int headerSIds = Math.min(SATSize, 109);
for (int i = 0; i < headerSIds; i++) {
mMasterSAT[i] = mInput.readInt();
//System.out.println("\tSID(" + i + "): " + mMasterSAT[i]);
masterSAT[i] = input.readInt();
//System.out.println("\tSID(" + i + "): " + masterSAT[i]);
}
if (masterSATSId == END_OF_CHAIN_SID) {
// End of chain
int freeSIdLength = 436 - (SATSize * 4);
if (mInput.skipBytes(freeSIdLength) != freeSIdLength) {
if (input.skipBytes(freeSIdLength) != freeSIdLength) {
throw new CorruptDocumentException();
}
}
@@ -288,17 +290,17 @@ public final class CompoundDocument {
int index = headerSIds;
for (int i = 0; i < masterSATSize; i++) {
for (int j = 0; j < 127; j++) {
int sid = mInput.readInt();
int sid = input.readInt();
switch (sid) {
case FREE_SID:// Free
break;
default:
mMasterSAT[index++] = sid;
masterSAT[index++] = sid;
break;
}
}
int next = mInput.readInt();
int next = input.readInt();
if (next == END_OF_CHAIN_SID) {// End of chain
break;
}
@@ -309,37 +311,37 @@ public final class CompoundDocument {
}
private void readSAT() throws IOException {
if (mSAT != null) {
if (SAT != null) {
return;
}
final int intsPerSector = mSectorSize / 4;
final int intsPerSector = sectorSize / 4;
// Read the Sector Allocation Table
mSAT = new int[mMasterSAT.length * intsPerSector];
SAT = new int[masterSAT.length * intsPerSector];
for (int i = 0; i < mMasterSAT.length; i++) {
seekToSId(mMasterSAT[i], FREE_SID);
for (int i = 0; i < masterSAT.length; i++) {
seekToSId(masterSAT[i], FREE_SID);
for (int j = 0; j < intsPerSector; j++) {
int nextSID = mInput.readInt();
int nextSID = input.readInt();
int index = (j + (i * intsPerSector));
mSAT[index] = nextSID;
SAT[index] = nextSID;
}
}
// Read the short-stream Sector Allocation Table
SIdChain chain = getSIdChain(mShortSATSID, FREE_SID);
mShortSAT = new int[mShortSATSize * intsPerSector];
for (int i = 0; i < mShortSATSize; i++) {
SIdChain chain = getSIdChain(shortSATSId, FREE_SID);
shortSAT = new int[shortSATSize * intsPerSector];
for (int i = 0; i < shortSATSize; i++) {
seekToSId(chain.get(i), FREE_SID);
for (int j = 0; j < intsPerSector; j++) {
int nextSID = mInput.readInt();
int nextSID = input.readInt();
int index = (j + (i * intsPerSector));
mShortSAT[index] = nextSID;
shortSAT[index] = nextSID;
}
}
}
@@ -355,7 +357,7 @@ public final class CompoundDocument {
private SIdChain getSIdChain(final int pSId, final long pStreamSize) throws IOException {
SIdChain chain = new SIdChain();
int[] sat = isShortStream(pStreamSize) ? mShortSAT : mSAT;
int[] sat = isShortStream(pStreamSize) ? shortSAT : SAT;
int sid = pSId;
while (sid != END_OF_CHAIN_SID && sid != FREE_SID) {
@@ -367,7 +369,7 @@ public final class CompoundDocument {
}
private boolean isShortStream(final long pStreamSize) {
return pStreamSize != FREE_SID && pStreamSize < mMinStreamSize;
return pStreamSize != FREE_SID && pStreamSize < minStreamSize;
}
/**
@@ -383,56 +385,56 @@ public final class CompoundDocument {
if (isShortStream(pStreamSize)) {
// The short-stream is not continouos...
Entry root = getRootEntry();
if (mShortStreamSIdChain == null) {
mShortStreamSIdChain = getSIdChain(root.startSId, root.streamSize);
if (shortStreamSIdChain == null) {
shortStreamSIdChain = getSIdChain(root.startSId, root.streamSize);
}
int shortPerStd = mSectorSize / mShortSectorSize;
int shortPerStd = sectorSize / shortSectorSize;
int offset = pSId / shortPerStd;
int shortOffset = pSId - (offset * shortPerStd);
pos = HEADER_SIZE
+ (mShortStreamSIdChain.get(offset) * (long) mSectorSize)
+ (shortOffset * (long) mShortSectorSize);
+ (shortStreamSIdChain.get(offset) * (long) sectorSize)
+ (shortOffset * (long) shortSectorSize);
}
else {
pos = HEADER_SIZE + pSId * (long) mSectorSize;
pos = HEADER_SIZE + pSId * (long) sectorSize;
}
if (mInput instanceof LittleEndianRandomAccessFile) {
((LittleEndianRandomAccessFile) mInput).seek(pos);
if (input instanceof LittleEndianRandomAccessFile) {
((LittleEndianRandomAccessFile) input).seek(pos);
}
else if (mInput instanceof ImageInputStream) {
((ImageInputStream) mInput).seek(pos);
else if (input instanceof ImageInputStream) {
((ImageInputStream) input).seek(pos);
}
else {
((SeekableLittleEndianDataInputStream) mInput).seek(pos);
((SeekableLittleEndianDataInputStream) input).seek(pos);
}
}
private void seekToDId(final int pDId) throws IOException {
if (mDirectorySIdChain == null) {
mDirectorySIdChain = getSIdChain(mDirectorySId, FREE_SID);
if (directorySIdChain == null) {
directorySIdChain = getSIdChain(directorySId, FREE_SID);
}
int dIdsPerSId = mSectorSize / Entry.LENGTH;
int dIdsPerSId = sectorSize / Entry.LENGTH;
int sIdOffset = pDId / dIdsPerSId;
int dIdOffset = pDId - (sIdOffset * dIdsPerSId);
int sId = mDirectorySIdChain.get(sIdOffset);
int sId = directorySIdChain.get(sIdOffset);
seekToSId(sId, FREE_SID);
if (mInput instanceof LittleEndianRandomAccessFile) {
LittleEndianRandomAccessFile input = (LittleEndianRandomAccessFile) mInput;
if (input instanceof LittleEndianRandomAccessFile) {
LittleEndianRandomAccessFile input = (LittleEndianRandomAccessFile) this.input;
input.seek(input.getFilePointer() + dIdOffset * Entry.LENGTH);
}
else if (mInput instanceof ImageInputStream) {
ImageInputStream input = (ImageInputStream) mInput;
else if (input instanceof ImageInputStream) {
ImageInputStream input = (ImageInputStream) this.input;
input.seek(input.getStreamPosition() + dIdOffset * Entry.LENGTH);
}
else {
SeekableLittleEndianDataInputStream input = (SeekableLittleEndianDataInputStream) mInput;
SeekableLittleEndianDataInputStream input = (SeekableLittleEndianDataInputStream) this.input;
input.seek(input.getStreamPosition() + dIdOffset * Entry.LENGTH);
}
}
@@ -442,7 +444,7 @@ public final class CompoundDocument {
// TODO: Detach? Means, we have to copy to a byte buffer, or keep track of
// positions, and seek back and forth (would be cool, but difficult)..
int sectorSize = pStreamSize < mMinStreamSize ? mShortSectorSize : mSectorSize;
int sectorSize = pStreamSize < minStreamSize ? shortSectorSize : this.sectorSize;
return new Stream(chain, pStreamSize, sectorSize, this);
}
@@ -453,7 +455,7 @@ public final class CompoundDocument {
byte[] bytes = new byte[Entry.LENGTH];
seekToDId(pDirectoryId);
mInput.readFully(bytes);
input.readFully(bytes);
return new ByteArrayInputStream(bytes);
}
@@ -462,8 +464,8 @@ public final class CompoundDocument {
Entry entry = Entry.readEntry(new LittleEndianDataInputStream(
getDirectoryStreamForDId(pDirectoryId)
));
entry.mParent = pParent;
entry.mDocument = this;
entry.parent = pParent;
entry.document = this;
return entry;
}
@@ -527,21 +529,21 @@ public final class CompoundDocument {
}
public Entry getRootEntry() throws IOException {
if (mRootEntry == null) {
if (rootEntry == null) {
readSAT();
mRootEntry = getEntry(0, null);
rootEntry = getEntry(0, null);
if (mRootEntry.type != Entry.ROOT_STORAGE) {
throw new CorruptDocumentException("Invalid root storage type: " + mRootEntry.type);
if (rootEntry.type != Entry.ROOT_STORAGE) {
throw new CorruptDocumentException("Invalid root storage type: " + rootEntry.type);
}
}
return mRootEntry;
return rootEntry;
}
// @Override
// public int hashCode() {
// return mUID.hashCode();
// return uUID.hashCode();
// }
//
// @Override
@@ -555,7 +557,7 @@ public final class CompoundDocument {
// }
//
// if (pOther.getClass() == getClass()) {
// return mUID.equals(((CompoundDocument) pOther).mUID);
// return uUID.equals(((CompoundDocument) pOther).uUID);
// }
//
// return false;
@@ -565,7 +567,7 @@ public final class CompoundDocument {
public String toString() {
return String.format(
"%s[uuid: %s, sector size: %d/%d bytes, directory SID: %d, master SAT: %s entries]",
getClass().getSimpleName(), mUID, mSectorSize, mShortSectorSize, mDirectorySId, mMasterSAT.length
getClass().getSimpleName(), uUID, sectorSize, shortSectorSize, directorySId, masterSAT.length
);
}
@@ -638,11 +640,11 @@ public final class CompoundDocument {
private boolean fillBuffer() throws IOException {
if (mNextSectorPos < mChain.length()) {
// TODO: Sync on mDocument.mInput here, and we are completely detached... :-)
// TODO: Sync on document.input here, and we are completely detached... :-)
// TODO: We also need to sync other places...
synchronized (mDocument) {
mDocument.seekToSId(mChain.get(mNextSectorPos), mLength);
mDocument.mInput.readFully(mBuffer);
mDocument.input.readFully(mBuffer);
}
mNextSectorPos++;
@@ -61,9 +61,9 @@ public final class Entry implements Comparable<Entry> {
int startSId;
int streamSize;
CompoundDocument mDocument;
Entry mParent;
SortedSet<Entry> mChildren;
CompoundDocument document;
Entry parent;
SortedSet<Entry> children;
public final static int LENGTH = 128;
@@ -190,7 +190,7 @@ public final class Entry implements Comparable<Entry> {
return null;
}
return mDocument.getInputStreamForSId(startSId, streamSize);
return document.getInputStreamForSId(startSId, streamSize);
}
/**
@@ -248,7 +248,7 @@ public final class Entry implements Comparable<Entry> {
* the root {@code Entry}
*/
public Entry getParentEntry() {
return mParent;
return parent;
}
/**
@@ -266,7 +266,7 @@ public final class Entry implements Comparable<Entry> {
Entry dummy = new Entry();
dummy.name = pName;
dummy.mParent = this;
dummy.parent = this;
SortedSet child = getChildEntries().tailSet(dummy);
return (Entry) child.first();
@@ -279,26 +279,26 @@ public final class Entry implements Comparable<Entry> {
* @throws java.io.IOException if an I/O exception occurs
*/
public SortedSet<Entry> getChildEntries() throws IOException {
if (mChildren == null) {
if (children == null) {
if (isFile() || rootNodeDId == -1) {
mChildren = NO_CHILDREN;
children = NO_CHILDREN;
}
else {
// Start at root node in R/B tree, and raed to the left and right,
// re-build tree, according to the docs
mChildren = mDocument.getEntries(rootNodeDId, this);
children = document.getEntries(rootNodeDId, this);
}
}
return mChildren;
return children;
}
@Override
public String toString() {
return "\"" + name + "\""
+ " (" + (isFile() ? "Document" : (isDirectory() ? "Directory" : "Root"))
+ (mParent != null ? ", parent: \"" + mParent.getName() + "\"" : "")
+ (isFile() ? "" : ", children: " + (mChildren != null ? String.valueOf(mChildren.size()) : "(unknown)"))
+ (parent != null ? ", parent: \"" + parent.getName() + "\"" : "")
+ (isFile() ? "" : ", children: " + (children != null ? String.valueOf(children.size()) : "(unknown)"))
+ ", SId=" + startSId + ", length=" + streamSize + ")";
}
@@ -312,8 +312,8 @@ public final class Entry implements Comparable<Entry> {
}
Entry other = (Entry) pOther;
return name.equals(other.name) && (mParent == other.mParent
|| (mParent != null && mParent.equals(other.mParent)));
return name.equals(other.name) && (parent == other.parent
|| (parent != null && parent.equals(other.parent)));
}
@Override