List of usage examples for org.apache.commons.io IOUtils toCharArray
public static char[] toCharArray(Reader input) throws IOException
Reader
as a character array. From source file:org.jspringbot.keyword.xml.XMLHelper.java
public void setXmlString(String xmlString) throws IOException, SAXException { this.xmlString = xmlString; if (StringUtils.startsWith(xmlString, "file:") || StringUtils.startsWith(xmlString, "classpath:")) { ResourceEditor editor = new ResourceEditor(); editor.setAsText(xmlString);/*from w w w . j a va2s . com*/ Resource resource = (Resource) editor.getValue(); xmlString = new String(IOUtils.toCharArray(resource.getInputStream())); } DOMParser parser = new DOMParser(); parser.parse(new InputSource(new StringReader(xmlString))); LOG.createAppender().appendBold("XML String:").appendXML(XMLFormatter.prettyPrint(xmlString)).log(); this.document = parser.getDocument(); }
From source file:org.marketcetera.util.file.CopyCharsUnicodeUtils.java
/** * Copies the character stream at the given location (and * interpreted using the given strategy) into memory, returning a * character array.//from ww w. ja v a 2s. c o m * * @param name The name of the character source, as interpreted by * {@link ReaderWrapper#ReaderWrapper(String,DecodingStrategy)}. * @param decodingStrategy The decoding strategy. It may be null * to use the default JVM charset. * * @return The array. * * @throws I18NException Thrown if there is a data read/write * error. */ public static char[] copy(String name, DecodingStrategy decodingStrategy) throws I18NException { try { ReaderWrapper inW = new ReaderWrapper(name, decodingStrategy); try { return IOUtils.toCharArray(inW.getReader()); } finally { inW.close(); } } catch (IOException ex) { throw ExceptUtils.wrap(ex, new I18NBoundMessage1P(Messages.CANNOT_COPY_MEMORY_DST, name)); } }
From source file:org.marketcetera.util.file.CopyCharsUtils.java
/** * Copies the character stream at the given location into memory, * returning a character array.//from w ww.j a v a 2s .c om * * @param name The name of the character source, as interpreted by * {@link ReaderWrapper#ReaderWrapper(String)}. * * @return The array. * * @throws I18NException Thrown if there is a data read/write * error. */ public static char[] copy(String name) throws I18NException { try { ReaderWrapper inW = new ReaderWrapper(name); try { return IOUtils.toCharArray(inW.getReader()); } finally { inW.close(); } } catch (IOException ex) { throw ExceptUtils.wrap(ex, new I18NBoundMessage1P(Messages.CANNOT_COPY_MEMORY_DST, name)); } }
From source file:org.mule.ibeans.flickr.FlickrSignatureFactory.java
public Object create(String paramName, boolean optional, InvocationContext invocationContext) { String secretKey = (String) invocationContext.getIBeanConfig().getPropertyParams().get("secret_key"); if (secretKey == null) { throw new IllegalArgumentException( "A Flickr secret key must be set using one of the init methods on this iBeans"); }/*from w w w . j a v a 2 s. co m*/ String sig; StringBuffer buf = new StringBuffer(); buf.append(secretKey); try { //Need to find a cleaner way of doing this for users if ("GET".equals(invocationContext.getIBeanConfig().getPropertyParams().get("http.method")) || invocationContext.isTemplateMethod()) { Map<String, String> params; if (invocationContext.isTemplateMethod()) { params = invocationContext.getTemplateSpecificUriParams(); } else { params = invocationContext.getCallSpecificUriParams(); } for (Map.Entry<String, String> entry : params.entrySet()) { //Always delete the param for this factory. Also photo should be removed if (entry.getKey().equals(paramName) || entry.getKey().equals("photo")) { continue; } else { buf.append(entry.getKey()).append(entry.getValue()); } } } else { for (DataSource ds : invocationContext.getIBeanConfig().getAttachments()) { if (ds.getName().equals("photo") || ds.getName().equals("api_sig")) { continue; } buf.append(ds.getName()).append(IOUtils.toCharArray(ds.getInputStream())); } } } catch (IOException e) { e.printStackTrace(); } sig = buf.toString(); try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(buf.toString().getBytes("UTF-8")); sig = StringUtils.toHexString(bytes); } catch (Exception e) { e.printStackTrace(); } return sig; }
From source file:org.owasp.dependencytrack.dao.LibraryVersionDaoImpl.java
/** * Add a Library + LibraryVersion./*from w ww . ja v a 2 s . c om*/ * * @param libraryname The name of the Library * @param libraryversion The version of the Library * @param vendor The vendor of the Library * @param license The license the Library is licensed under * @param file The license file * @param language The programming language the library was written in */ @SuppressWarnings("unchecked") public void addLibraries(final String libraryname, final String libraryversion, final String vendor, final String license, final MultipartFile file, final String language) { final Session session = getSession(); session.beginTransaction(); LibraryVendor libraryVendor; License licenses; Library library; Query query = session.createQuery("from LibraryVendor where upper(vendor) =upper(:vendor) "); query.setParameter("vendor", vendor); if (query.list().isEmpty()) { libraryVendor = new LibraryVendor(); libraryVendor.setVendor(vendor); session.save(libraryVendor); } else { libraryVendor = (LibraryVendor) query.list().get(0); } query = session.createQuery("from License where upper(licensename) =upper(:license) "); query.setParameter("license", license); if (query.list().isEmpty()) { licenses = new License(); InputStream licenseInputStream = null; try { licenseInputStream = file.getInputStream(); String licenceFileContent = new String(IOUtils.toCharArray(licenseInputStream)); final Blob blob = Hibernate.getLobCreator(session).createBlob(licenceFileContent.getBytes()); licenses.setFilename(file.getOriginalFilename()); licenses.setContenttype(file.getContentType()); licenses.setLicensename(license); licenses.setText(blob); session.save(licenses); } catch (IOException e) { LOGGER.error("An error occurred while adding a library with library version"); LOGGER.error(e.getMessage()); } finally { IOUtils.closeQuietly(licenseInputStream); } } else { licenses = (License) query.list().get(0); } query = session.createQuery( "from Library as lib where upper(lib.libraryname) =upper(:libraryname) and lib.libraryVendor=:vendor "); query.setParameter("libraryname", libraryname); query.setParameter("vendor", libraryVendor); if (query.list().isEmpty()) { library = new Library(); library.setLibraryname(libraryname); library.setLibraryVendor(libraryVendor); library.setLicense(licenses); library.setLanguage(language); session.save(library); } else { library = (Library) query.list().get(0); } query = session.createQuery("from LibraryVersion as libver where libver.library =:library " + "and libver.library.libraryVendor=:vendor and libver.libraryversion =:libver "); query.setParameter("library", library); query.setParameter("vendor", libraryVendor); query.setParameter("libver", libraryversion); if (query.list().isEmpty()) { final LibraryVersion libVersion = new LibraryVersion(); libVersion.setLibrary(library); libVersion.setLibraryversion(libraryversion); libVersion.setUuid(UUID.randomUUID().toString()); session.save(libVersion); } session.getTransaction().commit(); query = session.createQuery("from LibraryVersion as libver where libver.library =:library " + "and libver.library.libraryVendor=:vendor and libver.libraryversion =:libver "); query.setParameter("library", library); query.setParameter("vendor", libraryVendor); query.setParameter("libver", libraryversion); final List<LibraryVersion> libraryVersions = query.list(); applicationEventPublisher.publishEvent(new DependencyCheckAnalysisRequestEvent(this, libraryVersions)); }
From source file:org.owasp.dependencytrack.dao.LibraryVersionDaoImpl.java
public void uploadLicense(final int licenseid, final MultipartFile file, final String editlicensename) { Session session = getSession();//ww w.jav a 2 s . c om InputStream licenseInputStream = null; try { Blob blob; final Query query; if (file.isEmpty()) { query = session.createQuery("update License set licensename=:lname where id=:licenseid"); query.setParameter("licenseid", licenseid); query.setParameter("lname", editlicensename); query.executeUpdate(); } else { licenseInputStream = file.getInputStream(); String licenceFileContent = new String(IOUtils.toCharArray(licenseInputStream)); blob = Hibernate.getLobCreator(session).createBlob(licenceFileContent.getBytes()); query = session.createQuery("update License set licensename=:lname," + "text=:blobfile," + "filename=:filename," + "contenttype=:contenttype " + "where id=:licenseid"); query.setParameter("licenseid", licenseid); query.setParameter("lname", editlicensename); query.setParameter("blobfile", blob); query.setParameter("filename", file.getOriginalFilename()); query.setParameter("contenttype", file.getContentType()); query.executeUpdate(); } } catch (IOException e) { LOGGER.error("An error occurred while uploading a license"); LOGGER.error(e.getMessage()); } finally { IOUtils.closeQuietly(licenseInputStream); } }
From source file:org.owasp.dependencytrack.listener.DefaultObjectGenerator.java
/** * Loads the default licenses into the database if no license data exists. @throws IOException An exception if the license file cannot be found *///from w w w . jav a 2 s . c o m private void loadDefaultLicenses() throws IOException { if (getCount(session, License.class) > 0) { return; } if (LOGGER.isInfoEnabled()) { LOGGER.info("Adding default licenses to datastore."); } session.beginTransaction(); for (Map.Entry<String, String> entry : LICENSES.entrySet()) { final String licenseName = entry.getKey(); final String licenseFile = entry.getValue(); final String contentType = (licenseFile.endsWith(".html")) ? "text/html" : "text/plain"; final License license = new License(); license.setLicensename(licenseName); InputStream inputStream = null; Resource resource; try { resource = new ClassPathResource(licenseFile); license.setFilename(resource.getFilename()); license.setContenttype(contentType); inputStream = resource.getInputStream(); String licenceFileContent = new String(IOUtils.toCharArray(inputStream)); final Blob blob = Hibernate.getLobCreator(session).createBlob(licenceFileContent.getBytes()); license.setText(blob); session.save(license); if (LOGGER.isInfoEnabled()) { LOGGER.info("Added: " + licenseName); } } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(inputStream); } } session.getTransaction().commit(); }
From source file:org.sonar.channel.CodeBuffer.java
/** * Note that this constructor will read everything from reader and will close it. *//*www.j a va 2s .c o m*/ protected CodeBuffer(Reader initialCodeReader, CodeReaderConfiguration configuration) { Reader reader = null; try { lastChar = -1; cursor = new Cursor(); tabWidth = configuration.getTabWidth(); /* Setup the filters on the reader */ reader = initialCodeReader; for (CodeReaderFilter<?> codeReaderFilter : configuration.getCodeReaderFilters()) { reader = new Filter(reader, codeReaderFilter, configuration); } buffer = IOUtils.toCharArray(reader); } catch (IOException e) { throw new ChannelException(e.getMessage(), e); } finally { IOUtils.closeQuietly(reader); } }
From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java
@SuppressWarnings("unchecked") public static <T> T extractData(final Object columnObject, final Class<T> resultingClass) throws SQLException { if (columnObject == null) { return (T) nullValueMap.get(resultingClass); }//from www. j a v a 2s .co m // try the simple case if (resultingClass.isAssignableFrom(columnObject.getClass())) { return resultingClass.cast(columnObject); } // see if we can do a simple numeric assignment if (columnObject instanceof Number) { return fromNumber(columnObject, resultingClass); } // see if we can convert from a string if (columnObject instanceof String) { return fromString(columnObject, resultingClass); } if (columnObject instanceof Boolean) { final Boolean b = (Boolean) columnObject; return fromString(b ? "1" : "0", resultingClass); } if (columnObject instanceof byte[]) { try { if (resultingClass.isAssignableFrom(Clob.class)) { return resultingClass.cast( new SerialClob(IOUtils.toCharArray(new ByteArrayInputStream((byte[]) columnObject)))); } if (resultingClass.isAssignableFrom(Blob.class)) { return resultingClass.cast(new SerialBlob((byte[]) columnObject)); } if (resultingClass.isAssignableFrom(InputStream.class)) { return resultingClass.cast(new ByteArrayInputStream((byte[]) columnObject)); } final String s = new String((byte[]) columnObject); return fromString(s, resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } if (columnObject instanceof Blob) { try { final Blob b = (Blob) columnObject; if (resultingClass.isAssignableFrom(byte[].class)) { return resultingClass.cast(IOUtils.toByteArray(b.getBinaryStream())); } if (resultingClass.isAssignableFrom(Clob.class)) { return resultingClass.cast(new SerialClob(IOUtils.toCharArray(b.getBinaryStream()))); } if (resultingClass.isAssignableFrom(InputStream.class)) { return resultingClass.cast(b.getBinaryStream()); } final String s = new String(IOUtils.toByteArray(((Blob) columnObject).getBinaryStream())); return fromString(s, resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } if (columnObject instanceof Clob) { try { final Clob c = (Clob) columnObject; if (resultingClass.isAssignableFrom(byte[].class)) { return resultingClass.cast(IOUtils.toByteArray(c.getAsciiStream())); } if (resultingClass.isAssignableFrom(Blob.class)) { return resultingClass.cast(new SerialBlob(IOUtils.toByteArray(c.getAsciiStream()))); } if (resultingClass.isAssignableFrom(InputStream.class)) { return resultingClass.cast(c.getAsciiStream()); } final String s = String.valueOf(IOUtils.toCharArray(c.getCharacterStream())); return fromString(s, resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } if (columnObject instanceof InputStream) { try { final InputStream is = (InputStream) columnObject; if (resultingClass.isAssignableFrom(Clob.class)) { return resultingClass.cast(new SerialClob(IOUtils.toCharArray(is))); } if (resultingClass.isAssignableFrom(Blob.class)) { return resultingClass.cast(new SerialBlob(IOUtils.toByteArray(is))); } if (resultingClass.isAssignableFrom(byte[].class)) { return resultingClass.cast(IOUtils.toByteArray(is)); } return fromString(new String(IOUtils.toByteArray(is)), resultingClass); } catch (final IOException e) { throw new SQLException(e.getMessage(), e); } } throw new SQLException(String.format(" Can not cast %s (%s) to %s", columnObject.getClass(), columnObject.toString(), resultingClass)); }
From source file:org.xmlactions.pager.actions.form.FileViewerAction.java
private String loadPage() throws IOException { String page = null;/* w ww .j av a2s . c o m*/ if (getRef() != null) { page = execContext.getString(getRef()); } else { if (path == null) { path = (String) execContext.get(ActionConst.WEB_REAL_PATH_BEAN_REF); } String fileName = StrSubstitutor.replace(getFile_name(), execContext); if (path == null) { path = (String) execContext.get(ActionConst.WEB_REAL_PATH_BEAN_REF); } if (path != null) { path = StrSubstitutor.replace(getPath(), execContext); } try { page = Action.loadPage(path, fileName); } catch (IllegalArgumentException ex) { // try a load a page from a url InputStream is = null; try { URL url = new URL(fileName); if (url != null) { is = url.openStream(); // emm we may have an inputsteam char[] content = IOUtils.toCharArray(is); page = new String(content); } else { throw ex; } } catch (Exception ignoreme) { throw ex; } } } return page; }