为什么在 ObservableMap 中引用 InputStream 实例时关闭?
Why are InputStream instances closed when referenced within an ObservableMap?
我有 ObservableMap,其中添加了资源文件。
private ObservableMap<String, InputStream> resourceFilesData;
resourceFilesData = new ObservableMapWrapper<String, InputStream>(
new HashMap<String, InputStream>()
);
并以这种方式添加 InputStreams:
resourceFilesData.put(f.getName(), new FileInputStream(f));
最后,当我想使用流时,它们显示为已关闭!
为什么?我找不到理由。
也许,当流关闭时,有一些乳清可以处理时刻? (用于调试)
如何使用流:
private void pack() throws JAXBException, IOException {
HashMap<String, InputStream> resources = new HashMap<>();
byte[] buf = new byte[1024];
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("../" + fwData.getFileName() + ".iolfw"));
File xml = fwData.marshal();
InputStream xmlStream = new FileInputStream(xml);
resources.put(xml.getName(), xmlStream);
resources.putAll(resourceFilesData);
for (Map.Entry<String, InputStream> data: resources.entrySet()) {
InputStream input = data.getValue();
zos.putNextEntry(new ZipEntry(data.getKey()));
for (int readNum = 0; (readNum = input.read(buf)) != -1; ) {
zos.write(buf, 0, readNum);
}
zos.closeEntry();
input.close();
}
zos.close();
resources.remove(xmlStream);
xml.delete();
}
我不知道这种行为的原因。但是您可以尝试使用继承 class:
来调试问题
class FileInputStreamInh extends FileInputStream {
public FileInputStreamInh(File file) throws FileNotFoundException {
super(file);
}
@Override
public void close() throws IOException {
super.close();
^^^breakpoint here
}
}
因此,您应该创建 FileInputStreamInh
。
而不是创建 FileInputStream
我有 ObservableMap,其中添加了资源文件。
private ObservableMap<String, InputStream> resourceFilesData;
resourceFilesData = new ObservableMapWrapper<String, InputStream>(
new HashMap<String, InputStream>()
);
并以这种方式添加 InputStreams:
resourceFilesData.put(f.getName(), new FileInputStream(f));
最后,当我想使用流时,它们显示为已关闭!
为什么?我找不到理由。 也许,当流关闭时,有一些乳清可以处理时刻? (用于调试)
如何使用流:
private void pack() throws JAXBException, IOException {
HashMap<String, InputStream> resources = new HashMap<>();
byte[] buf = new byte[1024];
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("../" + fwData.getFileName() + ".iolfw"));
File xml = fwData.marshal();
InputStream xmlStream = new FileInputStream(xml);
resources.put(xml.getName(), xmlStream);
resources.putAll(resourceFilesData);
for (Map.Entry<String, InputStream> data: resources.entrySet()) {
InputStream input = data.getValue();
zos.putNextEntry(new ZipEntry(data.getKey()));
for (int readNum = 0; (readNum = input.read(buf)) != -1; ) {
zos.write(buf, 0, readNum);
}
zos.closeEntry();
input.close();
}
zos.close();
resources.remove(xmlStream);
xml.delete();
}
我不知道这种行为的原因。但是您可以尝试使用继承 class:
来调试问题class FileInputStreamInh extends FileInputStream {
public FileInputStreamInh(File file) throws FileNotFoundException {
super(file);
}
@Override
public void close() throws IOException {
super.close();
^^^breakpoint here
}
}
因此,您应该创建 FileInputStreamInh
。
FileInputStream