FileInputStream.read抛出java.io.IOException:结果太大

时间:2012-03-16 08:19:20

标签: java xml inputstream ioexception

我有一个临时文件

File myTmpFile = File.createTempFile( prefix, suffix );

表示通过

导出的xml
Preferences.exportSubtree(OutputStream);

现在我要用以下内容导入此文件:

Preferences.importPreferences( new FileInputStream( myTmpFile ));

但Stream会使用Message抛出IOException(“Result too large”) 这是什么意思?我该如何解决这个问题?

java.io.IOException: Result too large
at java.io.FileInputStream.read(Native Method)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2932)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:704)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at java.util.prefs.XmlSupport.loadPrefsDoc(XmlSupport.java:233)
at java.util.prefs.XmlSupport.importPreferences(XmlSupport.java:182)
at java.util.prefs.Preferences.importPreferences(Preferences.java:1236)
at MyPreferences.importPrefs(MyPreferences.java:322)    

public static void importPrefs(File file){
    FileInputStream is = null;
    try {
        is = new FileInputStream(file);
        java.util.prefs.Preferences.importPreferences( is );
    } catch( FileNotFoundException e ) {
    } catch( IOException e ) {
        e.printStackTrace();
    } catch( InvalidPreferencesFormatException e ) {
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch( IOException e ) {
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在尝试读取之前,请确保InputStream尚未关闭。

答案 1 :(得分:0)

所以我自己解析它。使用org.w3c.dom.ls.DOMImplementationLS
但是我需要复制EntityResolverjava.util.prefs.XmlSupport来自java.util.prefs.Preferences的{​​{1}}。它有效。

InputStream parsingStream = new FileInputStream(file)
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

builder.setEntityResolver(new Resolver());
Document doc = builder.parse( parsingStream );

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();    
DOMImplementationLS domImplementationLS = (DOMImplementationLS)doc.getImplementation().getFeature( "LS", "3.0" );
LSOutput lsOutput = domImplementationLS.createLSOutput();
lsOutput.setByteStream( outputStream );
LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
lsSerializer.write( doc, lsOutput );
byte[] byteArray = outputStream.toByteArray();


ByteArrayInputStream importStream = new ByteArrayInputStream( byteArray );
Preferences.importPreferences( importStream );

/**
 * For loading an XML document from specified input stream, which must have the requisite DTD URI.
 * @see java.util.prefs.XmlSupport.Resolver
 */
private static class Resolver implements EntityResolver {
    @Override
    public InputSource resolveEntity(String pid, String sid)
        throws SAXException {
        if (sid.equals(PREFS_DTD_URI)) {
            InputSource is;
            is = new InputSource(new StringReader(PREFS_DTD));
            is.setSystemId(PREFS_DTD_URI);
            return is;
        }
        throw new SAXException("Invalid system identifier: " + sid);
    }
}