List of usage examples for java.io RandomAccessFile close
public void close() throws IOException
From source file:eu.mhutti1.utils.storage.StorageDeviceUtils.java
private static boolean canWrite(File file) { try {/*from w w w . j a v a 2s.co m*/ RandomAccessFile randomAccessFile = new RandomAccessFile(file + "/test.txt", "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); FileLock fileLock = fileChannel.lock(); fileLock.release(); fileChannel.close(); randomAccessFile.close(); return true; } catch (Exception ex) { return false; } }
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; }//from w w w . ja v a2s .c om 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:org.apache.solr.util.FileUtils.java
/** * Copied from Lucene's FSDirectory.fsync(String) * * @param fullFile the File to be synced to disk * @throws IOException if the file could not be synced *///from www .j a v a2 s .c o m public static void sync(File fullFile) throws IOException { if (fullFile == null || !fullFile.exists()) throw new FileNotFoundException("File does not exist " + fullFile); boolean success = false; int retryCount = 0; IOException exc = null; while (!success && retryCount < 5) { retryCount++; RandomAccessFile file = null; try { try { file = new RandomAccessFile(fullFile, "rw"); file.getFD().sync(); success = true; } finally { if (file != null) file.close(); } } catch (IOException ioe) { if (exc == null) exc = ioe; try { // Pause 5 msec Thread.sleep(5); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } } } if (!success) // Throw original exception throw exc; }
From source file:com.linkedin.pinot.perf.ForwardIndexReaderBenchmark.java
public static void singleValuedReadBenchMarkV2(File file, int numDocs, int numBits) throws Exception { boolean signed = false; boolean isMmap = false; long start, end; boolean fullScan = true; boolean batchRead = true; boolean singleRead = true; PinotDataBuffer heapBuffer = PinotDataBuffer.fromFile(file, ReadMode.heap, FileChannel.MapMode.READ_ONLY, "benchmarking"); com.linkedin.pinot.core.io.reader.impl.v2.FixedBitSingleValueReader reader = new com.linkedin.pinot.core.io.reader.impl.v2.FixedBitSingleValueReader( heapBuffer, numDocs, numBits, signed); if (fullScan) { DescriptiveStatistics stats = new DescriptiveStatistics(); ByteBuffer buffer = ByteBuffer.allocateDirect((int) file.length()); RandomAccessFile raf = new RandomAccessFile(file, "r"); raf.getChannel().read(buffer);/*from w ww.j ava 2 s .c o m*/ raf.close(); int[] input = new int[numBits]; int[] output = new int[32]; int numBatches = (numDocs + 31) / 32; for (int run = 0; run < MAX_RUNS; run++) { start = System.currentTimeMillis(); for (int i = 0; i < numBatches; i++) { for (int j = 0; j < numBits; j++) { input[j] = buffer.getInt(i * numBits * 4 + j * 4); } BitPacking.fastunpack(input, 0, output, 0, numBits); } end = System.currentTimeMillis(); stats.addValue((end - start)); } System.out.println(" v2 full scan stats for " + file.getName()); System.out.println( stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues())); } if (singleRead) { DescriptiveStatistics stats = new DescriptiveStatistics(); // sequential read for (int run = 0; run < MAX_RUNS; run++) { start = System.currentTimeMillis(); for (int i = 0; i < numDocs; i++) { int value = reader.getInt(i); } end = System.currentTimeMillis(); stats.addValue((end - start)); } System.out.println(" v2 sequential single read for " + file.getName()); System.out.println( stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues())); } if (batchRead) { DescriptiveStatistics stats = new DescriptiveStatistics(); int batchSize = Math.min(5000, numDocs); int[] output = new int[batchSize]; int[] rowIds = new int[batchSize]; // sequential read for (int run = 0; run < MAX_RUNS; run++) { start = System.currentTimeMillis(); int rowId = 0; while (rowId < numDocs) { int length = Math.min(batchSize, numDocs - rowId); for (int i = 0; i < length; i++) { rowIds[i] = rowId + i; } reader.getIntBatch(rowIds, output, length); rowId = rowId + length; } end = System.currentTimeMillis(); stats.addValue((end - start)); } System.out.println("v2 sequential batch read stats for " + file.getName()); System.out.println( stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues())); } reader.close(); }
From source file:Main.java
public static byte[] readFile(File file) throws IOException { // Open file// w w w. j av a2 s. 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:org.apache.pig.test.TestJobControlCompiler.java
public static POLoad createPOLoadWithSize(long size, LoadFunc loadFunc) throws Exception { File file = File.createTempFile("tempFile", ".tmp"); file.deleteOnExit();// ww w . ja v a 2 s .c o m RandomAccessFile f = new RandomAccessFile(file, "rw"); f.setLength(size); f.close(); loadFunc.setLocation(file.getAbsolutePath(), new org.apache.hadoop.mapreduce.Job(CONF)); FuncSpec funcSpec = new FuncSpec(loadFunc.getClass().getCanonicalName()); POLoad poLoad = new POLoad(new OperatorKey(), loadFunc); poLoad.setLFile(new FileSpec(file.getAbsolutePath(), funcSpec)); poLoad.setPc(new PigContext()); poLoad.setUp(); return poLoad; }
From source file:shootersubdownloader.Shootersubdownloader.java
static String computefilehash(File f) throws Exception { if (!f.exists() || f.length() < 8 * 1024) { return null; }//from www . ja v a 2 s. c o m long l = f.length(); long[] offset = new long[4]; offset[3] = l - 8 * 1024; offset[2] = l / 3; offset[1] = l / 3 * 2; offset[0] = 4 * 1024; byte[] bBuf = new byte[1024 * 4]; RandomAccessFile raf = new RandomAccessFile(f, "r"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { raf.seek(offset[i]); int readlen = raf.read(bBuf, 0, 4 * 1024); String md5 = md5(bBuf); if (sb.length() != 0) { sb.append("%3B"); } sb.append(md5); } raf.close(); return sb.toString(); }
From source file:Main.java
public static String getCPUInfo() { RandomAccessFile reader = null; try {/*from www .j a v a2 s. c o m*/ byte[] bs = new byte[1024]; reader = new RandomAccessFile("/proc/cpuinfo", "r"); reader.read(bs); String ret = new String(bs); int index = ret.indexOf(0); return index != -1 ? ret.substring(0, index) : ret; } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:Main.java
public static boolean saveApk(File apk, String savePath) { FileInputStream in = null;//from w ww . jav a 2s .com RandomAccessFile accessFile = null; try { in = new FileInputStream(apk); byte[] buf = new byte[1024 * 4]; int len; File file = new File(savePath); accessFile = new RandomAccessFile(file, "rw"); FileDescriptor fd = accessFile.getFD(); while ((len = in.read(buf)) != -1) { accessFile.write(buf, 0, len); } fd.sync(); accessFile.close(); in.close(); return true; } catch (Exception e) { e.printStackTrace(); try { if (in != null) { in.close(); } if (accessFile != null) { accessFile.close(); } } catch (IOException e1) { e1.printStackTrace(); } return false; } }
From source file:com.amazonaws.eclipse.core.regions.RegionUtils.java
/** * Set the length of the file given to 0 bytes. */// w w w . ja v a 2 s . c om private static void truncateFile(File file) throws FileNotFoundException, IOException { if (file.exists()) { RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.getChannel().truncate(0); raf.close(); } }