List of usage examples for java.io File length
public long length()
From source file:JALPTest.java
/** * Creates a Producer using the given command line params. * * @param xml the ApplicationMetadataXML * @param socketPath a String which is the path to the socket * @param privateKeyPath a String which is the path to the private key in DER format * @param publicKeyPath a String which is the path to the public key in DER format * @param certPath a String which is the path to the certificate * @param hasDigest a Boolean, true to set a digest method in the producer * @return the created Producer//ww w .j av a2 s. c o m * @throws Exception */ private static Producer createProducer(ApplicationMetadataXML xml, String socketPath, String privateKeyPath, String publicKeyPath, String certPath, Boolean hasDigest) throws Exception { Producer producer = new Producer(xml); producer.setSocketFile(socketPath); if (privateKeyPath != null && !"".equals(privateKeyPath)) { File privateKeyFile = new File(privateKeyPath); DataInputStream privateDis = new DataInputStream(new FileInputStream(privateKeyFile)); byte[] privateKeyBytes = new byte[(int) privateKeyFile.length()]; privateDis.readFully(privateKeyBytes); privateDis.close(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); KeySpec privateKs = new PKCS8EncodedKeySpec(privateKeyBytes); PrivateKey privateKey = keyFactory.generatePrivate(privateKs); File publicKeyFile = new File(publicKeyPath); DataInputStream publicDis = new DataInputStream(new FileInputStream(publicKeyFile)); byte[] publicKeyBytes = new byte[(int) publicKeyFile.length()]; publicDis.readFully(publicKeyBytes); publicDis.close(); KeySpec publicKs = new X509EncodedKeySpec(publicKeyBytes); PublicKey publicKey = keyFactory.generatePublic(publicKs); producer.setPrivateKey(privateKey); producer.setPublicKey(publicKey); } if (certPath != null && !"".equals(certPath)) { InputStream inputStream = new FileInputStream(certPath); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream); inputStream.close(); producer.setCertificate(cert); } if (hasDigest) { producer.setDigestMethod(DMType.SHA256); } return producer; }
From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.FileUtil.java
/** * Formats file size provided in bytes properly into Kilobytes, Megabytes and Gigabytes * * @param file File/*from w w w .j av a 2s. c o m*/ * @return formatted size */ public static String getFormattedFileSize(final File file) { return getFormattedFileSize(file.length()); }
From source file:io.github.retz.executor.FileManager.java
private static boolean needsDecompression(File file, String dir) throws IOException, ArchiveException { // To use autodetect feature of ArchiveStreamFactory, input stream must be wrapped // with BufferedInputStream. From commons-compress mailing list. LOG.debug("loading file .... {} as {}", file, file.getPath()); boolean needsDecompression = false; try (ArchiveInputStream ain = createAIS(file)) { while (ain != null) { ArchiveEntry entry = ain.getNextEntry(); if (entry == null) break; // LOG.debug("name: {} size:{} dir: {}", entry.getName(), entry.getSize(), entry.isDirectory()); File f = new File(FilenameUtils.concat(dir, entry.getName())); // LOG.debug("{} exists: {}, size:{}, dir:{}", f, f.exists(), f.length(), f.isDirectory()); if (f.isDirectory()) { continue; } else if (!f.exists() || entry.getSize() != f.length()) { LOG.info("File {} differs: seems not decompressed before", f); needsDecompression = true; break; }/* w w w.jav a2s . com*/ } } catch (ArchiveException e) { needsDecompression = true; } return needsDecompression; }
From source file:com.bonsai.btcreceive.HDReceiver.java
public static JSONObject deserialize(File dir, String prefix) throws IOException, JSONException { String path = persistPath(prefix); mLogger.info("restoring HDReceiver from " + path); try {// www .j a v a2s. c o m File file = new File(dir, path); int len = (int) file.length(); BufferedReader br = null; StringBuilder sb = new StringBuilder(); try { String currentLine; br = new BufferedReader(new FileReader(file)); while ((currentLine = br.readLine()) != null) sb.append(currentLine); } finally { try { if (br != null) br.close(); } catch (IOException ex) { String msg = "problem closing file: " + ex.toString(); mLogger.error(msg); throw new RuntimeException(msg); } } String jsonstr = sb.toString(); /* String msg = jsonstr; while (msg.length() > 1024) { String chunk = msg.substring(0, 1024); mLogger.error(chunk); msg = msg.substring(1024); } mLogger.error(msg); */ JSONObject node = new JSONObject(jsonstr); return node; } catch (IOException ex) { mLogger.warn("trouble reading " + path + ": " + ex.toString()); throw ex; } catch (RuntimeException ex) { mLogger.warn("trouble restoring wallet: " + ex.toString()); throw ex; } }
From source file:org.openjira.jira.utils.LoadImageAsync.java
public static Bitmap getImageFromFile(String url) { try {/*from www.jav a 2 s . c o m*/ File root = Environment.getExternalStorageDirectory(); if (root.canWrite()) { File dir = new File(root, "openjiracache"); dir.mkdir(); File file = new File(dir, url.replace("/", "_").replace(":", "-").replace("?", "_").replace("=", "-")); if (file.exists() && file.length() > 0) { FileInputStream is = new FileInputStream(file); Bitmap bm = BitmapFactory.decodeStream(is); // Log.v(LOGTAG, "Loaded file from " + // file.getAbsolutePath()); WeakReference<Bitmap> ref = new WeakReference<Bitmap>(bm); cachedBitmaps.put(url, ref); return bm; } } } catch (Throwable e) { Log.e(LOGTAG, "Could not read file " + e.getMessage()); } return null; }
From source file:com.aurel.track.exchange.docx.exporter.ImageUtil.java
public static byte[] getImageBytes(String filePath) throws IOException { File file = new File(filePath); // Our utility method wants that as a byte array java.io.InputStream is = new java.io.FileInputStream(file); long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. if (length > Integer.MAX_VALUE) { LOGGER.warn("File " + filePath + " too large and will not be added"); is.close();//w ww . j a va 2 s . c o m return null; } else { byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { System.out.println("Could not completely read file " + file.getName()); } is.close(); return bytes; } }
From source file:com.ms.commons.test.classloader.IntlTestURLClassPath.java
private static boolean isOutOfDate(File signature, URL jar, StringBuilder refAst) throws IOException { File jf = FileUtil.convertURLToFile(jar); long len = jf.length(); long mod = jf.lastModified(); String ast = String.valueOf(len) + "/" + String.valueOf(mod); refAst.append(ast);//from ww w . j a v a 2 s . c om if (!signature.exists()) { return true; } String st = FileUtils.readFileToString(signature); return (!StringUtil.trimedIgnoreCaseEquals(st, ast)); }
From source file:de.ingrid.portal.global.UtilsFileHelper.java
/** * Get bytes of file/*from www .ja v a 2 s . co m*/ * * @param file * @return byte array of a file * @throws IOException */ public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } // Close the input stream and return bytes is.close(); return bytes; }
From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java
public static long getFileSizeKB(File file) { long filesize = file.length(); long fileSizeInKB = filesize / 1024; return fileSizeInKB; }
From source file:com.phonegap.DirectoryManager.java
/** * This method will determine the file properties of the file specified * by the filePath. Creates a JSONObject with name, lastModifiedDate and * size properties./* w ww . j a va 2 s . c om*/ * * @param filePath the file to get the properties of * @return a JSONObject with the files properties */ protected static JSONObject getFile(String filePath) { File fp = new File(filePath); JSONObject obj = new JSONObject(); try { obj.put("name", fp.getAbsolutePath()); obj.put("lastModifiedDate", new Date(fp.lastModified()).toString()); obj.put("size", fp.length()); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } return obj; }