2
我有一个方法返回S3上的文件的InputStream。如果我使用这个方法两次,S3的调用将被执行两次。我想知道是否有一种方法来存储输入流?有没有办法来存储/缓存InputStream?
例如:
InputStream getInputStreamForObject(String fileName) {
//return inputstream for fileName on S3
}
String determineFiletype (InputStream is) {
//some way to determine filetype from inputstream.
//inputstream would be read here
}
void someMethod(String fileName) {
String fileType = determineFileType(getInputStreamForObject(fileName))
if (fileType == "DOCX") {
docxParser.parse(getInputStreamForObject(fileName))
}
else if (fileType == "XLSX") {
xlsxParser.parse(getInputStreamForObject(fileName))
}
}
可以使用带有标记的的BufferedInputStream()和reset() –
或者你可以读到的一切为一个字节数组在内存中,并在字节数组上打开ByteArrayInputStream。 –
@Kayaman谢谢:)这应该是伪代码。我在groovy编码,所以这将工作 – Anthony