Java 将RAMDirectory上载到AzureCloud会创建EOF异常

Java 将RAMDirectory上载到AzureCloud会创建EOF异常,java,azure,lucene,azure-storage,azure-storage-blobs,Java,Azure,Lucene,Azure Storage,Azure Storage Blobs,我目前正在尝试使用AzureBlobStorage与Lucene合作。因此,我创建了一个新目录,为了避免太多延迟,我使用了一个RAMDirectory作为缓存(这可能不是最好的解决方案,但似乎很容易做到,我愿意接受建议)。不管怎么说,除了我将.nrm文件写到云端时,一切似乎都运行得很好,当我将它们上传到blob时,总是会引发EOFEException 我将快速解释目录是如何工作的,因为它将有助于理解:我创建了一个名为BlobOutputStream的新索引输出,它几乎封装了一个RAMOutput

我目前正在尝试使用AzureBlobStorage与Lucene合作。因此,我创建了一个新目录,为了避免太多延迟,我使用了一个RAMDirectory作为缓存(这可能不是最好的解决方案,但似乎很容易做到,我愿意接受建议)。不管怎么说,除了我将
.nrm
文件写到云端时,一切似乎都运行得很好,当我将它们上传到blob时,总是会引发EOFEException

我将快速解释目录是如何工作的,因为它将有助于理解:我创建了一个名为
BlobOutputStream
的新索引输出,它几乎封装了一个
RAMOutputStream
,但是当它关闭时,它会将所有内容上传到azureBlobStorage。这是如何做到的:

String fname = name;
output.flush();
long length = output.length();
output.close();
System.out.println("Size of the upload: " + length);
InputStream bStream = directory.openCachedInputAsStream(fname);
System.out.println("Uploading cache version of: " + fname);
blob.upload(bStream, length);
System.out.println("PUT finished for: " + fname);
blob
是一个
CloubBlockBlob
output
是一个
RAMOutputStream
<代码>目录。openCacheInputAsStream在
IndexInput
上打开一个新的
InputStream

因此,除
.nrm
文件外,所有文件在大多数情况下都能正常工作,这些文件在上载时总是引发
EOFEException
。尽管我检查过,当索引中只有一个文档并且包含“NRM-1和该文档的规范”时,它们的长度为5字节

当我在上传调用中指定了流的大小时,我真的不明白为什么Azure会尝试上传比文件中更多的内容

对不起,如果我不清楚的话,很难解释。如果你需要更多的代码,请告诉我,我会在github或其他网站上让所有东西都可以访问

谢谢你的回答

编辑 因此,我的
inputStream
的代码可能会显示问题:

public class StreamInput extends InputStream {
public IndexInput input;

public StreamInput(IndexInput openInput) {
    input = openInput;
}

@Override
public int read() throws IOException {
    System.out.println("Attempt to read byte: "+ input.getFilePointer());
    int b = input.readByte();
    System.out.println(b);
    return b;
}
}
以下是我得到的线索:

Size of the upload: 5
Uploading cache version of: _0.nrm
Attempt to read byte: 0
78
Attempt to read byte: 1
82
Attempt to read byte: 2
77
Attempt to read byte: 3
-1
Attempt to read byte: 4
114
Attempt to read byte: 5
Attempt to read byte: 1029
java.io.EOFException: read past EOF: RAMInputStream(name=_0.nrm)
    at org.apache.lucene.store.RAMInputStream.switchCurrentBuffer(RAMInputStream.java:100)
    at org.apache.lucene.store.RAMInputStream.readByte(RAMInputStream.java:73)
    at org.lahab.clucene.core.StreamInput.read(StreamInput.java:18)
    at java.io.InputStream.read(InputStream.java:151)
    at com.microsoft.windowsazure.services.core.storage.utils.Utility.writeToOutputStream(Utility.java:1024)
    at com.microsoft.windowsazure.services.blob.client.BlobOutputStream.write(BlobOutputStream.java:560)
    at com.microsoft.windowsazure.services.blob.client.CloudBlockBlob.upload(CloudBlockBlob.java:455)
    at com.microsoft.windowsazure.services.blob.client.CloudBlockBlob.upload(CloudBlockBlob.java:374)
    at org.lahab.clucene.core.BlobOutputStream.close(BlobOutputStream.java:92)
    at org.apache.lucene.util.IOUtils.close(IOUtils.java:141)
    at org.apache.lucene.index.NormsWriter.flush(NormsWriter.java:172)
    at org.apache.lucene.index.DocInverter.flush(DocInverter.java:71)
    at org.apache.lucene.index.DocFieldProcessor.flush(DocFieldProcessor.java:60)
    at org.apache.lucene.index.DocumentsWriter.flush(DocumentsWriter.java:581)
    at org.apache.lucene.index.IndexWriter.doFlush(IndexWriter.java:3587)
    at org.apache.lucene.index.IndexWriter.prepareCommit(IndexWriter.java:3376)
    at org.apache.lucene.index.IndexWriter.commitInternal(IndexWriter.java:3485)
    at org.apache.lucene.index.IndexWriter.commit(IndexWriter.java:3467)
    at org.apache.lucene.index.IndexWriter.commit(IndexWriter.java:3451)
    at org.lahab.clucene.server.IndexerNode.addDocuments(IndexerNode.java:139)

