List of usage examples for org.apache.commons.io FileUtils readFileToByteArray
public static byte[] readFileToByteArray(File file) throws IOException
From source file:com.t3.model.AssetManager.java
/** * Get the asset from the cache. If the asset is not currently available, will return null. Does not request the * asset from the server/*w w w. j a va2 s. co m*/ * * @param id * MD5 of the asset requested * @return Asset object for the MD5 sum */ public static Asset getAsset(MD5Key id) { if (id == null) { return null; } Asset asset = assetMap.get(id); if (asset == null && usePersistentCache && assetIsInPersistentCache(id)) { // Guaranteed that asset is in the cache. asset = getFromPersistentCache(id); } if (asset == null && assetHasLocalReference(id)) { File imageFile = getLocalReference(id); if (imageFile != null) { try { String name = FileUtil.getNameWithoutExtension(imageFile); byte[] data = FileUtils.readFileToByteArray(imageFile); asset = new Asset(name, data); // Just to be sure the image didn't change if (!asset.getId().equals(id)) { throw new IOException("Image reference did not match the requested image"); } // Put it in the persistent cache so we'll find it faster next time putInPersistentCache(asset); } catch (IOException ioe) { // Log, but continue as if we didn't have a link ioe.printStackTrace(); } } } return asset; }
From source file:ai.h2o.servicebuilder.Util.java
/** * The Java template file has a placeholder for the model name -- we replace that here * * @param tmpDir run in this directory * @param javaClassName model name/*from ww w . j ava 2 s. c o m*/ * @param templateFileName template file * @param resultFileName restult file * @throws IOException */ public static void InstantiateJavaTemplateFile(File tmpDir, String modelCode, String javaClassName, String replaceTransform, String pythonEnvironment, String templateFileName, String resultFileName) throws IOException { byte[] templateJava = FileUtils.readFileToByteArray(new File(tmpDir, templateFileName)); String java = new String(templateJava).replace(JAVA_TEMPLATE_REPLACE_WITH_PREDICTOR_CLASS_NAME, javaClassName); if (replaceTransform != null) java = java.replace(JAVA_TEMPLATE_REPLACE_WITH_TRANSFORMER_OBJECT, replaceTransform); if (pythonEnvironment != null) java = java.replace(JAVA_TEMPLATE_REPLACE_WITH_PYTHON_ENVIRONMENT_FILE, pythonEnvironment); if (modelCode != null) java = java.replace(JAVA_TEMPLATE_REPLACE_THIS_WITH_MODEL, modelCode); FileUtils.writeStringToFile(new File(tmpDir, resultFileName), java); }
From source file:gov.nih.nci.firebird.test.FirebirdFileFactory.java
public FirebirdFile create(File file) throws IOException { FirebirdFile firebirdFile = create(FileUtils.readFileToByteArray(file)); String resourceBaseName = file.getName(); firebirdFile.setName(resourceBaseName); return firebirdFile; }
From source file:com.coinblesk.client.backup.BackupDialogFragment.java
private void addFilesToZip(ZipOutputStream zos) throws IOException { File appRoot = new File(getActivity().getApplicationInfo().dataDir); Collection<File> filesToBackup = filesToBackup(); for (File file : filesToBackup) { byte[] data = FileUtils.readFileToByteArray(file); String zipName = stripParentPath(file, appRoot); addZipEntry(zipName, data, zos); Log.i(TAG, "Added file to zip: app[" + file.toString() + "] -> backup[" + zipName + "]"); }//from www.j a v a 2 s . c om }
From source file:com.huawei.ais.demo.TokenDemo.java
/** * ?Base64???Token???// w w w . j a va2 s .c om * @param token token? * @param formFile * @throws IOException */ public static void requestOcrDriverLicenseBase64(String token, String formFile) { // 1.???? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/driver-license"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.???, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.edmunds.etm.common.impl.UrlTokenRepository.java
/** * Reads a list of tokens from the specified XML file. * * @param file an XML file containing token definitions * @return list of URL tokens/*from w w w . j a v a2 s . c om*/ * @throws IOException if an error occurs while reading the XML */ public List<UrlToken> readTokensFromFile(File file) throws IOException { byte[] xmlData = FileUtils.readFileToByteArray(file); List<UrlToken> tokens; try { tokens = unmarshalTokensFromXml(xmlData); } catch (XmlValidationException e) { String message = "Invalid default URL token XML file"; logger.error(message, e); throw new IOException(message, e); } catch (RuntimeException e) { String message = "Error loading default tokens"; logger.error(message, e); throw new IOException(message, e); } return tokens; }
From source file:ga.rugal.jpt.common.tracker.server.TrackedTorrent.java
/** * Load a tracked torrent from the given torrent file. * * @param file The abstract {@link File} object representing the * <tt>.torrent</tt> file to load. * * @return//from www . j a v a 2 s .c o m * * @throws IOException When the torrent file cannot be read. */ public static TrackedTorrent load(File file) throws IOException { byte[] data = FileUtils.readFileToByteArray(file); TrackedTorrent torrent = new TrackedTorrent(data); return torrent; }
From source file:com.greenpepper.server.license.DefaultAuthorizer.java
private void registerLicense(File licenseFile) { sessionService.startSession();/*w w w . j a va2s . co m*/ sessionService.beginTransaction(); try { SystemInfo systemInfo = systDao.getSystemInfo(); systemInfo.setLicense(new String(Base64.encodeBase64(FileUtils.readFileToByteArray(licenseFile)))); sessionService.commitTransaction(); } catch (Exception ex) { sessionService.rollbackTransaction(); log.warn(ex.getMessage()); log.debug("Register license", ex); } finally { sessionService.closeSession(); IOUtil.deleteFile(licenseFile); } }
From source file:com.frostwire.bittorrent.BTEngine.java
private SessionParams loadSettings() { try {//from ww w . j a v a2 s . com File f = settingsFile(); if (f.exists()) { byte[] data = FileUtils.readFileToByteArray(f); byte_vector buffer = Vectors.bytes2byte_vector(data); bdecode_node n = new bdecode_node(); error_code ec = new error_code(); int ret = bdecode_node.bdecode(buffer, n, ec); if (ret == 0) { String stateVersion = n.dict_find_string_value_s(STATE_VERSION_KEY); if (!STATE_VERSION_VALUE.equals(stateVersion)) { return defaultParams(); } session_params params = libtorrent.read_session_params(n); buffer.clear(); // prevents GC return new SessionParams(params); } else { LOG.error("Can't decode session state data: " + ec.message()); return defaultParams(); } } else { return defaultParams(); } } catch (Throwable e) { LOG.error("Error loading session state", e); return defaultParams(); } }
From source file:br.eti.kinoshita.selenium.SeleniumWebTest.java
/** * <p>In this method, your screen shot will be converted into a TAP Stream * and then put into a TAPAttribute object. This object will be stored in * TestNG Test Context.</p>//from w w w. j ava 2 s .c o m * * <p>Later, TestTAPReporter from tap4j.org project has the logic to * transform it into a YAMLish diagnostic entry in your TAP Stream. From * this point on, the limit to where, how, when it will be used is your * imagination.</p> * * @see {@link TAPAttribute} * @see {@link TestTAPReporter} * @see <a href="http://www.testanything.org">Test Anything Protocol</a> * * @param context TestNG test context. * @param method TestNG test method, to which we will link your attribute to. * @param screenshot A screen shot, that will be added to a TAPAttribute. */ @SuppressWarnings("unchecked") protected void addScreenShot(ITestContext context, Method method, SeleniumScreenshot screenshot) { final Object o = context.getAttribute("Files"); Map<String, Object> filesMap = null; if (o == null) { filesMap = new LinkedHashMap<String, Object>(); } else { TAPAttribute attr = (TAPAttribute) o; if (attr.getMethod() != method) { filesMap = new LinkedHashMap<String, Object>(); } else { filesMap = (Map<String, Object>) attr.getValue(); } } final Map<String, Object> fileMap = new LinkedHashMap<String, Object>(); final File file = screenshot.getFile(); // These properties were not randomly chosen. Some of them were // defined in a TAP Wiki page. I know I have the link is somewhere here... fileMap.put("File-Location", file.getAbsolutePath()); fileMap.put("File-Title", screenshot.getTitle()); fileMap.put("File-Description", screenshot.getDescription()); fileMap.put("File-Size", file.length()); fileMap.put("File-Name", file.getName()); byte[] fileData = null; try { fileData = FileUtils.readFileToByteArray(file); } catch (IOException e) { Assert.fail("Failed to read file to byte array.", e); } final String content = Base64.encodeBase64String(fileData); fileMap.put("File-Content", content); fileMap.put("File-Type", screenshot.getFileType()); filesMap.put(file.getAbsolutePath(), fileMap); final TAPAttribute attribute = new TAPAttribute(method, filesMap); context.setAttribute("Files", attribute); }