List of usage examples for java.io RandomAccessFile getChannel
public final FileChannel getChannel()
From source file:com.amazonaws.eclipse.core.regions.RegionUtils.java
/** * Set the length of the file given to 0 bytes. *//*from w w w .j a v a2 s . c o m*/ private static void truncateFile(File file) throws FileNotFoundException, IOException { if (file.exists()) { RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.getChannel().truncate(0); raf.close(); } }
From source file:com.linkedin.pinot.perf.ForwardIndexReaderBenchmark.java
public static void multiValuedReadBenchMarkV1(File file, int numDocs, int totalNumValues, int maxEntriesPerDoc, int columnSizeInBits) throws Exception { System.out.println("******************************************************************"); System.out.println("Analyzing " + file.getName() + " numDocs:" + numDocs + ", totalNumValues:" + totalNumValues + ", maxEntriesPerDoc:" + maxEntriesPerDoc + ", numBits:" + columnSizeInBits); long start, end; boolean readFile = true; boolean randomRead = true; boolean contextualRead = true; boolean signed = false; boolean isMmap = false; PinotDataBuffer heapBuffer = PinotDataBuffer.fromFile(file, ReadMode.mmap, FileChannel.MapMode.READ_ONLY, "benchmarking"); BaseSingleColumnMultiValueReader reader = new com.linkedin.pinot.core.io.reader.impl.v1.FixedBitMultiValueReader( heapBuffer, numDocs, totalNumValues, columnSizeInBits, signed); int[] intArray = new int[maxEntriesPerDoc]; File outfile = new File("/tmp/" + file.getName() + ".raw"); FileWriter fw = new FileWriter(outfile); for (int i = 0; i < numDocs; i++) { int length = reader.getIntArray(i, intArray); StringBuilder sb = new StringBuilder(); String delim = ""; for (int j = 0; j < length; j++) { sb.append(delim);/*from w w w. j a v a2 s. c om*/ sb.append(intArray[j]); delim = ","; } fw.write(sb.toString()); fw.write("\n"); } fw.close(); // sequential read if (readFile) { DescriptiveStatistics stats = new DescriptiveStatistics(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); ByteBuffer buffer = ByteBuffer.allocateDirect((int) file.length()); raf.getChannel().read(buffer); for (int run = 0; run < MAX_RUNS; run++) { long length = file.length(); start = System.currentTimeMillis(); for (int i = 0; i < length; i++) { byte b = buffer.get(i); } end = System.currentTimeMillis(); stats.addValue((end - start)); } System.out.println("v1 multi value read bytes stats for " + file.getName()); System.out.println( stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues())); raf.close(); } if (randomRead) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (int run = 0; run < MAX_RUNS; run++) { start = System.currentTimeMillis(); for (int i = 0; i < numDocs; i++) { int length = reader.getIntArray(i, intArray); } end = System.currentTimeMillis(); stats.addValue((end - start)); } System.out.println("v1 multi value sequential read one stats for " + file.getName()); System.out.println( stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues())); } if (contextualRead) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (int run = 0; run < MAX_RUNS; run++) { MultiValueReaderContext context = (MultiValueReaderContext) reader.createContext(); start = System.currentTimeMillis(); for (int i = 0; i < numDocs; i++) { int length = reader.getIntArray(i, intArray, context); } end = System.currentTimeMillis(); // System.out.println("RUN:" + run + "Time:" + (end-start)); stats.addValue((end - start)); } System.out.println("v1 multi value sequential read one with context stats for " + file.getName()); System.out.println( stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues())); } reader.close(); heapBuffer.close(); System.out.println("******************************************************************"); }
From source file:org.geoserver.rest.util.RESTUtils.java
/** * Reads content from the body of a request and writes it to a file. * /*from ww w . j a v a 2 s . c o m*/ * @param fileName The name of the file to write out. * @param directory The directory to write the file to. * @param deleteDirectoryContent Delete directory content if the file already exists. * @param request The request. * * @return The file object representing the newly written file. * * @throws IOException Any I/O errors that occur. * * TODO: move this to IOUtils. */ public static File handleBinUpload(String fileName, File directory, boolean deleteDirectoryContent, Request request) throws IOException { final File newFile = new File(directory, fileName); if (newFile.exists()) { if (deleteDirectoryContent) { FileUtils.cleanDirectory(directory); } else { // delete the file, otherwise replacing it with a smaller one will leave bytes at the end newFile.delete(); } } final ReadableByteChannel source = request.getEntity().getChannel(); RandomAccessFile raf = null; FileChannel outputChannel = null; try { raf = new RandomAccessFile(newFile, "rw"); outputChannel = raf.getChannel(); IOUtils.copyChannel(1024 * 1024, source, outputChannel); } finally { try { if (raf != null) { raf.close(); } } finally { IOUtils.closeQuietly(source); IOUtils.closeQuietly(outputChannel); } } return newFile; }
From source file:org.apache.hadoop.dfs.TestDatanodeBlockScanner.java
public static boolean corruptReplica(String blockName, int replica) throws IOException { Random random = new Random(); File baseDir = new File(System.getProperty("test.build.data"), "dfs/data"); boolean corrupted = false; for (int i = replica * 2; i < replica * 2 + 2; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + blockName); if (blockFile.exists()) { // Corrupt replica by writing random bytes into replica RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); FileChannel channel = raFile.getChannel(); String badString = "BADBAD"; int rand = random.nextInt((int) channel.size() / 2); raFile.seek(rand);/*w w w .j a v a2s .co m*/ raFile.write(badString.getBytes()); raFile.close(); corrupted = true; } } return corrupted; }
From source file:org.opencastproject.util.IoSupport.java
private static Effect0 aquireLock(File file) { try {//w w w . j ava 2 s .com final RandomAccessFile raf = new RandomAccessFile(file, "rw"); final FileLock lock = raf.getChannel().lock(); return new Effect0() { @Override protected void run() { try { lock.release(); } catch (IOException ignore) { } IoSupport.closeQuietly(raf); } }; } catch (Exception e) { throw new RuntimeException("Error aquiring lock for " + file.getAbsolutePath(), e); } }
From source file:Main.java
public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) { try {/*from w ww . j a v a 2 s . co m*/ // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. int index = tempFileName.lastIndexOf("."); if (index != -1) tempFileName = tempFileName.substring(0, index); File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); Config type = srcBitmap.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height); srcBitmap.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. srcBitmap.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. srcBitmap = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary srcBitmap.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temp file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return srcBitmap; }
From source file:org.noroomattheinn.utils.Utils.java
/** * Lock a global resource between instances of Java running in different * processes. This can be used to ensure that only one instance of an * application is running. The lock is obtained by locking a file in * the file system that is agreed upon by all parties involved in the * locking process./*from w w w . ja v a 2 s . c o m*/ * @param lockName The file to lock * @param folder The folder containing the file to lock * @return true if this process obtained the lock * false if the lock is already held by another process */ public static boolean obtainLock(String lockName, File folder) { File lockFile = new File(folder, lockName); try { RandomAccessFile raf = new RandomAccessFile(lockFile, "rw"); FileLock instanceLock = raf.getChannel().tryLock(); return instanceLock != null; } catch (IOException ex) { Tesla.logger.severe(ex.getMessage()); return false; } }
From source file:xbird.util.nio.RemoteMemoryMappedFile.java
public static void handleResponse(final ReadRequestMessage request, final ProtocolEncoderOutput out, final ConcurrentMap<String, FileChannel> fdCacheMap) { final String filePath = request.filePath; FileChannel fileChannel = fdCacheMap.get(filePath); if (fileChannel == null) { File file = new File(filePath); if (!file.exists()) { throw new IllegalStateException("file not exists: " + filePath); }//from w w w . ja v a 2s. co m final RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } fileChannel = raf.getChannel(); fdCacheMap.put(filePath, fileChannel); } long count = request.endOffset - request.startOffset; long position = request.startOffset; if (LOG.isDebugEnabled()) { LOG.debug("Transfer " + count + " bytes of file '" + filePath + "' from the offset " + position); } FileRegion fileRegion = new DefaultFileRegion(fileChannel, position, count); out.write(fileRegion); }
From source file:in.bbat.license.LicenseManager.java
public static void cleanupOlderLicenseFiles() { File localFile1 = new File(""); File[] arrayOfFile1 = localFile1.listFiles(new FilenameFilter() { public boolean accept(File paramAnonymousFile, String paramAnonymousString) { return paramAnonymousString.endsWith(".license.lock"); }/*from www . j a v a 2 s. c om*/ }); for (File localFile2 : arrayOfFile1) { FileLock localFileLock = null; RandomAccessFile localRandomAccessFile = null; try { localRandomAccessFile = new RandomAccessFile(localFile2, "rw"); localFileLock = localRandomAccessFile.getChannel().tryLock(); } catch (Exception localException) { localFileLock = null; } if (localFileLock != null) { try { localFileLock.release(); localRandomAccessFile.close(); } catch (IOException localIOException1) { LOG.error("Error releasing an acquired lock. No problem, continuing!"); } String str = null; try { str = FileUtils.readFileToString(localFile2); returnOlderLicense(str); localFile2.delete(); } catch (IOException localIOException2) { LOG.error("Error returning an older license that is no longer valid. No problem, continuing!"); } } } }
From source file:xbird.storage.io.RemoteVarSegments.java
public static void handleResponse(final TrackReadRequestMessage request, final ProtocolEncoderOutput out, final ConcurrentMap<String, FileChannel> fdCacheMap, final ConcurrentMap<String, IDescriptor> directoryCache) throws IOException { final String filePath = request.filePath; final long[] idxs = request.idxs; final int size = idxs.length; // look-up directory final File dataFile = new File(filePath); final long[] offsets = new long[size]; IDescriptor directory = directoryCache.get(filePath); try {/*from w w w . j a v a2s . com*/ if (directory == null) { directory = VarSegments.initDescriptor(dataFile); directoryCache.put(filePath, directory); } for (int i = 0; i < size; i++) { offsets[i] = directory.getRecordAddr(idxs[i]); } } catch (IOException e) { LOG.error(e); throw e; } FileChannel fileChannel = fdCacheMap.get(filePath); if (fileChannel == null) { if (!dataFile.exists()) { throw new IllegalStateException("file not exists: " + filePath); } final RandomAccessFile raf; try { raf = new RandomAccessFile(dataFile, "r"); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } fileChannel = raf.getChannel(); fdCacheMap.put(filePath, fileChannel); } for (int i = 0; i < size; i++) { final long offset = offsets[i]; // get data length final ByteBuffer tmpBuf = ByteBuffer.allocate(4); try { fileChannel.read(tmpBuf, offset); } catch (IOException e) { LOG.error(e); throw e; } tmpBuf.flip(); final int length = tmpBuf.getInt(); tmpBuf.rewind(); IoBuffer ioBuf = IoBuffer.wrap(tmpBuf); out.write(ioBuf); // attempt zero-copy sendfile long position = offset + 4; long count = length; FileRegion fileRegion = new DefaultFileRegion(fileChannel, position, count); out.write(fileRegion); } }