public static byte[] ReadFileFromPath(File file) { // Open file var f = new RandomAccessFile(file, "r"); try { // Get and check length var longlength = f.Length(); var length = (int)longlength; if (length != longlength) { throw new IOException("Tamanho do arquivo excede o permitido!"); } // Read file and return data var data = new byte[length]; f.ReadFully(data); return(data); } catch (Exception ex) { System.Diagnostics.Debug.Write(ex); return(null); } finally { f.Close(); f.Dispose(); } }
public byte[] getAsBinary(string key) { RandomAccessFile raFile = null; bool removeFile = false; try { File file = mCache.get(key); if (!file.Exists()) { return(null); } raFile = new RandomAccessFile(file, "r"); byte[] byteArray = new byte[(int)raFile.Length()]; raFile.Read(byteArray); if (!Utils.isDue(byteArray)) { return(Utils.clearDateInfo(byteArray)); } else { removeFile = true; return(null); } } catch (System.Exception ex) { VPNLog.d("ACache", ex.ToString()); return(null); } finally { if (raFile != null) { try { raFile.Dispose(); } catch (IOException ex) { ex.PrintStackTrace(); } } if (removeFile) { remove(key); } } }