protected void openSupport(String reason) throws FMFileManagerException { if (raf != null) { throw (new FMFileManagerException("file already open")); } reserveAccess(reason); try { file_access.aboutToOpen(); raf = new RandomAccessFile( linked_file, access_mode == FM_READ ? READ_ACCESS_MODE : WRITE_ACCESS_MODE); } catch (FileNotFoundException e) { int st = file_access.getStorageType(); boolean ok = false; // edge case here when switching one file from dnd -> download and the compact files at edge // boundaries not // yet allocated - attempt to create the file on demand try { linked_file.getParentFile().mkdirs(); linked_file.createNewFile(); raf = new RandomAccessFile( linked_file, access_mode == FM_READ ? READ_ACCESS_MODE : WRITE_ACCESS_MODE); ok = true; } catch (Throwable f) { } if (!ok) { Debug.printStackTrace(e); throw (new FMFileManagerException("open fails", e)); } } catch (Throwable e) { Debug.printStackTrace(e); throw (new FMFileManagerException("open fails", e)); } }
protected void reopen(FMFileManagerException cause) throws Throwable { if (!cause.isRecoverable()) { throw (cause); } if (raf != null) { try { raf.close(); } catch (Throwable e) { // ignore any close failure as can't do much } // don't clear down raf here as we want to leave things looking as they were // if the subsequent open fails } file_access.aboutToOpen(); raf = new RandomAccessFile( linked_file, access_mode == FM_READ ? READ_ACCESS_MODE : WRITE_ACCESS_MODE); Debug.outNoStack("Recovered connection to " + getName() + " after access failure"); }
public void setStorageType(int new_type) throws FMFileManagerException { try { this_mon.enter(); boolean was_open = isOpen(); if (was_open) { closeSupport(false); } try { file_access.setStorageType(new_type); } finally { if (was_open) { openSupport("Re-open after storage type change"); } } } finally { this_mon.exit(); } }
protected String getString() { File cPath = new File(canonical_path); String sPaths; if (cPath.equals(linked_file)) sPaths = "can/link=" + Debug.secretFileName(canonical_path); else sPaths = "can=" + Debug.secretFileName(canonical_path) + ",link=" + Debug.secretFileName(linked_file.toString()); return sPaths + ",raf=" + raf + ",acc=" + access_mode + ",ctrl = " + file_access.getString(); }
protected void setLengthSupport(long length) throws FMFileManagerException { try { file_access.setLength(raf, length); } catch (FMFileManagerException e) { if (OUTPUT_REOPEN_RELATED_ERRORS) { Debug.printStackTrace(e); } try { reopen(e); file_access.setLength(raf, length); } catch (Throwable e2) { throw (e); } } }
protected long getLengthSupport() throws FMFileManagerException { try { return (file_access.getLength(raf)); } catch (FMFileManagerException e) { if (OUTPUT_REOPEN_RELATED_ERRORS) { Debug.printStackTrace(e); } try { reopen(e); return (file_access.getLength(raf)); } catch (Throwable e2) { throw (e); } } }
protected void writeSupport(DirectByteBuffer[] buffers, long position) throws FMFileManagerException { try { file_access.write(raf, buffers, position); } catch (FMFileManagerException e) { if (OUTPUT_REOPEN_RELATED_ERRORS) { Debug.printStackTrace(e); } try { reopen(e); file_access.write(raf, buffers, position); } catch (Throwable e2) { throw (e); } } }
protected void setPieceCompleteSupport(int piece_number, DirectByteBuffer piece_data) throws FMFileManagerException { file_access.setPieceComplete(raf, piece_number, piece_data); }
protected boolean isPieceCompleteProcessingNeeded(int piece_number) throws FMFileManagerException { return (file_access.isPieceCompleteProcessingNeeded(piece_number)); }
public void flush() throws FMFileManagerException { file_access.flush(); }
public int getStorageType() { return (file_access.getStorageType()); }