private Properties loadProperties(Path propertiesFile) throws IOException { final Properties properties = new Properties(); try (InputStream stream = Fs.getInputStream(propertiesFile)) { properties.load(stream); } return properties; }
public static byte[] getBytes(Path path) throws IOException { return Util.readBytes(getInputStream(path)); }
private static Properties getProperties(Path propertiesFile) { Properties properties = new Properties(); // return an empty Properties object if the propertiesFile does not exist if (!Files.exists(propertiesFile)) return properties; InputStream stream; try { stream = Fs.getInputStream(propertiesFile); } catch (IOException e) { throw new RuntimeException(e); } try { try { properties.load(stream); } finally { stream.close(); } } catch (IOException e) { throw new RuntimeException(e); } return properties; }
private static synchronized Document parse(Path xmlFile) { InputStream inputStream = null; try { if (documentBuilder == null) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setIgnoringComments(true); documentBuilderFactory.setIgnoringElementContentWhitespace(true); documentBuilder = documentBuilderFactory.newDocumentBuilder(); } inputStream = Fs.getInputStream(xmlFile); return documentBuilder.parse(inputStream); } catch (ParserConfigurationException | IOException | SAXException e) { throw new RuntimeException(e); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { // ignore } } }
@Override public InputStream getRawValue(ResName resName, ResTable_config config) { FileTypedResource fileTypedResource = getFileResource(resName, config); if (fileTypedResource == null) { return null; } else { Path file = fileTypedResource.getPath(); try { return file == null ? null : Fs.getInputStream(file); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override protected void loadResourceXmlFile(XmlContext xmlContext) { Path xmlFile = xmlContext.getXmlFile(); XMLStreamReader xmlStreamReader; try { xmlStreamReader = factory.createXMLStreamReader(Fs.getInputStream(xmlFile)); doParse(xmlStreamReader, xmlContext); } catch (Exception e) { throw new RuntimeException("error parsing " + xmlFile, e); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }
/** * Extract an asset from a zipped up assets provided by the build system, this is required because * there is no way to get a FileDescriptor from a zip entry. This is a temporary measure for Bazel * which can be removed once binary resources are supported. */ private static Path getFileFromZip(Path path) { byte[] buffer = new byte[1024]; try { Path outputDir = new TempDirectory("robolectric_assets").create("fromzip"); try (InputStream zis = Fs.getInputStream(path)) { Path fileFromZip = outputDir.resolve(path.getFileName().toString()); try (OutputStream fos = Files.newOutputStream(fileFromZip)) { int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } return fileFromZip; } } catch (IOException e) { throw new RuntimeException(e); } }
@Implementation protected final InputStream open(String fileName, int accessMode) throws IOException { return Fs.getInputStream(findAssetFile(fileName)); }
@Implementation protected final InputStream open(String fileName) throws IOException { return Fs.getInputStream(findAssetFile(fileName)); }
InputStream inputStream = Fs.getInputStream(androidManifestFile); Document manifestDocument = db.parse(inputStream); inputStream.close();
@HiddenApi @Implementation public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException { final ResName resName = qualifyFromNonAssetFileName(fileName); final FileTypedResource typedResource = (FileTypedResource) resourceTable.getValue(resName, config); if (typedResource == null) { throw new IOException("Unable to find resource for " + fileName); } InputStream stream; if (accessMode == AssetManager.ACCESS_STREAMING) { stream = Fs.getInputStream(typedResource.getPath()); } else { stream = new ByteArrayInputStream(Fs.getBytes(typedResource.getPath())); } if (RuntimeEnvironment.getApiLevel() >= P) { Asset asset = Asset.newFileAsset(typedResource); long assetPtr = Registries.NATIVE_ASSET_REGISTRY.register(asset); // Camouflage the InputStream as an AssetInputStream so subsequent instanceof checks pass. stream = ShadowAssetInputStream.createAssetInputStream(stream, assetPtr, realObject); } return stream; }