You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by Dmitry Goldenberg <dm...@weblayers.com> on 2006/01/21 00:37:33 UTC

Urgent issue with custom Directory/IndexInput/IndexOutput

Hello,
 
I'm having trouble getting a custom Directory to work, keep getting exceptions in org.apache.lucene.store.BufferedIndexInput.refill (stack attached below).  Could someone review the code below and tell me what I'm doing wrong?  Any pointers would be greatly appreciated.
 
- Dmitry
 
=============================================================
public class BufferedRepoIndexOutput extends BufferedIndexOutput {
  private XmlRepository repo;
  private String resourcePath;
  private ByteArrayOutputStream content;
  private long length = 0L;
  
  public BufferedRepoIndexOutput(XmlRepository repo, String resourcePath) throws IOException {
    this.repo = repo;
    this.resourcePath = resourcePath;    
    content = new ByteArrayOutputStream();
  }

  protected void flushBuffer(byte[] b, int len) throws IOException {
    content.write(b, 0, len);
    length += len;
  }

  public long length() throws IOException {
    return length;
  }
  
  public void close() throws IOException {
    super.close();
    byte[] bytes = content.toByteArray();
    repo.saveUnencryptedContents(resourcePath, bytes, true);     
  }
}
 
=============================================================
 
public class BufferedRepoIndexInput extends BufferedIndexInput {
  private XmlRepository repo = null;
  private String resourcePath;
  private long position = 0L;
  private long length = 0L;
  private byte[] content = null;
  private boolean isClosed = false;
  
  public BufferedRepoIndexInput(XmlRepository repo, String resourcePath) throws IOException {
    this.resourcePath = resourcePath;    
    this.repo = repo;
    InputStream stream = repo.getUnencryptedContentsAsStream(resourcePath);
    content = StreamHelper.read(stream, true);
  }
  
  protected void readInternal(byte[] bytes, int offset, int len) throws IOException {
    System.arraycopy(content, offset, bytes, 0, len);
  }
 
  protected void seekInternal(long pos) throws IOException {
    if (pos > length) {
      throw new IOException("Seeking past end of file, resource: " + resourcePath);
    }
    position = pos;
  }
 
  public void close() throws IOException {   
    isClosed = true;
  }
 
  public long length() {
    return content.length;
  }
}
 
=============================================================
 
java.io.IOException: read past EOF

at org.apache.lucene.store.BufferedIndexInput.refill(BufferedIndexInput.java:62)

at org.apache.lucene.store.BufferedIndexInput.readByte(BufferedIndexInput.java:33)

at org.apache.lucene.store.IndexInput.readVInt(IndexInput.java:56)

at org.apache.lucene.index.FieldInfos.read(FieldInfos.java:279)

at org.apache.lucene.index.FieldInfos.<init>(FieldInfos.java:58)

at org.apache.lucene.index.SegmentReader.initialize(SegmentReader.java:144)

at org.apache.lucene.index.SegmentReader.get(SegmentReader.java:129)

at org.apache.lucene.index.SegmentReader.get(SegmentReader.java:110)

at org.apache.lucene.index.IndexWriter.mergeSegments(IndexWriter.java:674)

at org.apache.lucene.index.IndexWriter.mergeSegments(IndexWriter.java:658)

at org.apache.lucene.index.IndexWriter.flushRamSegments(IndexWriter.java:628)

at org.apache.lucene.index.IndexWriter.optimize(IndexWriter.java:508)

at com.weblayers.core.search.lucene.IndexerImpl.index(IndexerImpl.java:170)