Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

In this page you can find the example usage for java.io FileInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

@SuppressWarnings("unchecked")
public static Parcelable readParcelable(Context context, String fileName, ClassLoader classLoader) {
    Parcelable parcelable = null;/*from w  w w. ja va 2 s. c  o  m*/
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            parcelable = parcel.readParcelable(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        parcelable = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return parcelable;
}

From source file:cn.fql.utility.FileUtility.java

/**
 * read file to byte[] with specified file path
 *
 * @param filePath specified file path/*  ww  w  .  j a v a2s .com*/
 * @return <code>byte[]</code> file content
 */
public static byte[] readFile(String filePath) {
    File infoFile = new File(filePath);
    byte[] result = null;
    if (infoFile.exists()) {
        result = new byte[(int) infoFile.length()];
        try {
            FileInputStream fis = new FileInputStream(infoFile);
            fis.read(result);
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.cisco.dbds.utils.ftp.FTPHandler.java

/**
 * Sftp_put.//  w  w  w .j av a  2 s . com
 *
 * @param host the host
 * @param user the user
 * @param pass the pass
 * @param lpath the lpath
 * @param rpath the rpath
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void sftp_put(String host, String user, String pass, String lpath, String rpath)
        throws IOException {
    File rfile = new File(rpath);
    File lfile = new File(lpath);
    System.out.println("rfile: " + rfile);
    if (!lfile.exists() || lfile.isDirectory()) {
        throw new IOException("Local file must be a regular file: " + lpath);
    }

    Connection ssh = new Connection(host);
    ssh.connect();
    ssh.authenticateWithPassword(user, pass);
    SFTPv3Client sftp = new SFTPv3Client(ssh);
    SFTPv3FileHandle file = sftp.createFileTruncate(rpath);

    long fileOffset = 0;
    byte[] src = new byte[32768];
    int i = 0;
    FileInputStream input = new FileInputStream(lfile);
    while ((i = input.read(src)) != -1) {
        sftp.write(file, fileOffset, src, 0, i);
        fileOffset += i;
    }

    input.close();
    sftp.closeFile(file);
    sftp.close();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static List<Parcelable> readParcelableList(Context context, String fileName, ClassLoader classLoader) {
    List<Parcelable> results = null;
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try {//from w ww  .j  a  v  a  2  s  . co m
        fis = context.openFileInput(fileName);
        if (fis != null) {
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byte[] data = bos.toByteArray();

            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            results = parcel.readArrayList(classLoader);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        results = null;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        if (bos != null)
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return results;
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

static PublicKey loadPublicKey(String file, String algorithm)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Public Key.
    File filePublicKey = new File(file);
    FileInputStream fis = new FileInputStream(file);
    byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    fis.read(encodedPublicKey);
    fis.close();/* w w  w . j a v  a2s.  c  om*/

    // Generate Public Key.
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    return publicKey;
}

From source file:Main.java

private static boolean moveFile(File oldPlace, File newPlace, boolean removeOld) {
    boolean removeNewFile = true;
    Log.i("cr3", "Moving file " + oldPlace.getAbsolutePath() + " to " + newPlace.getAbsolutePath());
    if (!oldPlace.exists()) {
        Log.i("cr3", "File " + oldPlace.getAbsolutePath() + " does not exist!");
        return false;
    }/*from w  ww.j  av a2 s  . co  m*/
    FileOutputStream os = null;
    FileInputStream is = null;
    try {
        if (!newPlace.createNewFile())
            return false; // cannot create file
        os = new FileOutputStream(newPlace);
        is = new FileInputStream(oldPlace);
        byte[] buf = new byte[0x10000];
        for (;;) {
            int bytesRead = is.read(buf);
            if (bytesRead <= 0)
                break;
            os.write(buf, 0, bytesRead);
        }
        removeNewFile = false;
        oldPlace.delete();
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (IOException ee) {
            // ignore
        }
        try {
            if (is != null)
                is.close();
        } catch (IOException ee) {
            // ignore
        }
        if (removeNewFile)
            newPlace.delete();
    }
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

static PrivateKey loadPrivateKey(String file, String algorithm)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Private Key.
    File filePrivateKey = new File(file);
    FileInputStream fis = new FileInputStream(file);
    byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    fis.read(encodedPrivateKey);
    fis.close();/*  w w w .  ja v a  2 s.  c  om*/

    // Generate KeyPair.
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return privateKey;
}

From source file:outfox.dict.contest.util.FileUtils.java

/**
 * /*  w w  w . java  2 s  . co m*/
 * @param file
 * @return
 */
public static byte[] getBytesFromFile(File file) {
    byte[] byteArray = null;
    try {
        if (file == null) {
            return null;
        }
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[4096];
        int n;
        while ((n = in.read(b)) != -1) {
            out.write(b, 0, n);
        }
        byteArray = out.toByteArray();
        in.close();
        out.close();
    } catch (IOException e) {
        LOG.error("FileUtils.getBytesFromFile error...", e);
    }
    return byteArray;
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

private static void zipDirectory(File directory, String name, ZipOutputStream zos) throws IOException {
    // *MUST* append the trailing slash for a ZipEntry to identify an
    // entry as a directory.
    name += "/";/*from  ww w .  ja  v  a  2 s .c  o m*/

    zos.putNextEntry(new ZipEntry(name));
    zos.closeEntry();

    String[] entryList = directory.list();

    for (int i = 0; i < entryList.length; ++i) {
        File f = new File(directory, entryList[i]);

        if (f.isDirectory()) {
            zipDirectory(f, name + f.getName(), zos);
        } else {
            FileInputStream fis = new FileInputStream(f);
            ZipEntry entry = new ZipEntry(name + f.getName());
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesIn = 0;

            zos.putNextEntry(entry);

            while ((bytesIn = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesIn);
            }

            fis.close();
            zos.closeEntry();
        }
    }
}

From source file:Main.java

/**
 * read file to a string//from w ww . j  a va2s .  c  om
 * 
 * @param context
 * @param file
 * @return
 */
public static String loadString(File file) {
    if (null == file || !file.exists()) {
        return "";
    }
    FileInputStream fis = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        fis = new FileInputStream(file);
        int restSize = fis.available();
        int bufSize = restSize > BUF_SIZE ? BUF_SIZE : restSize;
        byte[] buf = new byte[bufSize];
        while (fis.read(buf) != -1) {
            baos.write(buf);
            restSize -= bufSize;

            if (restSize <= 0)
                break;
            if (restSize < bufSize)
                bufSize = restSize;
            buf = new byte[bufSize];
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return baos.toString();
}