上传大小:5
正在上载的缓存版本:_0.nrm
尝试读取字节:0
78
尝试读取字节:1
82
尝试读取字节:2
77
尝试读取字节:3
-1
尝试读取字节:4
114
尝试读取字节:5
尝试读取字节:1029
java.io.EOFException:read-over-EOF:RAMInputStream(name=_0.nrm)
位于org.apache.lucene.store.RAMInputStream.switchCurrentBuffer(RAMInputStream.java:100)
位于org.apache.lucene.store.RAMInputStream.readByte(RAMInputStream.java:73)
位于org.lahab.clucene.core.StreamInput.read(StreamInput.java:18)
读取(InputStream.java:151)
位于com.microsoft.windowsazure.services.core.storage.utils.Utility.writeToOutputStream(Utility.java:1024)
位于com.microsoft.windowsazure.services.blob.client.BlobOutputStream.write(BlobOutputStream.java:560)
位于com.microsoft.windowsazure.services.blob.client.CloudBlockBlob.upload(CloudBlockBlob.java:455)
位于com.microsoft.windowsazure.services.blob.client.CloudBlockBlob.upload(CloudBlockBlob.java:374)
位于org.lahab.clucene.core.BlobOutputStream.close(BlobOutputStream.java:92)
位于org.apache.lucene.util.IOUtils.close(IOUtils.java:141)
位于org.apache.lucene.index.NormsWriter.flush(NormsWriter.java:172)
位于org.apache.lucene.index.DocInverter.flush(DocInverter.java:71)
位于org.apache.lucene.index.DocFieldProcessor.flush(DocFieldProcessor.java:60)
位于org.apache.lucene.index.DocumentsWriter.flush(DocumentsWriter.java:581)
位于org.apache.lucene.index.IndexWriter.doFlush(IndexWriter.java:3587)
位于org.apache.lucene.index.IndexWriter.prepareCommit(IndexWriter.java:3376)
位于org.apache.lucene.index.IndexWriter.commitInternal(IndexWriter.java:3485)
位于org.apache.lucene.index.IndexWriter.commit(IndexWriter.java:3467)
位于org.apache.lucene.index.IndexWriter.commit(IndexWriter.java:3451)
位于org.lahab.clucene.server.IndexerNode.addDocuments(IndexerNode.java:139)


上传似乎太过分了…

所以问题在于我的输入流,以及我无法读取文档和转换字节;)。 我的阅读功能应该是:

System.out.println(“文件:”+input.getFilePointer()+“/”+input.length());
if(input.getFilePointer()>=input.length()){
返回-1;
}
System.out.println(“尝试读取字节:+input.getFilePointer());
int b=(int)input.readByte()&0xff;
系统输出打印ln(b);
返回b;

javadoc说明了inputStream.read()

从输入流读取数据的下一个字节。值字节作为0到255范围内的整数返回如果由于到达流的结尾而没有可用字节,则返回值-1。此方法会一直阻塞,直到输入数据可用、检测到流结束或引发异常为止

然后,
&0xff
将屏蔽符号位

System.out.println("file:" + input.getFilePointer() + "/" + input.length()); if (input.getFilePointer() >= input.length()) { return -1; } System.out.println("Attempt to read byte: "+ input.getFilePointer()); int b = (int) input.readByte() & 0xff; System.out.println(b); return b;