List of usage examples for java.io RandomAccessFile readFully
public final void readFully(byte b[]) throws IOException
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }//w w w. j a v a 2 s .co m File file = new File(fileName); if (!file.exists()) { //WLog.i(FileUtils.class, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } if (offset < 0) { //WLog.e(FileUtils.class, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { //WLog.e(FileUtils.class, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { //WLog.e(FileUtils.class, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { // WLog.e(FileUtils.class, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }// w w w . j av a 2s. com File file = new File(fileName); if (!file.exists()) { Log.i(TAG, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len)); if (offset < 0) { Log.e(TAG, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { Log.e(TAG, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { Log.e(TAG, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { Log.e(TAG, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:Main.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }//from w w w. j a va2 s .c o m File file = new File(fileName); if (!file.exists()) { Log.i(TAG, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len)); if (offset < 0) { Log.e(TAG, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { Log.e(TAG, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { Log.e(TAG, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; // in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { Log.e(TAG, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:org.apache.hadoop.hdfs.server.namenode.FSImageUtil.java
public static boolean checkFileFormat(RandomAccessFile file) throws IOException { if (file.length() < Loader.MINIMUM_FILE_LENGTH) return false; byte[] magic = new byte[MAGIC_HEADER.length]; file.readFully(magic); if (!Arrays.equals(MAGIC_HEADER, magic)) return false; return true;// www . ja va 2 s.com }
From source file:com.parse.ParseKeyValueCache.java
static String loadFromKeyValueCache(final String key, final long maxAgeMilliseconds) { synchronized (MUTEX_IO) { File file = getKeyValueCacheFile(key); if (file == null) { return null; }/* www.ja v a 2 s .com*/ Date now = new Date(); long oldestAcceptableAge = Math.max(0, now.getTime() - maxAgeMilliseconds); if (getKeyValueCacheAge(file) < oldestAcceptableAge) { return null; } // Update mtime to make the LRU work file.setLastModified(now.getTime()); try { RandomAccessFile f = new RandomAccessFile(file, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes, "UTF-8"); } catch (IOException e) { PLog.e(TAG, "error reading from cache", e); return null; } } }
From source file:Main.java
public static byte[] readFile(File file) throws IOException { // Open file/* w ww. j a v a 2s . c om*/ RandomAccessFile f = new RandomAccessFile(file, "r"); try { // Get and check length long longlength = f.length(); int length = (int) longlength; if (length != longlength) throw new IOException("File size >= 2 GB"); // Read file and return data byte[] data = new byte[length]; f.readFully(data); return data; } finally { f.close(); } }
From source file:eu.delving.metadata.Hasher.java
public static String quickHash(File file) throws IOException { Hasher hasher = new Hasher(); RandomAccessFile raf = new RandomAccessFile(file, "r"); byte[] chunk = new byte[QUICK_SAMPLE_SIZE]; long length = raf.length() - chunk.length; long step = length / QUICK_SAMPLES; for (int walk = 0; walk < QUICK_SAMPLES; walk++) { raf.seek(step * walk);/*from ww w. j av a 2 s .com*/ raf.readFully(chunk); hasher.update(chunk, chunk.length); } raf.close(); return hasher.getHashString().substring(4, 14); }
From source file:com.shopgun.android.sdk.utils.ExternalClientIdStore.java
public static String getCid(ShopGun sgn) { // First try SharedPrefs String cid = sgn.getSettings().getClientId(); if (cid != null) { return cid; }/* ww w . j a va 2s. co m*/ // Then try external storage File cidFile = getCidFile(sgn.getContext()); if (cidFile == null) { return null; } RandomAccessFile f = null; try { f = new RandomAccessFile(cidFile, "r"); // Get and check length long longlength = f.length(); int length = (int) longlength; if (length != longlength) return null; // Read file and return data byte[] data = new byte[length]; f.readFully(data); return new String(data); } catch (Exception e) { // Ignore } finally { try { f.close(); } catch (Exception e) { // Ignore } // Cleanup the cid file, we won't need it any more deleteCidFile(sgn.getContext()); } return null; }
From source file:com.geekandroid.sdk.pay.utils.Util.java
public static byte[] readFromFile(String fileName, int offset, int len) { if (fileName == null) { return null; }/* w ww. jav a 2 s . c o m*/ File file = new File(fileName); if (!file.exists()) { Log.i(TAG, "readFromFile: file not found"); return null; } if (len == -1) { len = (int) file.length(); } Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len)); if (offset < 0) { Log.e(TAG, "readFromFile invalid offset:" + offset); return null; } if (len <= 0) { Log.e(TAG, "readFromFile invalid len:" + len); return null; } if (offset + len > (int) file.length()) { Log.e(TAG, "readFromFile invalid file len:" + file.length()); return null; } byte[] b = null; try { RandomAccessFile in = new RandomAccessFile(fileName, "r"); b = new byte[len]; // ?? in.seek(offset); in.readFully(b); in.close(); } catch (Exception e) { Log.e(TAG, "readFromFile : errMsg = " + e.getMessage()); e.printStackTrace(); } return b; }
From source file:org.apache.hadoop.hdfs.server.namenode.FSImageUtil.java
public static FileSummary loadSummary(RandomAccessFile file) throws IOException { final int FILE_LENGTH_FIELD_SIZE = 4; long fileLength = file.length(); file.seek(fileLength - FILE_LENGTH_FIELD_SIZE); int summaryLength = file.readInt(); if (summaryLength <= 0) { throw new IOException("Negative length of the file"); }//from w w w. jav a2 s . c om file.seek(fileLength - FILE_LENGTH_FIELD_SIZE - summaryLength); byte[] summaryBytes = new byte[summaryLength]; file.readFully(summaryBytes); FileSummary summary = FileSummary.parseDelimitedFrom(new ByteArrayInputStream(summaryBytes)); if (summary.getOndiskVersion() != FILE_VERSION) { throw new IOException("Unsupported file version " + summary.getOndiskVersion()); } if (!NameNodeLayoutVersion.supports(Feature.PROTOBUF_FORMAT, summary.getLayoutVersion())) { throw new IOException("Unsupported layout version " + summary.getLayoutVersion()); } return summary; }