List of usage examples for java.io RandomAccessFile getChannel
public final FileChannel getChannel()
From source file:com.notifier.desktop.Main.java
private static boolean getExclusiveExecutionLock() throws IOException { File lockFile = new File(OperatingSystems.getWorkDirectory(), Application.ARTIFACT_ID + ".lock"); Files.createParentDirs(lockFile); lockFile.createNewFile();// w ww .j av a2 s .co m final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw"); final FileChannel fileChannel = randomAccessFile.getChannel(); final FileLock fileLock = fileChannel.tryLock(); if (fileLock == null) { Closeables.closeQuietly(fileChannel); Closeables.closeQuietly(randomAccessFile); return false; } Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { fileLock.release(); } catch (IOException e) { System.err.println("Error releasing file lock"); e.printStackTrace(System.err); } finally { Closeables.closeQuietly(fileChannel); Closeables.closeQuietly(randomAccessFile); } } }); return true; }
From source file:com.wabacus.util.FileLockTools.java
public static Object lock(String lockfile) { RandomAccessFile raf = null; FileChannel fc = null;//w w w . j av a 2 s . c o m boolean islocked = true; try { raf = new RandomAccessFile(new File(lockfile), "rw"); fc = raf.getChannel(); FileLock fl = fc.tryLock(); if (fl != null && fl.isValid()) { Map mapResources = new HashMap(); mapResources.put("RAF", raf); mapResources.put("FC", fc); mapResources.put("FL", fl); return mapResources; } else { islocked = false; return null; } } catch (Exception e) { log.error("?" + lockfile + "?", e); return null; } finally { if (!islocked) { release(fc); release(raf); } } }
From source file:com.github.neoio.nio.util.NIOUtils.java
public static FileLock tryLock(File file) { FileLock toReturn = null;//from www . j a v a 2 s . co m try { RandomAccessFile raf = new RandomAccessFile(file, "rw"); try { FileChannel channel = raf.getChannel(); toReturn = channel.tryLock(); raf.writeBytes("lock file for: " + ManagementFactory.getRuntimeMXBean().getName()); } finally { if (toReturn == null) raf.close(); } } catch (OverlappingFileLockException e) { toReturn = null; } catch (FileNotFoundException e) { toReturn = null; } catch (IOException e) { toReturn = null; } return toReturn; }
From source file:fusejext2.FuseJExt2.java
private static void setupBlockDevice() { try {//www . j a va 2 s .co m RandomAccessFile blockDevFile = new RandomAccessFile(filename, "rw"); blockDev = blockDevFile.getChannel(); } catch (FileNotFoundException e) { System.out.println("Can't open block device or file " + filename); System.out.println(e.getMessage()); System.exit(1); } }
From source file:org.uva.itast.blended.omr.pages.PDFPageImage.java
/** * @param filePath2//from ww w . j av a 2 s. c o m * @throws IOException */ public static PDFFile loadPDFFile(File inputpath) throws IOException { RandomAccessFile raf = new RandomAccessFile(inputpath, "r"); //se carga la imagen pdf para leerla FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); return pdffile; }
From source file:com.amazonaws.services.s3.internal.FileLocks.java
/** * Acquires an exclusive lock on the specified file, creating the file as * necessary. Caller of this method is responsible to call the * {@link #unlock(File)} method to prevent release leakage. * /*from w ww . j a va 2 s .co m*/ * @return true if the locking is successful; false otherwise. * * @throws FileLockException if we failed to lock the file */ public static boolean lock(File file) { synchronized (lockedFiles) { if (lockedFiles.containsKey(file)) return false; // already locked } FileLock lock = null; RandomAccessFile raf = null; try { // Note if the file does not already exist then an attempt will be // made to create it because of the use of "rw". raf = new RandomAccessFile(file, "rw"); FileChannel channel = raf.getChannel(); if (EXTERNAL_LOCK) lock = channel.lock(); } catch (Exception e) { IOUtils.closeQuietly(raf, log); throw new FileLockException(e); } final boolean locked; synchronized (lockedFiles) { RandomAccessFile prev = lockedFiles.put(file, raf); if (prev == null) { locked = true; } else { // race condition: some other thread got locked it before this locked = false; lockedFiles.put(file, prev); // put it back } } if (locked) { if (log.isDebugEnabled()) log.debug("Locked file " + file + " with " + lock); } else { IOUtils.closeQuietly(raf, log); } return locked; }
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); raf.close();/*from w w w .j a va 2 s .c o m*/ 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:org.ednovo.gooru.application.util.GooruImageUtil.java
public static PDFFile getPDFFile(String pdfPath) { ByteBuffer buf;/* w ww.j a va 2 s . co m*/ PDFFile pdfFile = null; try { File file = new File(pdfPath); @SuppressWarnings("resource") RandomAccessFile accessFile = new RandomAccessFile(file, "r"); FileChannel channel = accessFile.getChannel(); buf = channel.map(MapMode.READ_ONLY, 0, channel.size()); pdfFile = new PDFFile(buf); } catch (Exception e) { LOGGER.error("getPDFFile: " + e); } return pdfFile; }
From source file:com.aliyun.oss.integrationtests.TestUtils.java
public static String genFixedLengthFile(long fixedLength) throws IOException { ensureDirExist(TestBase.UPLOAD_DIR); String filePath = TestBase.UPLOAD_DIR + System.currentTimeMillis(); RandomAccessFile raf = new RandomAccessFile(filePath, "rw"); FileChannel fc = raf.getChannel(); MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, fixedLength); try {/*from w w w .ja v a 2 s .c o m*/ for (int i = 0; i < fixedLength; i++) { mbb.put(pickupAlphabet()); } return filePath; } finally { if (fc != null) { fc.close(); } if (raf != null) { raf.close(); } } }
From source file:com.aliyun.oss.integrationtests.TestUtils.java
public static String genRandomLengthFile() throws IOException { ensureDirExist(TestBase.UPLOAD_DIR); String filePath = TestBase.UPLOAD_DIR + System.currentTimeMillis(); RandomAccessFile raf = new RandomAccessFile(filePath, "rw"); FileChannel fc = raf.getChannel(); long fileLength = rand.nextInt(MAX_RANDOM_LENGTH); MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, fileLength); try {//from w ww .j a va 2 s. c o m for (int i = 0; i < fileLength; i++) { mbb.put(pickupAlphabet()); } return filePath; } finally { if (fc != null) { fc.close(); } if (raf != null) { raf.close(); } } }