为什么在ObservableMap中引用时,InputStream实例是关闭的?

时间:2015-07-24 12:06:12

标签: java inputstream

我有ObservableMap,其中添加了资源文件。

private ObservableMap<String, InputStream> resourceFilesData;
resourceFilesData = new ObservableMapWrapper<String, InputStream>(
    new HashMap<String, InputStream>()
);

并以这种方式添加了InputStream:

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();
}

迹:     http://pastebin.com/hE21ECL9

1 个答案:

答案 0 :(得分:1)

我不知道这种行为的原因。但您可以尝试使用继承的类来调试问题:

class FileInputStreamInh extends FileInputStream {

    public FileInputStreamInh(File file) throws FileNotFoundException {
        super(file);
    }

    @Override
    public void close() throws IOException {
        super.close();
        ^^^breakpoint here
    }
}

因此,您应该创建FileInputStream

,而不是创建FileInputStreamInh