List of usage examples for java.io FileInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
public static String loadFileIntoString(File file) { String fString = null;/* ww w . java2s.c om*/ FileInputStream fis = null; try { fis = new FileInputStream(file); int i = (int) file.length(); byte[] b = new byte[i]; fis.read(b); ByteArrayOutputStream bfos = new ByteArrayOutputStream(); bfos.write(b); fString = bfos.toString("UTF-8"); } catch (Exception e) { // TODO Auto-generated catch block log.error(e); } finally { if (fis != null) { try { fis.close(); } catch (IOException ioExc) { log.error(ioExc); } } } return fString; }
From source file:com.rackspacecloud.client.cloudfiles.sample.FilesCopy.java
public static File zipFile(File f) throws IOException { byte[] buf = new byte[1024]; int len;/*from ww w.ja va 2 s. c o m*/ // Create the ZIP file String filenameWithZipExt = f.getName() + ZIPEXTENSION; File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile)); FileInputStream in = new FileInputStream(f); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(f.getName())); // Transfer bytes from the file to the ZIP file while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); out.flush(); in.close(); out.close(); return zippedFile; }
From source file:com.mvdb.etl.actions.ActionUtils.java
private static void zipDir(String origDir, File dirObj, ZipOutputStream zos) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zipDir(origDir, files[i], zos); continue; }//w w w . jav a 2 s.c o m String wAbsolutePath = files[i].getAbsolutePath().substring(origDir.length() + 1, files[i].getAbsolutePath().length()); FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); zos.putNextEntry(new ZipEntry(wAbsolutePath)); int len; while ((len = in.read(tmpBuf)) > 0) { zos.write(tmpBuf, 0, len); } zos.closeEntry(); in.close(); } }
From source file:net.sf.keystore_explorer.crypto.signing.MidletSigner.java
private static byte[] signJarDigest(File jarFile, RSAPrivateKey privateKey) throws CryptoException { // Create a SHA-1 signature for the supplied JAR file FileInputStream fis = null; try {//from ww w . j a va 2s.c o m Signature signature = Signature.getInstance(SignatureType.SHA1_RSA.jce()); signature.initSign(privateKey); fis = new FileInputStream(jarFile); byte buffer[] = new byte[1024]; int read = 0; while ((read = fis.read(buffer)) != -1) { signature.update(buffer, 0, read); } return signature.sign(); } catch (IOException ex) { throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex); } catch (GeneralSecurityException ex) { throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex); } finally { IOUtils.closeQuietly(fis); } }
From source file:Main.java
/** * Copy a file into a dstPath directory. * The output filename can be provided.//from w ww .j a v a2 s.c om * The output file is not overriden if it is already exist. * @param context the context * @param sourceFile the file source path * @param dstDirPath the dst path * @param outputFilename optional the output filename * @return the downloads file path if the file exists or has been properly saved */ public static String saveFileInto(Context context, File sourceFile, String dstDirPath, String outputFilename) { // sanity check if ((null == sourceFile) || (null == dstDirPath)) { return null; } // defines another name for the external media String dstFileName; // build a filename is not provided if (null == outputFilename) { // extract the file extension from the uri int dotPos = sourceFile.getName().lastIndexOf("."); String fileExt = ""; if (dotPos > 0) { fileExt = sourceFile.getName().substring(dotPos); } dstFileName = "MatrixConsole_" + System.currentTimeMillis() + fileExt; } else { dstFileName = outputFilename; } File dstDir = Environment.getExternalStoragePublicDirectory(dstDirPath); if (dstDir != null) { dstDir.mkdirs(); } File dstFile = new File(dstDir, dstFileName); // Copy source file to destination FileInputStream inputStream = null; FileOutputStream outputStream = null; try { // create only the if (!dstFile.exists()) { dstFile.createNewFile(); inputStream = new FileInputStream(sourceFile); outputStream = new FileOutputStream(dstFile); byte[] buffer = new byte[1024 * 10]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } } } catch (Exception e) { dstFile = null; } finally { // Close resources try { if (inputStream != null) inputStream.close(); if (outputStream != null) outputStream.close(); } catch (Exception e) { } } if (null != dstFile) { return dstFile.getAbsolutePath(); } else { return null; } }
From source file:com.globalsight.everest.webapp.pagehandler.tm.management.FileUploadHelper.java
/** * Renames a file from its originalName to a newName. NOTE: * java.io.File.renameTo() has some known bugs that apparently are still * unfixed in 1.3 so it cannot be used. <br> * This implementation simply copies originalName to newName and then * deletes the originalName file.//from w w w. ja v a 2s .com * * @param p_originalName -- * source file name * @param p_newName -- * destination file name */ public static void renameFile(File p_originalName, File p_newName) throws IOException { FileInputStream fis = new FileInputStream(p_originalName); FileOutputStream fos = new FileOutputStream(p_newName); byte buffer[] = new byte[2056]; boolean keepReading = true; int numBytesRead = -1; while (keepReading) { numBytesRead = fis.read(buffer); if (numBytesRead == -1) keepReading = false; else fos.write(buffer, 0, numBytesRead); } fis.close(); fos.close(); p_originalName.delete(); }
From source file:com.easysoft.build.utils.PatchUtil.java
/** * ?SQL/*from www .j a va 2s . c o m*/ * @param sqlFile * @return * @throws java.io.IOException */ public static StringBuilder readFile(File file, String encoding) throws IOException { if (encoding == null) encoding = "UTF-8"; StringBuilder sb = new StringBuilder(); if (!file.exists() || !file.isFile()) return sb; final int buffer = 1024; byte[] bs = new byte[buffer]; FileInputStream in = new FileInputStream(file); int len = in.read(bs); int total = 0; while (len != -1) { total += len; byte[] tmp = new byte[total + buffer]; System.arraycopy(bs, 0, tmp, 0, total); bs = tmp; len = in.read(bs, total, buffer); } sb.append(new String(bs, 0, total, encoding)); in.close(); return sb; }
From source file:models.Attachment.java
/** * Moves a file to the Upload Directory. * * This method is used to move a file stored in temporary directory by * PlayFramework to the Upload Directory managed by Yobi. * * @param file/*from ww w . j a va 2 s . c om*/ * @return SHA1 hash of the file * @throws NoSuchAlgorithmException * @throws IOException */ private static String moveFileIntoUploadDirectory(File file) throws NoSuchAlgorithmException, IOException { // Compute sha1 checksum. MessageDigest algorithm = MessageDigest.getInstance("SHA1"); byte buf[] = new byte[10240]; FileInputStream fis = new FileInputStream(file); for (int size = 0; size >= 0; size = fis.read(buf)) { algorithm.update(buf, 0, size); } Formatter formatter = new Formatter(); for (byte b : algorithm.digest()) { formatter.format("%02x", b); } String hash = formatter.toString(); formatter.close(); fis.close(); // Store the file. // Before do that, create upload directory if it doesn't exist. File uploads = new File(uploadDirectory); uploads.mkdirs(); if (!uploads.isDirectory()) { throw new NotDirectoryException("'" + file.getAbsolutePath() + "' is not a directory."); } File attachedFile = new File(uploadDirectory, hash); boolean isMoved = file.renameTo(attachedFile); if (!isMoved) { FileUtils.copyFile(file, attachedFile); file.delete(); } // Close all resources. return hash; }
From source file:Main.java
@Nullable private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) { if (uri == null) return null; FileInputStream input = null; FileOutputStream output = null; try {/*from ww w . ja v a2 s . co m*/ ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r"); FileDescriptor fd = pfd.getFileDescriptor(); input = new FileInputStream(fd); String tempFilename = getTempFilename(context); output = new FileOutputStream(tempFilename); int read; byte[] bytes = new byte[4096]; while ((read = input.read(bytes)) != -1) { output.write(bytes, 0, read); } return new File(tempFilename); } catch (IOException ignored) { // Nothing we can do } finally { closeSilently(input); closeSilently(output); } return null; }
From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java
public static String saveAttachment(String md5, String group, String name) throws IOException { String outDir = UsenetConstants.EXTERNALSTORAGE + "/downloads"; File dirOutDir = new File(outDir); if (!dirOutDir.exists()) dirOutDir.mkdirs();/*from w w w .j av a 2s.c o m*/ name = sanitizeFileName(name); String origFilePath = UsenetConstants.EXTERNALSTORAGE + "/" + UsenetConstants.APPNAME + "/" + UsenetConstants.ATTACHMENTSDIR + "/" + group + "/" + md5; String destFilePath = outDir + "/" + name; FileInputStream in = new FileInputStream(origFilePath); FileOutputStream out = new FileOutputStream(destFilePath); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); return destFilePath; }