List of usage examples for java.io RandomAccessFile getFD
public final FileDescriptor getFD() throws IOException
From source file:github.daneren2005.dsub.util.FileUtil.java
public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass, int hoursOld) { Input in = null;//from www . j ava2 s .c om try { File file = new File(context.getCacheDir(), fileName); if (!file.exists()) { return null; } if (hoursOld != 0) { Date fileDate = new Date(file.lastModified()); // Convert into hours long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600; if (age > hoursOld) { return null; } } RandomAccessFile randomFile = new RandomAccessFile(file, "r"); in = new Input(new FileInputStream(randomFile.getFD())); synchronized (kryo) { T result = kryo.readObject(in, tClass); return result; } } catch (FileNotFoundException e) { // Different error message Log.w(TAG, "No serialization for object from " + fileName); return null; } catch (Throwable x) { Log.w(TAG, "Failed to deserialize object from " + fileName); return null; } finally { Util.close(in); } }
From source file:github.daneren2005.dsub.util.FileUtil.java
public static <T extends Serializable> boolean serialize(Context context, T obj, String fileName) { Output out = null;/*from ww w .j a va 2s. c o m*/ try { RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); out = new Output(new FileOutputStream(file.getFD())); synchronized (kryo) { kryo.writeObject(out, obj); } return true; } catch (Throwable x) { Log.w(TAG, "Failed to serialize object to " + fileName); return false; } finally { Util.close(out); } }
From source file:org.apache.hadoop.io.nativeio.NativeIO2.java
/** * Create a FileInputStream that shares delete permission on the * file opened at a given offset, i.e. other process can delete * the file the FileInputStream is reading. Only Windows implementation * uses the native interface.//from www. ja v a 2s. c om */ public static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset) throws IOException { if (!Shell.WINDOWS) { RandomAccessFile rf = new RandomAccessFile(f, "r"); if (seekOffset > 0) { rf.seek(seekOffset); } return new FileInputStream(rf.getFD()); } else { // Use Windows native interface to create a FileInputStream that // shares delete permission on the file opened, and set it to the // given offset. // FileDescriptor fd = NativeIO2.Windows.createFile( f.getAbsolutePath(), NativeIO2.Windows.GENERIC_READ, NativeIO2.Windows.FILE_SHARE_READ | NativeIO2.Windows.FILE_SHARE_WRITE | NativeIO2.Windows.FILE_SHARE_DELETE, NativeIO2.Windows.OPEN_EXISTING); if (seekOffset > 0) NativeIO2.Windows.setFilePointer(fd, seekOffset, NativeIO2.Windows.FILE_BEGIN); return new FileInputStream(fd); } }
From source file:github.daneren2005.dsub.util.FileUtil.java
public static <T extends Serializable> boolean serializeCompressed(Context context, T obj, String fileName) { Output out = null;/*ww w. ja v a 2 s.co m*/ try { RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw"); out = new Output(new DeflaterOutputStream(new FileOutputStream(file.getFD()))); synchronized (kryo) { kryo.writeObject(out, obj); } return true; } catch (Throwable x) { Log.w(TAG, "Failed to serialize compressed object to " + fileName); return false; } finally { Util.close(out); } }
From source file:com.buaa.cfs.io.nativeio.NativeIO.java
/** * Create a FileInputStream that shares delete permission on the file opened at a given offset, i.e. other process * can delete the file the FileInputStream is reading. Only Windows implementation uses the native interface. *///from w w w.j ava2s . c o m public static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset) throws IOException { if (!Shell.WINDOWS) { RandomAccessFile rf = new RandomAccessFile(f, "r"); if (seekOffset > 0) { rf.seek(seekOffset); } return new FileInputStream(rf.getFD()); } else { // Use Windows native interface to create a FileInputStream that // shares delete permission on the file opened, and set it to the // given offset. // FileDescriptor fd = NativeIO.Windows.createFile( f.getAbsolutePath(), NativeIO.Windows.GENERIC_READ, NativeIO.Windows.FILE_SHARE_READ | NativeIO.Windows.FILE_SHARE_WRITE | NativeIO.Windows.FILE_SHARE_DELETE, NativeIO.Windows.OPEN_EXISTING); if (seekOffset > 0) NativeIO.Windows.setFilePointer(fd, seekOffset, NativeIO.Windows.FILE_BEGIN); return new FileInputStream(fd); } }
From source file:github.daneren2005.dsub.util.FileUtil.java
@TargetApi(Build.VERSION_CODES.GINGERBREAD) public static <T extends Serializable> T deserializeCompressed(Context context, String fileName, Class<T> tClass) {/* w w w .jav a2s . c o m*/ Input in = null; try { RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r"); in = new Input(new InflaterInputStream(new FileInputStream(file.getFD()))); synchronized (kryo) { T result = kryo.readObject(in, tClass); return result; } } catch (FileNotFoundException e) { // Different error message Log.w(TAG, "No serialization compressed for object from " + fileName); return null; } catch (Throwable x) { Log.w(TAG, "Failed to deserialize compressed object from " + fileName); return null; } finally { Util.close(in); } }
From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java
@Test(expected = BadCheckpointException.class) public void testTruncateMeta() throws Exception { EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test"); backingStore.close();/*from w ww. jav a 2s . com*/ Assert.assertTrue(checkpoint.exists()); File metaFile = Serialization.getMetaDataFile(checkpoint); Assert.assertTrue(metaFile.length() != 0); RandomAccessFile writer = new RandomAccessFile(metaFile, "rw"); writer.setLength(0); writer.getFD().sync(); writer.close(); backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test"); }
From source file:com.thoughtworks.go.config.GoConfigFileWriter.java
public synchronized void writeToConfigXmlFile(String content) { FileChannel channel = null;//from w w w. j a v a 2s. c o m FileOutputStream outputStream = null; FileLock lock = null; try { RandomAccessFile randomAccessFile = new RandomAccessFile(fileLocation(), "rw"); channel = randomAccessFile.getChannel(); lock = channel.lock(); randomAccessFile.seek(0); randomAccessFile.setLength(0); outputStream = new FileOutputStream(randomAccessFile.getFD()); IOUtils.write(content, outputStream, UTF_8); } catch (Exception e) { throw new RuntimeException(e); } finally { if (channel != null && lock != null) { try { lock.release(); channel.close(); IOUtils.closeQuietly(outputStream); } catch (IOException e) { LOGGER.error("Error occured when releasing file lock and closing file.", e); } } } }
From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java
@Test(expected = InvalidProtocolBufferException.class) public void testCorruptMeta() throws Throwable { EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test"); backingStore.close();/*from w ww . j a va 2 s .com*/ Assert.assertTrue(checkpoint.exists()); File metaFile = Serialization.getMetaDataFile(checkpoint); Assert.assertTrue(metaFile.length() != 0); RandomAccessFile writer = new RandomAccessFile(metaFile, "rw"); writer.seek(10); writer.writeLong(new Random().nextLong()); writer.getFD().sync(); writer.close(); try { backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test"); } catch (BadCheckpointException ex) { throw ex.getCause(); } }
From source file:tayler.TailerTest.java
protected void eraseFile(File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "rws"); try {//w w w . j a va 2 s . c o m raf.setLength(0); raf.getFD().sync(); } finally { IOUtils.closeQuietly(raf); } }