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[], int off, int len) throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

public static String imgCacheRead(Context context, String cacheImgFileName) {
    int len = 1024;
    byte[] buffer = new byte[len];
    try {//from   www. j  a v  a  2 s  .  c o  m
        FileInputStream fis = context.openFileInput(cacheImgFileName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int nrb = fis.read(buffer, 0, len); // read up to len bytes
        while (nrb != -1) {
            baos.write(buffer, 0, nrb);
            nrb = fis.read(buffer, 0, len);
        }
        buffer = baos.toByteArray();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        return new String(buffer, "utf-8");
    } catch (UnsupportedEncodingException e) {

        return null;
    }
}

From source file:Main.java

public static String getFileHeader(String filePath) {
    FileInputStream is = null;
    String value = null;//from w ww  .jav a2  s .  c  om
    try {
        is = new FileInputStream(filePath);
        byte[] b = new byte[3];
        is.read(b, 0, b.length);
        value = bytesToHexString(b);
    } catch (Exception e) {
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return value;
}

From source file:Main.java

/**
 * (java) read from file/*from   ww  w. ja v a2  s. c  om*/
 * 
 * @param path
 * @return
 */
public static String readFile(File file) {
    String str = "";
    FileInputStream in;
    try {
        in = new FileInputStream(file);
        int length = (int) file.length();
        byte[] temp = new byte[length];
        in.read(temp, 0, length);
        str = EncodingUtils.getString(temp, FILE_ENCODING);
        in.close();
    } catch (IOException e) {
        Log.e("", "read error!");

    }
    return str;
}

From source file:Main.java

public static String getSHA(File f, int length) {
    if (!f.isFile() || length <= 0) {
        return null;
    }/* w w w  .  j  a  va 2  s.c o  m*/
    FileInputStream f1;
    byte[] buf = new byte[length];
    int n;
    try {
        f1 = new FileInputStream(f);
        n = f1.read(buf, 0, length);
        f1.close();
    } catch (IOException e) {
        return null;
    }
    return getSHA(buf);
}

From source file:gov.nasa.arc.geocam.talk.UIUtils.java

/**
 * Play the audio of a {@link GeoCamTalkMessage} if available.
 *
 * @param context The activity context to send the intent from.
 * @param msg The {@link GeoCamTalkMessage} to play audio of.
 * @param player An instance of an {@link IAudioPlayer}
 * @param siteAuth The {@link ISiteAuth} to use to get attain audio from if not local
 * @throws ClientProtocolException the client protocol exception
 * @throws AuthenticationFailedException the authentication failed exception
 * @throws IOException Signals that an I/O exception has occurred.
 *///from   w  w w.j  a v a  2 s.c o m
public static void playAudio(Context context, GeoCamTalkMessage msg, IAudioPlayer player, ISiteAuth siteAuth)
        throws ClientProtocolException, AuthenticationFailedException, IOException {

    String audioUrl = msg.getAudioUrl();

    // No audio recorded with message
    if (msg.getAudio() == null && audioUrl == null) {
        return;
    }
    // We have audio, but not locally
    else if (msg.getAudio() == null && audioUrl != null) {
        String localFileName = siteAuth.getAudioFile(audioUrl, null);
        player.startPlaying(localFileName);
        File audioFile = new File(localFileName);
        int length = (int) audioFile.length();
        byte[] audioBytes = new byte[length];

        FileInputStream fis = new FileInputStream(audioFile);
        fis.read(audioBytes, 0, length);
        msg.setAudio(audioBytes);
    }
    player.startPlaying(msg.getAudio());
}

From source file:Main.java

/**
 * Reads the given file, translating {@link IOException} to a
 * {@link RuntimeException} of some sort.
 *
 * @param file {@code non-null;} the file to read
 * @return {@code non-null;} contents of the file
 */// w w  w  . j av a 2 s  . c o  m
public static byte[] readFile(File file) {
    if (!file.exists()) {
        throw new RuntimeException(file + ": file not found");
    }

    if (!file.isFile()) {
        throw new RuntimeException(file + ": not a file");
    }

    if (!file.canRead()) {
        throw new RuntimeException(file + ": file not readable");
    }

    long longLength = file.length();
    int length = (int) longLength;
    if (length != longLength) {
        throw new RuntimeException(file + ": file too long");
    }

    byte[] result = new byte[length];

    try {
        FileInputStream in = new FileInputStream(file);
        int at = 0;
        while (length > 0) {
            int amt = in.read(result, at, length);
            if (amt == -1) {
                throw new RuntimeException(file + ": unexpected EOF");
            }
            at += amt;
            length -= amt;
        }
        in.close();
    } catch (IOException ex) {
        throw new RuntimeException(file + ": trouble reading", ex);
    }

    return result;
}

From source file:Main.java

public static String getFileMD5(File file) {
    if (!file.isFile()) {
        return null;
    }//from w  w  w  .  j a  v  a 2 s.  co m
    MessageDigest digest = null;
    FileInputStream in = null;
    byte buffer[] = new byte[1024];
    int len;
    try {
        digest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        while ((len = in.read(buffer, 0, 1024)) != -1) {
            digest.update(buffer, 0, len);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    BigInteger bigInt = new BigInteger(1, digest.digest());
    return bigInt.toString(16);
}

From source file:Main.java

public static String mergeFlockedFiles(String FilePath) {

    String result = null;/*from  w w w  . ja v  a  2  s .  c  om*/
    try {
        result = java.net.URLDecoder.decode(FilePath, "UTF-8");
    } catch (UnsupportedEncodingException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    String fileName = result.substring(result.lastIndexOf('/') + 1);
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        long leninfile = 0, leng = 0;
        int count = 1, data = 0;
        int bytesRead = 0;
        try {
            File filename = new File(flockedFilesFolder.toString() + "/" + fileName);
            if (filename.exists())
                filename.delete();
            //BUFFER_SIZE = (int) filename.length();
            System.out.println("filename: " + filename);
            FileOutputStream fos = new FileOutputStream(filename, true);

            while (true) {
                File filepart = new File(filename + ".part" + count);
                System.out.println("part filename: " + filepart);
                if (filepart.exists()) {
                    FileInputStream fis = new FileInputStream(filepart);
                    byte fileBytes[] = new byte[(int) filepart.length()];
                    bytesRead = fis.read(fileBytes, 0, (int) filepart.length());
                    assert (bytesRead == fileBytes.length);
                    assert (bytesRead == (int) filepart.length());
                    fos.write(fileBytes);
                    fos.flush();
                    fileBytes = null;
                    fis.close();
                    fis = null;
                    count++;
                    filepart.delete();
                } else
                    break;
            }
            fos.close();
            fos = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return fileName;
}

From source file:Utils.java

public static byte[] readAsByteArray(File file, long length) {
    try {/*w  w  w .ja  v a  2s  . co m*/
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        FileInputStream is = new FileInputStream(file);
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        is.close();
        return bytes;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static byte[] readELFHeadrIndentArray(File libFile) {
    if (libFile != null && libFile.exists()) {
        FileInputStream inputStream = null;
        try {/*from  w  w w  . ja  v a2  s.co  m*/
            inputStream = new FileInputStream(libFile);
            if (inputStream != null) {
                byte[] tempBuffer = new byte[16];
                int count = inputStream.read(tempBuffer, 0, 16);
                if (count == 16) {
                    return tempBuffer;
                } else {
                    if (LOGENABLE) {
                        Log.e("readELFHeadrIndentArray",
                                "Error: e_indent lenght should be 16, but actual is " + count);
                    }
                }
            }
        } catch (Throwable t) {
            if (LOGENABLE) {
                Log.e("readELFHeadrIndentArray", "Error:" + t.toString());
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    return null;
}