2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using BufferedIndexInput
= Lucene
.Net
.Store
.BufferedIndexInput
;
18 using Directory
= Lucene
.Net
.Store
.Directory
;
19 using IndexInput
= Lucene
.Net
.Store
.IndexInput
;
20 using IndexOutput
= Lucene
.Net
.Store
.IndexOutput
;
21 using Lock
= Lucene
.Net
.Store
.Lock
;
22 namespace Lucene
.Net
.Index
26 /// <summary> Class for accessing a compound stream.
27 /// This class implements a directory, but is limited to only read operations.
28 /// Directory methods that would normally modify data throw an exception.
31 /// <author> Dmitry Serebrennikov
33 /// <version> $Id: CompoundFileReader.cs,v 1.3 2005/10/06 19:29:55 dsd Exp $
35 public class CompoundFileReader
: Directory
38 private sealed class FileEntry
46 private Directory directory
;
47 private System
.String fileName
;
50 private IndexInput stream
;
51 private System
.Collections
.Hashtable entries
= new System
.Collections
.Hashtable();
54 public CompoundFileReader(Directory dir
, System
.String name
)
63 stream
= dir
.OpenInput(name
);
65 // read the directory and init files
66 int count
= stream
.ReadVInt();
67 FileEntry entry
= null;
68 for (int i
= 0; i
< count
; i
++)
70 long offset
= stream
.ReadLong();
71 System
.String id
= stream
.ReadString();
75 // set length of the previous entry
76 entry
.length
= offset
- entry
.offset
;
79 entry
= new FileEntry();
80 entry
.offset
= offset
;
84 // set the length of the final entry
87 entry
.length
= stream
.Length() - entry
.offset
;
94 if (!success
&& (stream
!= null))
100 catch (System
.IO
.IOException e
)
107 public virtual Directory
GetDirectory()
112 public virtual System
.String
GetName()
117 public override void Close()
122 throw new System
.IO
.IOException("Already closed");
130 public override IndexInput
OpenInput(System
.String id
)
135 throw new System
.IO
.IOException("Stream closed");
137 FileEntry entry
= (FileEntry
) entries
[id
];
139 throw new System
.IO
.IOException("No sub-file with id " + id
+ " found");
141 return new CSIndexInput(stream
, entry
.offset
, entry
.length
);
145 /// <summary>Returns an array of strings, one for each file in the directory. </summary>
146 public override System
.String
[] List()
148 System
.String
[] res
= new System
.String
[entries
.Count
];
149 entries
.Keys
.CopyTo(res
, 0);
153 /// <summary>Returns true iff a file with the given name exists. </summary>
154 public override bool FileExists(System
.String name
)
156 return entries
.ContainsKey(name
);
159 /// <summary>Returns the time the named file was last modified. </summary>
160 public override long FileModified(System
.String name
)
162 return directory
.FileModified(fileName
);
165 /// <summary>Set the modified time of an existing file to now. </summary>
166 public override void TouchFile(System
.String name
)
168 directory
.TouchFile(fileName
);
171 /// <summary>Not implemented</summary>
172 /// <throws> UnsupportedOperationException </throws>
173 public override void DeleteFile(System
.String name
)
175 throw new System
.NotSupportedException();
178 /// <summary>Not implemented</summary>
179 /// <throws> UnsupportedOperationException </throws>
180 public override void RenameFile(System
.String
from, System
.String to
)
182 throw new System
.NotSupportedException();
185 /// <summary>Returns the length of a file in the directory.</summary>
186 /// <throws> IOException if the file does not exist </throws>
187 public override long FileLength(System
.String name
)
189 FileEntry e
= (FileEntry
) entries
[name
];
191 throw new System
.IO
.IOException("File " + name
+ " does not exist");
195 /// <summary>Not implemented</summary>
196 /// <throws> UnsupportedOperationException </throws>
197 public override IndexOutput
CreateOutput(System
.String name
)
199 throw new System
.NotSupportedException();
202 /// <summary>Not implemented</summary>
203 /// <throws> UnsupportedOperationException </throws>
204 public override Lock
MakeLock(System
.String name
)
206 throw new System
.NotSupportedException();
209 /// <summary>Implementation of an IndexInput that reads from a portion of the
210 /// compound file. The visibility is left as "package" *only* because
211 /// this helps with testing since JUnit test cases in a different class
212 /// can then access package fields of this class.
214 public /*internal*/ sealed class CSIndexInput
: BufferedIndexInput
217 public /*internal*/ IndexInput base_Renamed
;
218 internal long fileOffset
;
219 internal long length
;
221 internal CSIndexInput(IndexInput base_Renamed
, long fileOffset
, long length
)
223 this.base_Renamed
= base_Renamed
;
224 this.fileOffset
= fileOffset
;
225 this.length
= length
;
228 /// <summary>Expert: implements buffer refill. Reads bytes from the current
229 /// position in the input.
231 /// <param name="b">the array to read bytes into
233 /// <param name="offset">the offset in the array to start storing bytes
235 /// <param name="len">the number of bytes to read
237 public override void ReadInternal(byte[] b
, int offset
, int len
)
241 long start
= GetFilePointer();
242 if (start
+ len
> length
)
243 throw new System
.IO
.IOException("read past EOF");
244 base_Renamed
.Seek(fileOffset
+ start
);
245 base_Renamed
.ReadBytes(b
, offset
, len
);
249 /// <summary>Expert: implements seek. Sets current position in this file, where
250 /// the next {@link #ReadInternal(byte[],int,int)} will occur.
252 /// <seealso cref="#ReadInternal(byte[],int,int)">
254 public override void SeekInternal(long pos
)
258 /// <summary>Closes the stream to further operations. </summary>
259 public override void Close()
263 public override long Length()