List of usage examples for java.lang Class getResourceAsStream
@CallerSensitive
public InputStream getResourceAsStream(String name)
From source file:ja.centre.gui.resources.Resources.java
private Resources(Class aClass) { Arguments.assertNotNull("aClass", aClass); this.properties = new Properties(); this.aClass = aClass; String path = aClass.getName(); path = path.replace(".", "/"); path = "/" + path + ".properties"; InputStream resource = aClass.getResourceAsStream(path); if (resource == null) { Arguments.doThrow("Properties file for class \"" + aClass.getName() + "\" does not exist"); }//w w w . ja v a2 s. c o m try { properties.load(resource); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.sap.dirigible.repository.ext.db.DBUtils.java
/** * Read whole SQL script from the class path. It can contain multiple * statements separated with ';'/*from w w w.ja v a 2 s. c om*/ * * @param path * @return the SQL script as a String */ public String readScript(Connection conn, String path, Class<?> clazz) throws IOException { logger.debug("entering readScript"); //$NON-NLS-1$ String sql = null; InputStream in = clazz.getResourceAsStream(path); if (in == null) { throw new IOException("SQL script does not exist: " + path); } BufferedInputStream bufferedInput = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(baos); byte[] buffer = new byte[1024]; try { bufferedInput = new BufferedInputStream(in); int bytesRead = 0; while ((bytesRead = bufferedInput.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead, Charset.defaultCharset()); writer.write(chunk); } writer.flush(); sql = new String(baos.toByteArray(), Charset.defaultCharset()); String productName = conn.getMetaData().getDatabaseProductName(); IDialectSpecifier dialectSpecifier = getDialectSpecifier(productName); sql = dialectSpecifier.specify(sql); } catch (FileNotFoundException ex) { logger.error(ex.getMessage()); } catch (IOException ex) { logger.error(ex.getMessage()); } catch (SQLException ex) { logger.error(ex.getMessage()); } finally { try { if (bufferedInput != null) bufferedInput.close(); } catch (IOException ex) { logger.error(ex.getMessage()); } } logger.debug("exiting readScript"); //$NON-NLS-1$ return sql; }
From source file:org.arrow.test.SpringWorkflowTestExecutionListener.java
/** * {@inheritDoc}//from w w w . j av a 2 s . co m */ @Override public void beforeTestMethod(TestContext testContext) throws Exception { ApplicationContext context = testContext.getApplicationContext(); CONTEXT_HOLDER.set(context); RepositoryService rs = context.getBean(RepositoryService.class); Class<?> testClass = testContext.getTestClass(); Method method = testContext.getTestMethod(); Given given = findAnnotation(method, Given.class); if (given != null) { final String[] fileNames = given.value(); for (String fileName : fileNames) { BpmnParser driver = new XStreamBpmnParser(); InputStream stream = testClass.getResourceAsStream(fileName); Definitions definitions = driver.parse(stream); rs.deploy(definitions); } } StopWatch stopWatch = new StopWatch(); stopWatch.start("START TEST"); testContext.setAttribute("stopWatch", stopWatch); }
From source file:com.tesora.dve.client.ClientTestNG.java
private void loadSQLFile(Class<?> testClass, String fileName) throws Exception { InputStream is = testClass.getResourceAsStream(fileName); if (is != null) { logger.info("Reading SQL statements from " + fileName); dbHelper.setDisconnectAfterProcess(false); dbHelper.executeFromStream(is);/*ww w. j av a 2 s . c o m*/ try { is.close(); } catch (IOException e) { } } }
From source file:kr.co.bitnine.octopus.testutils.MemoryDatabase.java
public void importJSON(Class<?> clazz, String resourceName) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/* w ww . j av a2 s . c om*/ Statement stmt = conn.createStatement(); JSONParser jsonParser = new JSONParser(); JSONArray tables = (JSONArray) jsonParser .parse(new InputStreamReader(clazz.getResourceAsStream(resourceName))); for (Object tableObj : tables) { StringBuilder queryBuilder = new StringBuilder(); JSONObject table = (JSONObject) tableObj; String tableName = jsonValueToSqlIdent(table.get("table-name")); queryBuilder.append("CREATE TABLE ").append(tableName).append('('); JSONArray schema = (JSONArray) table.get("table-schema"); for (Object columnNameObj : schema) { String columnName = jsonValueToSqlValue(columnNameObj); queryBuilder.append(columnName).append(','); } queryBuilder.setCharAt(queryBuilder.length() - 1, ')'); stmt.execute(queryBuilder.toString()); JSONArray rows = (JSONArray) table.get("table-rows"); for (Object rowObj : rows) { JSONArray row = (JSONArray) rowObj; queryBuilder.setLength(0); queryBuilder.append("INSERT INTO ").append(tableName).append(" VALUES("); for (Object columnObj : row) queryBuilder.append(jsonValueToSqlValue(columnObj)).append(','); queryBuilder.setCharAt(queryBuilder.length() - 1, ')'); stmt.executeUpdate(queryBuilder.toString()); } } stmt.close(); conn.commit(); conn.close(); }
From source file:org.eclipse.swt.examples.controlexample.ControlExample.java
/** * Loads the resources/*from w w w. j a va 2s. c o m*/ */ 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) { try (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); } } } } return; } catch (Throwable t) { } } String error = (resourceBundle != null) ? getResourceString("error.CouldNotLoadResources") : "Unable to load resources"; //$NON-NLS-1$ freeResources(); throw new RuntimeException(error); }
From source file:org.firesoa.common.util.LocalVariableTableParameterNameDiscoverer.java
/** * Create a ClassReader for the given class. *///from w w w. j a v a 2s . co m private ClassReader createClassReader(Class clazz) throws IOException { String className = clazz.getName(); int index = className.lastIndexOf("."); if (index > 0) { className = className.substring(index + 1); } InputStream inStream = clazz.getResourceAsStream(className + ".class"); return new ClassReader(inStream); }
From source file:org.gradle.foundation.ipc.gradle.AbstractGradleServerProtocol.java
/** * This extracts the given class' resource to the specified file if it doesn't already exist. * * @param resourceClass the class associated with the resource * @param name the resource's name//from www . j av a 2 s. c om * @param file where to put the resource * @return true if successful, false if not. */ public boolean extractResourceAsFile(Class resourceClass, String name, File file) { InputStream stream = resourceClass.getResourceAsStream(name); if (stream == null) { return false; } byte[] bytes = new byte[0]; try { bytes = IOUtils.toByteArray(stream); } catch (IOException e) { logger.error("Extracting resource as file", e); return false; } FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); try { IOUtils.write(bytes, fileOutputStream); } finally { fileOutputStream.close(); } return true; } catch (IOException e) { logger.error("Extracting resource as file (writing bytes)", e); return false; } finally { IOUtils.closeQuietly(fileOutputStream); } }
From source file:com.quirklabs.authorise.ProxyAwareLocalVariableTableParameterNameDiscoverer.java
/** * Obtain a (cached) ClassReader for the given class. *//*from w ww . ja va2s . c o m*/ private ClassReader getClassReader(Class clazz) throws IOException { synchronized (this.classReaderCache) { ClassReader classReader = (ClassReader) this.classReaderCache.get(clazz.getDeclaringClass()); if (classReader == null) { InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz)); if (is == null) { throw new FileNotFoundException("Class file for class [" + clazz.getName() + "] not found"); } try { classReader = new ClassReader(is); this.classReaderCache.put(clazz.getDeclaringClass(), classReader); } finally { is.close(); } } return classReader; } }
From source file:org.eclipse.dirigible.repository.ext.db.DBUtils.java
/** * Read whole SQL script from the class path. It can contain multiple * statements separated with ';'//from w w w .ja va 2 s .c o m * * @param path * @return the SQL script as a String */ public String readScript(Connection conn, String path, Class<?> clazz) throws IOException { logger.debug("entering readScript"); //$NON-NLS-1$ String sql = null; InputStream in = clazz.getResourceAsStream(path); if (in == null) { throw new IOException("SQL script does not exist: " + path); } BufferedInputStream bufferedInput = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(baos); byte[] buffer = new byte[1024]; try { bufferedInput = new BufferedInputStream(in); int bytesRead = 0; while ((bytesRead = bufferedInput.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead, Charset.defaultCharset()); writer.write(chunk); } writer.flush(); sql = new String(baos.toByteArray(), Charset.defaultCharset()); String productName = conn.getMetaData().getDatabaseProductName(); IDialectSpecifier dialectSpecifier = getDialectSpecifier(productName); sql = dialectSpecifier.specify(sql); } catch (FileNotFoundException ex) { logger.error(ex.getMessage(), ex); } catch (IOException ex) { logger.error(ex.getMessage(), ex); } catch (SQLException ex) { logger.error(ex.getMessage(), ex); } finally { try { if (bufferedInput != null) { bufferedInput.close(); } } catch (IOException ex) { logger.error(ex.getMessage(), ex); } } logger.debug("exiting readScript"); //$NON-NLS-1$ return sql; }