/** Loads the resources */ void initResources() { final Class<ControlExample> clazz = ControlExample.class; if (resourceBundle != null) { try { if (images == null) { images = new Image[imageLocations.length]; for (int i = 0; i < imageLocations.length; ++i) { InputStream sourceStream = clazz.getResourceAsStream(imageLocations[i]); ImageData source = new ImageData(sourceStream); if (imageTypes[i] == SWT.ICON) { ImageData mask = source.getTransparencyMask(); images[i] = new Image(null, source, mask); } else { images[i] = new Image(null, source); } try { sourceStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return; } catch (Throwable t) { } } String error = (resourceBundle != null) ? getResourceString("error.CouldNotLoadResources") : "Unable to load resources"; //$NON-NLS-1$ freeResources(); throw new RuntimeException(error); }
/** * Helper method that will try to load version information for specified class. Implementation is * simple: class loader that loaded specified class is asked to load resource with name "VERSION" * from same location (package) as class itself had. If no version information is found, {@link * Version#unknownVersion()} is returned. */ public static Version versionFor(Class<?> cls) { InputStream in; Version version = null; try { in = cls.getResourceAsStream(VERSION_FILE); if (in != null) { try { BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); String groupStr = null, artifactStr = null; String versionStr = br.readLine(); if (versionStr != null) { groupStr = br.readLine(); if (groupStr != null) { groupStr = groupStr.trim(); artifactStr = br.readLine(); if (artifactStr != null) { artifactStr = artifactStr.trim(); } } } version = parseVersion(versionStr, groupStr, artifactStr); } finally { try { in.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } catch (IOException e) { } return (version == null) ? Version.unknownVersion() : version; }
public static Properties loadProperties(Class<?> clazz, String name, String extraProperty) throws IOException { Closer closer = Closer.create(); Properties prop = new Properties(); try { InputStream in = closer.register(clazz.getResourceAsStream(name)); prop.load(in); String extraPath = System.getProperty(extraProperty); if (extraPath != null) { log.info( "Loading extra properties for " + clazz.getCanonicalName() + ":" + name + " from " + extraPath + "..."); in = closer.register( new BufferedInputStream(closer.register(new FileInputStream(extraPath)))); prop.load(in); } } finally { closer.close(); } return prop; }
public static String loadText(String aResourceName, Class aBaseClass) { String result = null; try { InputStream inputStream = aBaseClass.getResourceAsStream(aResourceName); result = loadText(inputStream); } catch (Exception ex) { System.err.println( (new StringBuilder()).append("Can't load resource ").append(aResourceName).toString()); } return result; }
public static FragmentShader load(Class<?> base, String name) { InputStream in = base.getResourceAsStream(name); try { try { return (parse(new InputStreamReader(in, Utils.ascii))); } finally { in.close(); } } catch (IOException e) { throw (new RuntimeException(e)); } }
private DesignDocument.View loadViewFromFile( Map<String, DesignDocument.View> views, View input, Class<?> repositoryClass) { try { InputStream in = repositoryClass.getResourceAsStream(input.file()); if (in == null) { throw new FileNotFoundException("Could not load view file with path: " + input.file()); } String json = IOUtils.toString(in, "UTF-8"); return mapper().readValue(json.replaceAll("\n", ""), DesignDocument.View.class); } catch (Exception e) { throw Exceptions.propagate(e); } }
private static InputSource getConfigSource(Class pAppClass) throws DataNotFoundException { URL resource = pAppClass.getResource(CONFIG_FILE_NAME); String resourceFileName = resource.toExternalForm(); File resourceFile = new File(resourceFileName); InputStream configResourceStream = pAppClass.getResourceAsStream(CONFIG_FILE_NAME); if (null == configResourceStream) { throw new DataNotFoundException( "unable to find XML configuration file resource: " + CONFIG_FILE_NAME + " for class: " + pAppClass.getName()); } InputSource inputSource = new InputSource(configResourceStream); if (!resourceFile.exists()) { inputSource.setSystemId(resourceFileName); } return (inputSource); }
/** * Gets an image resource. * * @param strResourceFilename Name of the image file. * @param srcClass Class from which the location of the resource is determined. * @return Instance of an <code>Image</code>. */ public static Image getImageResourceFile(String strResourceFilename, Class srcClass) { PngImage pngImage = null; Image image = null; try { BufferedInputStream in = new BufferedInputStream(srcClass.getResourceAsStream(strResourceFilename)); if (in == null) { System.err.println("Image not found:" + strResourceFilename); return null; } if (strResourceFilename.endsWith(".png")) { pngImage = new PngImage(in); image = Toolkit.getDefaultToolkit().createImage(pngImage); } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); copy(in, out); image = Toolkit.getDefaultToolkit().createImage(out.toByteArray()); } } catch (java.io.IOException e) { System.err.println("Unable to read image " + strResourceFilename + "."); e.printStackTrace(); } return (image); }
private void createFromTemplate() { InputStream istr = null; OutputStream ostr = null; try { istr = resourceClass.getResourceAsStream(templateName); if (istr == null) { LOGGER.log(Level.SEVERE, tl("couldNotFindTemplate", templateName)); return; } ostr = new FileOutputStream(configFile); byte[] buffer = new byte[1024]; int length = 0; length = istr.read(buffer); while (length > 0) { ostr.write(buffer, 0, length); length = istr.read(buffer); } } catch (IOException ex) { LOGGER.log(Level.SEVERE, tl("failedToWriteConfig", configFile.toString()), ex); } finally { try { if (istr != null) { istr.close(); } } catch (IOException ex) { Logger.getLogger(EssentialsConf.class.getName()).log(Level.SEVERE, null, ex); } try { if (ostr != null) { ostr.close(); } } catch (IOException ex) { LOGGER.log(Level.SEVERE, tl("failedToCloseConfig", configFile.toString()), ex); } } }