Lines 361-377
Link Here
|
361 |
void initialize(boolean reuseExistingFile) throws IOException { |
361 |
void initialize(boolean reuseExistingFile) throws IOException { |
362 |
if (this.indexFile.exists()) { |
362 |
if (this.indexFile.exists()) { |
363 |
if (reuseExistingFile) { |
363 |
if (reuseExistingFile) { |
364 |
RandomAccessFile file = new RandomAccessFile(this.indexFile, "r"); //$NON-NLS-1$ |
364 |
FileInputStream stream = new FileInputStream(this.indexFile); |
|
|
365 |
this.streamBuffer = new byte[BUFFER_READ_SIZE]; |
366 |
this.bufferIndex = 0; |
367 |
this.bufferEnd = stream.read(this.streamBuffer, 0, 128); |
365 |
try { |
368 |
try { |
366 |
String signature = file.readUTF(); |
369 |
char[] signature = readStreamChars(stream); |
367 |
if (!signature.equals(SIGNATURE)) |
370 |
if (!CharOperation.equals(signature, SIGNATURE_CHARS)) { |
368 |
throw new IOException(Messages.exception_wrongFormat); |
371 |
throw new IOException(Messages.exception_wrongFormat); |
369 |
|
372 |
} |
370 |
this.headerInfoOffset = file.readInt(); |
373 |
this.headerInfoOffset = readStreamInt(stream); |
371 |
if (this.headerInfoOffset > 0) // file is empty if its not set |
374 |
if (this.headerInfoOffset > 0) { // file is empty if its not set |
372 |
readHeaderInfo(file); |
375 |
stream.skip(this.headerInfoOffset - this.bufferEnd); // assume that the header info offset is over current buffer end |
|
|
376 |
this.bufferIndex = 0; |
377 |
this.bufferEnd = stream.read(this.streamBuffer, 0, this.streamBuffer.length); |
378 |
readHeaderInfo(stream); |
379 |
} |
373 |
} finally { |
380 |
} finally { |
374 |
file.close(); |
381 |
stream.close(); |
375 |
} |
382 |
} |
376 |
return; |
383 |
return; |
377 |
} |
384 |
} |
Lines 382-393
Link Here
|
382 |
} |
389 |
} |
383 |
} |
390 |
} |
384 |
if (this.indexFile.createNewFile()) { |
391 |
if (this.indexFile.createNewFile()) { |
385 |
RandomAccessFile file = new RandomAccessFile(this.indexFile, "rw"); //$NON-NLS-1$ |
392 |
FileOutputStream stream = new FileOutputStream(this.indexFile, false); |
386 |
try { |
393 |
try { |
387 |
file.writeUTF(SIGNATURE); |
394 |
this.streamBuffer = new byte[BUFFER_READ_SIZE]; |
388 |
file.writeInt(-1); // file is empty |
395 |
this.bufferIndex = 0; |
|
|
396 |
writeStreamChars(stream, SIGNATURE_CHARS); |
397 |
writeStreamInt(stream, -1); // file is empty |
398 |
// write the buffer to the stream |
399 |
if (this.bufferIndex > 0) { |
400 |
stream.write(this.streamBuffer, 0, this.bufferIndex); |
401 |
this.bufferIndex = 0; |
402 |
} |
389 |
} finally { |
403 |
} finally { |
390 |
file.close(); |
404 |
stream.close(); |
391 |
} |
405 |
} |
392 |
} else { |
406 |
} else { |
393 |
if (DEBUG) |
407 |
if (DEBUG) |
Lines 750-777
Link Here
|
750 |
this.streamBuffer = null; |
764 |
this.streamBuffer = null; |
751 |
} |
765 |
} |
752 |
} |
766 |
} |
753 |
private void readHeaderInfo(RandomAccessFile file) throws IOException { |
767 |
private void readHeaderInfo(FileInputStream stream) throws IOException { |
754 |
file.seek(this.headerInfoOffset); |
|
|
755 |
|
768 |
|
756 |
// must be same order as writeHeaderInfo() |
769 |
// must be same order as writeHeaderInfo() |
757 |
this.numberOfChunks = file.readInt(); |
770 |
this.numberOfChunks = readStreamInt(stream); |
758 |
this.sizeOfLastChunk = file.readUnsignedByte(); |
771 |
this.sizeOfLastChunk = this.streamBuffer[this.bufferIndex++] & 0xFF; |
759 |
this.documentReferenceSize = file.readUnsignedByte(); |
772 |
this.documentReferenceSize = this.streamBuffer[this.bufferIndex++] & 0xFF; |
760 |
|
773 |
|
761 |
this.chunkOffsets = new int[this.numberOfChunks]; |
774 |
this.chunkOffsets = new int[this.numberOfChunks]; |
762 |
for (int i = 0; i < this.numberOfChunks; i++) |
775 |
for (int i = 0; i < this.numberOfChunks; i++) |
763 |
this.chunkOffsets[i] = file.readInt(); |
776 |
this.chunkOffsets[i] = readStreamInt(stream); |
764 |
|
777 |
|
765 |
this.startOfCategoryTables = file.readInt(); |
778 |
this.startOfCategoryTables = readStreamInt(stream); |
766 |
|
779 |
|
767 |
int size = file.readInt(); |
780 |
int size = readStreamInt(stream); |
768 |
this.categoryOffsets = new HashtableOfIntValues(size); |
781 |
this.categoryOffsets = new HashtableOfIntValues(size); |
769 |
this.categoryEnds = new HashtableOfIntValues(size); |
782 |
this.categoryEnds = new HashtableOfIntValues(size); |
770 |
char[] previousCategory = null; |
783 |
char[] previousCategory = null; |
771 |
int offset = -1; |
784 |
int offset = -1; |
772 |
for (int i = 0; i < size; i++) { |
785 |
for (int i = 0; i < size; i++) { |
773 |
char[] categoryName = INTERNED_CATEGORY_NAMES.get(file.readUTF().toCharArray()); |
786 |
char[] categoryName = INTERNED_CATEGORY_NAMES.get(readStreamChars(stream)); |
774 |
offset = file.readInt(); |
787 |
offset = readStreamInt(stream); |
775 |
this.categoryOffsets.put(categoryName, offset); // cache offset to category table |
788 |
this.categoryOffsets.put(categoryName, offset); // cache offset to category table |
776 |
if (previousCategory != null) { |
789 |
if (previousCategory != null) { |
777 |
this.categoryEnds.put(previousCategory, offset); // cache end of the category table |
790 |
this.categoryEnds.put(previousCategory, offset); // cache end of the category table |