#pragma warning restore CA2213 // Disposable fields should be disposed /// <summary> /// Create a new <see cref="CompoundFileDirectory"/>. /// </summary> public CompoundFileDirectory(Directory directory, string fileName, IOContext context, bool openForWrite) { this.directory = directory; this.fileName = fileName; //this.readBufferSize = BufferedIndexInput.GetBufferSize(context); // LUCENENET: Never read this.IsOpen = false; this.openForWrite = openForWrite; if (!openForWrite) { bool success = false; handle = directory.CreateSlicer(fileName, context); try { this.entries = ReadEntries(handle, directory, fileName); success = true; } finally { if (!success) { IOUtils.DisposeWhileHandlingException(handle); } } this.IsOpen = true; writer = null; } else { if (Debugging.AssertsEnabled) { Debugging.Assert(!(directory is CompoundFileDirectory), "compound file inside of compound file: {0}", fileName); } this.entries = SENTINEL; this.IsOpen = true; writer = new CompoundFileWriter(directory, fileName); handle = null; } }
/// <summary> /// Helper method that reads CFS entries from an input stream </summary> private static IDictionary <string, FileEntry> ReadEntries(IndexInputSlicer handle, Directory dir, string name) { IOException priorE = null; IndexInput stream = null; ChecksumIndexInput entriesStream = null; // read the first VInt. If it is negative, it's the version number // otherwise it's the count (pre-3.1 indexes) try { IDictionary <string, FileEntry> mapping; #pragma warning disable 612, 618 stream = handle.OpenFullSlice(); #pragma warning restore 612, 618 int firstInt = stream.ReadVInt32(); // impossible for 3.0 to have 63 files in a .cfs, CFS writer was not visible // and separate norms/etc are outside of cfs. if (firstInt == CODEC_MAGIC_BYTE1) { sbyte secondByte = (sbyte)stream.ReadByte(); sbyte thirdByte = (sbyte)stream.ReadByte(); sbyte fourthByte = (sbyte)stream.ReadByte(); if (secondByte != CODEC_MAGIC_BYTE2 || thirdByte != CODEC_MAGIC_BYTE3 || fourthByte != CODEC_MAGIC_BYTE4) { throw new CorruptIndexException("Illegal/impossible header for CFS file: " + secondByte + "," + thirdByte + "," + fourthByte); } int version = CodecUtil.CheckHeaderNoMagic(stream, CompoundFileWriter.DATA_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT); string entriesFileName = IndexFileNames.SegmentFileName( IndexFileNames.StripExtension(name), "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION); entriesStream = dir.OpenChecksumInput(entriesFileName, IOContext.READ_ONCE); CodecUtil.CheckHeader(entriesStream, CompoundFileWriter.ENTRY_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT); int numEntries = entriesStream.ReadVInt32(); mapping = new Dictionary <string, FileEntry>(numEntries); for (int i = 0; i < numEntries; i++) { FileEntry fileEntry = new FileEntry(); string id = entriesStream.ReadString(); FileEntry previous = mapping.Put(id, fileEntry); if (previous != null) { throw new CorruptIndexException("Duplicate cfs entry id=" + id + " in CFS: " + entriesStream); } fileEntry.Offset = entriesStream.ReadInt64(); fileEntry.Length = entriesStream.ReadInt64(); } if (version >= CompoundFileWriter.VERSION_CHECKSUM) { CodecUtil.CheckFooter(entriesStream); } else { #pragma warning disable 612, 618 CodecUtil.CheckEOF(entriesStream); #pragma warning restore 612, 618 } } else { // TODO remove once 3.x is not supported anymore mapping = ReadLegacyEntries(stream, firstInt); } return(mapping); } catch (IOException ioe) { priorE = ioe; } finally { IOUtils.DisposeWhileHandlingException(priorE, stream, entriesStream); } // this is needed until Java 7's real try-with-resources: throw new InvalidOperationException("impossible to get here"); }
public virtual void TestCloneSliceSafety() { MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneSliceSafety")); IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random())); io.WriteInt32(1); io.WriteInt32(2); io.Dispose(); IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random())); IndexInput one = slicer.OpenSlice("first int", 0, 4); IndexInput two = slicer.OpenSlice("second int", 4, 4); IndexInput three = (IndexInput)one.Clone(); // clone of clone IndexInput four = (IndexInput)two.Clone(); // clone of clone slicer.Dispose(); try { one.ReadInt32(); Assert.Fail("Must throw ObjectDisposedException"); } #pragma warning disable 168 catch (ObjectDisposedException ignore) #pragma warning restore 168 { // pass } try { two.ReadInt32(); Assert.Fail("Must throw ObjectDisposedException"); } #pragma warning disable 168 catch (ObjectDisposedException ignore) #pragma warning restore 168 { // pass } try { three.ReadInt32(); Assert.Fail("Must throw ObjectDisposedException"); } #pragma warning disable 168 catch (ObjectDisposedException ignore) #pragma warning restore 168 { // pass } try { four.ReadInt32(); Assert.Fail("Must throw ObjectDisposedException"); } #pragma warning disable 168 catch (ObjectDisposedException ignore) #pragma warning restore 168 { // pass } one.Dispose(); two.Dispose(); three.Dispose(); four.Dispose(); // test double-close of slicer: slicer.Dispose(); mmapDir.Dispose(); }
/// <summary> /// Helper method that reads CFS entries from an input stream </summary> private static IDictionary<string, FileEntry> ReadEntries(IndexInputSlicer handle, Directory dir, string name) { System.IO.IOException priorE = null; IndexInput stream = null; ChecksumIndexInput entriesStream = null; // read the first VInt. If it is negative, it's the version number // otherwise it's the count (pre-3.1 indexes) try { IDictionary<string, FileEntry> mapping; stream = handle.OpenFullSlice(); int firstInt = stream.ReadVInt(); // impossible for 3.0 to have 63 files in a .cfs, CFS writer was not visible // and separate norms/etc are outside of cfs. if (firstInt == CODEC_MAGIC_BYTE1) { byte secondByte = stream.ReadByte(); byte thirdByte = stream.ReadByte(); byte fourthByte = stream.ReadByte(); if (secondByte != CODEC_MAGIC_BYTE2 || thirdByte != CODEC_MAGIC_BYTE3 || fourthByte != CODEC_MAGIC_BYTE4) { throw new CorruptIndexException("Illegal/impossible header for CFS file: " + secondByte + "," + thirdByte + "," + fourthByte); } int version = CodecUtil.CheckHeaderNoMagic(stream, CompoundFileWriter.DATA_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT); string entriesFileName = IndexFileNames.SegmentFileName(IndexFileNames.StripExtension(name), "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION); entriesStream = dir.OpenChecksumInput(entriesFileName, IOContext.READONCE); CodecUtil.CheckHeader(entriesStream, CompoundFileWriter.ENTRY_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT); int numEntries = entriesStream.ReadVInt(); mapping = new Dictionary<string, FileEntry>(numEntries); for (int i = 0; i < numEntries; i++) { FileEntry fileEntry = new FileEntry(); string id = entriesStream.ReadString(); //If the key was already present if (mapping.ContainsKey(id)) { throw new CorruptIndexException("Duplicate cfs entry id=" + id + " in CFS: " + entriesStream); } else { mapping[id] = fileEntry; } fileEntry.Offset = entriesStream.ReadLong(); fileEntry.Length = entriesStream.ReadLong(); } if (version >= CompoundFileWriter.VERSION_CHECKSUM) { CodecUtil.CheckFooter(entriesStream); } else { CodecUtil.CheckEOF(entriesStream); } } else { // TODO remove once 3.x is not supported anymore mapping = ReadLegacyEntries(stream, firstInt); } return mapping; } catch (System.IO.IOException ioe) { priorE = ioe; } finally { IOUtils.CloseWhileHandlingException(priorE, stream, entriesStream); } // this is needed until Java 7's real try-with-resources: throw new InvalidOperationException("impossible to get here"); }
/// <summary> /// Create a new CompoundFileDirectory. /// </summary> public CompoundFileDirectory(Directory directory, string fileName, IOContext context, bool openForWrite) { this.Directory_Renamed = directory; this.FileName = fileName; this.ReadBufferSize = BufferedIndexInput.BufferSize(context); this.isOpen = false; this.OpenForWrite = openForWrite; if (!openForWrite) { bool success = false; Handle = directory.CreateSlicer(fileName, context); try { this.Entries = ReadEntries(Handle, directory, fileName); success = true; } finally { if (!success) { IOUtils.CloseWhileHandlingException(Handle); } } this.isOpen = true; Writer = null; } else { Debug.Assert(!(directory is CompoundFileDirectory), "compound file inside of compound file: " + fileName); this.Entries = SENTINEL; this.isOpen = true; Writer = new CompoundFileWriter(directory, fileName); Handle = null; } }
public IndexInputSlicerAnonymousInnerClassHelper(MockDirectoryWrapper outerInstance, string name, IndexInputSlicer delegateHandle) : base(outerInstance) { this.OuterInstance = outerInstance; this.Name = name; this.DelegateHandle = delegateHandle; }