List of usage examples for java.io IOException toString
public String toString()
From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.FileUtil.java
public static void createCompressedFiles(final List<String> fileNamesToBeCompressed, final String compressedFileName) throws IOException { final File compressedFile = new File(compressedFileName); for (final String fileNameToBeCompressed : fileNamesToBeCompressed) { final File fileToBeCompressed = new File(fileNameToBeCompressed); if (!fileToBeCompressed.exists()) { throw new IOException("Cache File does not exist: " + fileToBeCompressed.getPath()); }//from www .ja va 2 s . c om } final FileOutputStream outputFileStream = new FileOutputStream(compressedFile); final TarOutputStream tarStream = new TarOutputStream(new GZIPOutputStream(outputFileStream)); try { for (final String fileNameToBeCompressed : fileNamesToBeCompressed) { final File fileToBeCompressed = new File(fileNameToBeCompressed); final String name = fileToBeCompressed.getName(); final TarEntry tarAdd = new TarEntry(fileToBeCompressed); tarStream.setLongFileMode(TarOutputStream.LONGFILE_GNU); tarAdd.setModTime(fileToBeCompressed.lastModified()); tarAdd.setName(name); tarStream.putNextEntry(tarAdd); FileInputStream inputStream = null; byte[] buffer = new byte[1024 * 64]; try { inputStream = new FileInputStream(fileToBeCompressed); int nRead = inputStream.read(buffer, 0, buffer.length); while (nRead >= 0) { tarStream.write(buffer, 0, nRead); nRead = inputStream.read(buffer, 0, buffer.length); } tarStream.closeEntry(); } finally { buffer = null; try { if (inputStream != null) { inputStream.close(); } } catch (IOException ie) { logger.error("Error closing I/O streams " + ie.toString()); } } } } finally { if (tarStream != null) { tarStream.close(); } } }
From source file:it.cnr.icar.eric.common.AbstractProperties.java
/** * Checks if a directory for the given name exists. If not, try to create it. * * @param homeDir A File descriptor for the directory to be checked. * @return Full path to the directory.// ww w . j a v a 2 s . c o m * @throw JAXRException if it fails to find and create the directory. */ protected static String initHomeDir(String propName, File homeDir) { try { if (!homeDir.exists()) { if (!homeDir.mkdirs()) { throw new RuntimeException(CommonResourceBundle.getInstance() .getString("message.createDirectory", new String[] { propName, homeDir.getPath() })); } } else { if (!homeDir.isDirectory()) { throw new RuntimeException(CommonResourceBundle.getInstance() .getString("message.accessDirectory", new String[] { propName, homeDir.getPath() })); } } String homeDirStr = homeDir.getCanonicalPath(); return homeDirStr; } catch (SecurityException se) { throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.permissionDenied", new String[] { propName, homeDir.getPath(), se.toString() })); } catch (IOException io) { throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.createAccessDirectory", new String[] { propName, homeDir.getPath(), io.toString() })); } }
From source file:com.viettel.dms.download.DownloadFile.java
/** * Download a file from a URL somewhere. The download is atomic; * that is, it downloads to inta temporary file, then renames it to * the requested file name only if the download successfully * completes./*w w w . j a v a 2 s . c o m*/ * * Returns TRUE if download succeeds, FALSE otherwise. * * @param url Source URL * @param output Path to output file * @param tmpDir Place to put file download in progress */ public static void download(String url, File output, File tmpDir) { InputStream is = null; OutputStream os = null; File tmp = null; try { VTLog.i("DownloadDB", "Downloading url :" + url); tmp = File.createTempFile("download", ".tmp", tmpDir); is = new URL(url).openStream(); os = new BufferedOutputStream(new FileOutputStream(tmp)); copyStream(is, os); tmp.renameTo(output); tmp = null; } catch (IOException e) { VTLog.e("DownloadDB", "Loi download file db " + e.toString()); VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e)); throw new RuntimeException(e); } catch (Exception e) { VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e)); throw new RuntimeException(e); } finally { if (tmp != null) { try { tmp.delete(); tmp = null; } catch (Exception ignore) { ; } } if (is != null) { try { is.close(); is = null; } catch (Exception ignore) { ; } } if (os != null) { try { os.close(); os = null; } catch (Exception ignore) { ; } } } }
From source file:com.alibaba.akita.io.HttpInvoker.java
public static String get(String url, Header[] headers) throws AkServerStatusException, AkInvokeException { Log.v(TAG, "get:" + url); //url = url.replace("gw.api.alibaba.com", "205.204.112.73"); // usa ocean //url = url.replace("gw.api.alibaba.com", "172.20.128.127"); // yufa //url = url.replace("mobi.aliexpress.com", "172.20.226.142"); String retString = null;/*w w w. jav a 2s . c o m*/ try { HttpGet request = new HttpGet(url); if (headers != null) { for (Header header : headers) { request.addHeader(header); } } HttpResponse response = client.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_ACCEPTED) { HttpEntity resEntity = response.getEntity(); retString = (resEntity == null) ? null : EntityUtils.toString(resEntity, CHARSET); } else { HttpEntity resEntity = response.getEntity(); throw new AkServerStatusException(response.getStatusLine().getStatusCode(), EntityUtils.toString(resEntity, CHARSET)); } } catch (ClientProtocolException cpe) { Log.e(TAG, cpe.toString(), cpe); throw new AkInvokeException(AkInvokeException.CODE_HTTP_PROTOCOL_ERROR, cpe.toString(), cpe); } catch (IOException ioe) { Log.e(TAG, ioe.toString(), ioe); throw new AkInvokeException(AkInvokeException.CODE_CONNECTION_ERROR, ioe.toString(), ioe); } Log.v(TAG, "response:" + retString); return retString; }
From source file:org.akita.io.HttpInvoker.java
public static String get(String url, Header[] headers) throws AkServerStatusException, AkInvokeException { Log.v(TAG, "get:" + url); String retString = null;/* ww w .j a v a 2 s . c o m*/ try { HttpGet request = new HttpGet(url); if (headers != null) { for (Header header : headers) { request.addHeader(header); } } HttpResponse response = client.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_ACCEPTED) { HttpEntity resEntity = response.getEntity(); retString = (resEntity == null) ? null : EntityUtils.toString(resEntity, CHARSET); } else { HttpEntity resEntity = response.getEntity(); throw new AkServerStatusException(response.getStatusLine().getStatusCode(), EntityUtils.toString(resEntity, CHARSET)); } } catch (ClientProtocolException cpe) { Log.e(TAG, cpe.toString(), cpe); throw new AkInvokeException(AkInvokeException.CODE_HTTP_PROTOCOL_ERROR, cpe.toString(), cpe); } catch (IOException ioe) { Log.e(TAG, ioe.toString(), ioe); throw new AkInvokeException(AkInvokeException.CODE_CONNECTION_ERROR, ioe.toString(), ioe); } Log.v(TAG, "response:" + retString); return retString; }
From source file:com.aerospike.load.Parser.java
/** * Process column definitions in JSON formated file and create two lists for metadata and bindata and one list for metadataLoader * @param file Config file name/*from w ww .j a va2 s.c o m*/ * @param metadataLoader Map of metadata for loader to use, given in config file * @param metadataColumnDefs List of column definitions for metadata of bins like key,set * @param binColumnDefs List of column definitions for bins * @param params User given parameters * @throws ParseException * @throws IOException */ public static boolean processJSONColumnDefinitions(File file, HashMap<String, String> metadataConfigs, List<ColumnDefinition> metadataColumnDefs, List<ColumnDefinition> binColumnDefs, Parameters params) { boolean processConfig = false; if (params.verbose) { log.setLevel(Level.DEBUG); } try { // Create parser object JSONParser jsonParser = new JSONParser(); // Read the json config file Object obj; obj = jsonParser.parse(new FileReader(file)); JSONObject jobj; // Check config file contains metadata for datafile if (obj == null) { log.error("Empty config File"); return processConfig; } else { jobj = (JSONObject) obj; log.debug("Config file contents:" + jobj.toJSONString()); } // Get meta data of loader // Search for input_type if ((obj = getJsonObject(jobj, Constants.VERSION)) != null) { metadataConfigs.put(Constants.VERSION, obj.toString()); } else { log.error("\"" + Constants.VERSION + "\" Key is missing in config file"); return processConfig; } if ((obj = getJsonObject(jobj, Constants.INPUT_TYPE)) != null) { // Found input_type, check for csv if (obj instanceof String && obj.toString().equals(Constants.CSV_FILE)) { // Found csv format metadataConfigs.put(Constants.INPUT_TYPE, obj.toString()); // Search for csv_style if ((obj = getJsonObject(jobj, Constants.CSV_STYLE)) != null) { // Found csv_style JSONObject cobj = (JSONObject) obj; // Number_Of_Columns in data file if ((obj = getJsonObject(cobj, Constants.COLUMNS)) != null) { metadataConfigs.put(Constants.COLUMNS, obj.toString()); } else { log.error("\"" + Constants.COLUMNS + "\" Key is missing in config file"); return processConfig; } // Delimiter for parsing data file if ((obj = getJsonObject(cobj, Constants.DELIMITER)) != null) metadataConfigs.put(Constants.DELIMITER, obj.toString()); // Skip first row of data file if it contains column names if ((obj = getJsonObject(cobj, Constants.IGNORE_FIRST_LINE)) != null) metadataConfigs.put(Constants.IGNORE_FIRST_LINE, obj.toString()); } else { log.error("\"" + Constants.CSV_STYLE + "\" Key is missing in config file"); return processConfig; } } else { log.error("\"" + obj.toString() + "\" file format is not supported in config file"); return processConfig; } } else { log.error("\"" + Constants.INPUT_TYPE + "\" Key is missing in config file"); return processConfig; } // Get metadata of records // Get key definition of records if ((obj = getJsonObject(jobj, Constants.KEY)) != null) { metadataColumnDefs.add(getColumnDefs((JSONObject) obj, Constants.KEY)); } else { log.error("\"" + Constants.KEY + "\" Key is missing in config file"); return processConfig; } // Get set definition of records. Optional because we can get "set" name from user. if ((obj = getJsonObject(jobj, Constants.SET)) != null) { if (obj instanceof String) { metadataColumnDefs.add(new ColumnDefinition(Constants.SET, obj.toString(), true, true, "string", "string", null, -1, -1, null, null)); } else { metadataColumnDefs.add(getColumnDefs((JSONObject) obj, Constants.SET)); } } // Get bin column definitions JSONArray binList; if ((obj = getJsonObject(jobj, Constants.BINLIST)) != null) { // iterator for bins binList = (JSONArray) obj; Iterator<?> i = binList.iterator(); // take each bin from the JSON array separately while (i.hasNext()) { JSONObject binObj = (JSONObject) i.next(); binColumnDefs.add(getColumnDefs(binObj, Constants.BINLIST)); } } else { return processConfig; } log.info(String.format("Number of columns: %d(metadata) + %d(bins)", (metadataColumnDefs.size()), binColumnDefs.size())); processConfig = true; } catch (IOException ie) { log.error("File:" + Utils.getFileName(file.getName()) + " Config i/o Error: " + ie.toString()); if (log.isDebugEnabled()) { ie.printStackTrace(); } } catch (ParseException pe) { log.error("File:" + Utils.getFileName(file.getName()) + " Config parsing Error: " + pe.toString()); if (log.isDebugEnabled()) { pe.printStackTrace(); } } catch (Exception e) { log.error("File:" + Utils.getFileName(file.getName()) + " Config unknown Error: " + e.toString()); if (log.isDebugEnabled()) { e.printStackTrace(); } } return processConfig; }
From source file:com.viettel.dms.download.DownloadFile.java
/** * Download file su dung httpclient request * @param url/* w w w. j ava 2s. c o m*/ * @param output * @param tmpDir */ public static void downloadWithHTTPClient(String url, File output, File tmpDir) { File tmp = null; HttpClient httpclient = new DefaultHttpClient(); BufferedOutputStream os = null; BufferedInputStream is = null; try { VTLog.i("DownloadDB", "Downloading url :" + url); tmp = File.createTempFile("download", ".tmp", tmpDir); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { is = new BufferedInputStream(entity.getContent()); os = new BufferedOutputStream(new FileOutputStream(tmp)); copyStream(is, os); tmp.renameTo(output); tmp = null; httpclient.getConnectionManager().shutdown(); } } catch (IOException e) { VTLog.e("DownloadDB", "Loi download file db " + e.toString()); VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e)); throw new RuntimeException(e); } catch (Exception e) { VTLog.e("DownloadDB", "Loi download file db " + e.toString()); VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e)); throw new RuntimeException(e); } finally { if (tmp != null) { try { tmp.delete(); tmp = null; } catch (Exception ignore) { ; } } if (is != null) { try { is.close(); is = null; } catch (Exception ignore) { ; } } if (os != null) { try { os.close(); os = null; } catch (Exception ignore) { ; } } } }
From source file:com.android.providers.downloads.OmaDownload.java
/** * This method parses an xml file into a provided component. @param ddUrl the URL of the download descriptor file @param file the file containing the XML to be parsed @param component the component to which the parsed xml data should be added @return the status code (success or other error code) */// ww w. j a v a 2 s. c om protected static int parseXml(URL ddUrl, File file, OmaDescription component) { BufferedReader sReader = null; //Initialize the status code in the component component.setStatusCode(OmaStatusHandler.SUCCESS); if (file == null || ddUrl == null) { component.setStatusCode(OmaStatusHandler.INVALID_DESCRIPTOR); } else { try { sReader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Log.e("@M_" + Constants.LOG_OMA_DL, e.toString()); } try { Xml.parse(sReader, OMADL_INSTANCE.new DDHandler(ddUrl, component)); } catch (IOException e) { // TODO Auto-generated catch block Log.e("@M_" + Constants.LOG_OMA_DL, e.toString()); } catch (SAXException e) { // TODO Auto-generated catch block Log.e("@M_" + Constants.LOG_OMA_DL, e.toString()); component.setStatusCode(OmaStatusHandler.INVALID_DESCRIPTOR); //parse install notify url String strLine; try { sReader = new BufferedReader(new FileReader(file)); while ((strLine = sReader.readLine()) != null) { strLine = strLine.trim(); StringBuffer strBuffer = new StringBuffer(strLine); String startTag = "<installNotifyURI>"; String endTag = "</installNotifyURI>"; int startTagPos = strBuffer.lastIndexOf(startTag); int endTagPos = strBuffer.lastIndexOf(endTag); if (startTagPos != -1 && endTagPos != -1) { strLine = strLine.substring(startTagPos + startTag.length(), endTagPos); Log.d("@M_" + Constants.LOG_OMA_DL, "install notify URI: " + strLine); URL url = new URL(strLine); component.setInstallNotifyUrl(url); break; } } } catch (IOException e1) { // TODO Auto-generated catch block Log.e("@M_" + Constants.LOG_OMA_DL, e1.toString()); } } } return component.getStatusCode(); }
From source file:io.agi.framework.persistence.PersistenceUtil.java
public static boolean SaveSubtree(String entityName, String type, String path) { boolean success = false; String subtree = ExportSubtree(entityName, type); File file = new File(path); try {//from ww w . ja va 2 s. c om FileUtils.writeStringToFile(file, subtree); file.setWritable(true, false); success = true; } catch (IOException e) { _logger.error("Unable to save subtree for entity: " + entityName); _logger.error(e.toString(), e); } return success; }
From source file:com.alibaba.akita.io.HttpInvoker.java
/** * version 1 remoteimageview download impl, use InputStream to decode. * @param imgUrl//from www . j av a 2s. c o m * @param inSampleSize * @return * @throws AkServerStatusException * @throws AkInvokeException */ public static Bitmap getImageFromUrl(String imgUrl, int inSampleSize) throws AkServerStatusException, AkInvokeException { Log.v(TAG, "getImageFromUrl:" + imgUrl); Bitmap bitmap = null; for (int cnt = 0; cnt < NUM_RETRIES; cnt++) { try { HttpGet request = new HttpGet(imgUrl); HttpResponse response = client.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_ACCEPTED) { HttpEntity resEntity = response.getEntity(); try { BitmapFactory.Options options = new BitmapFactory.Options(); if (inSampleSize > 0 && inSampleSize < 10) { options.inSampleSize = inSampleSize; } else { options.inSampleSize = 0; } InputStream inputStream = resEntity.getContent(); // return BitmapFactory.decodeStream(inputStream); // Bug on slow connections, fixed in future release. bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream), null, options); } catch (Exception e) { e.printStackTrace(); //TODO no op // no op } break; } else { HttpEntity resEntity = response.getEntity(); throw new AkServerStatusException(response.getStatusLine().getStatusCode(), EntityUtils.toString(resEntity, CHARSET)); } } catch (ClientProtocolException cpe) { Log.e(TAG, cpe.toString(), cpe); throw new AkInvokeException(AkInvokeException.CODE_HTTP_PROTOCOL_ERROR, cpe.toString(), cpe); } catch (IOException ioe) { Log.e(TAG, ioe.toString(), ioe); throw new AkInvokeException(AkInvokeException.CODE_CONNECTION_ERROR, ioe.toString(), ioe); } } return bitmap; }