@Override public void flush() { try { writer.flush() ; } catch (IOException ex) { IO.exception(ex) ; } }
@Override public void close() { try { writer.close() ; } catch (IOException ex) { IO.exception(ex) ; } }
@Override public void close() { try { reader.close() ; } catch (IOException ex) { IO.exception(ex) ; } }
private void checkPosition(long posn) { // Allows posn to be exactly the byte beyond the end if ( posn < 0 || posn > dataLength ) IO.exception(String.format("Position out of bounds: %d in [0,%d]", posn, dataLength)) ; }
@Override public long position() { try { return file.position() ; } catch (IOException e) { IO.exception(e) ; return -1 ; } }
@Override public int read(ByteBuffer buffer, long loc) { try { return file.read(buffer, loc) ; } catch (IOException e) { IO.exception(e) ; return -1 ; } }
@Override public int write(ByteBuffer buffer) { try { return file.write(buffer) ; } catch (IOException e) { IO.exception(e) ; return -1 ; } }
@Override public void close() { try { super.close() ; } catch (IOException ex) { IO.exception(ex) ; } } }
private void free() { try { if ( fileLock != null ) fileLock.release(); fileChannel.close(); fileLock = null; } catch (IOException ex) { IO.exception(ex); } }
/** Copy a file */ public static void copyFile(File source, File dest) { try { Files.copy(source.toPath(), dest.toPath()); } catch (IOException ex) { IO.exception(ex) ; } }
@Override public void truncate(long length) { try { // http://bugs.sun.com/view_bug.do?bug_id=6191269 if ( length < file.position() ) file.position(length) ; file.truncate(length) ; } catch (IOException e) { IO.exception(e) ; } }
/** Open a file for output - may include adding gzip processing. */ static public OutputStream openOutputFile(String filename) { try { return openOutputFileEx(filename) ; } catch (IOException ex) { IO.exception(ex) ; return null ; } }
@Override protected ByteBuffer allocateBuffer(long size) { try { return file.channel().map(FileChannel.MapMode.READ_WRITE, 0, size) ; } catch (IOException ex) { IO.exception(ex) ; return null ; } } }
@Override public int read(ByteBuffer buffer) { try { return file.channel().read(buffer) ; } catch (IOException e) { IO.exception(e) ; return -1 ; } }
@Override public int write(ByteBuffer buffer, long loc) { try { return file.channel().write(buffer, loc) ; } catch (IOException e) { IO.exception(e) ; return -1 ; } }
@Override public boolean isEmpty() { try { return file.channel().size() == 0 ; } catch (IOException e) { IO.exception(e) ; return false ; } }
public void truncate(long length) { if ( TRACKING ) log("truncate(%d)", length) ; if ( length < 0 ) IO.exception(String.format("truncate: bad length : %d", length)) ; checkOpen() ; dataLength = Math.min(dataLength, length) ; // clear above? }
@Override public void close() { try { reader.close() ; } catch (IOException ex) { IO.exception(ex) ; } }
/** Write to a file */ public static void write(String filename, StoreParams params) { try (OutputStream out = new FileOutputStream(filename); OutputStream out2 = new BufferedOutputStream(out); ) { JsonObject object = encodeToJson(params) ; JSON.write(out2, object) ; out2.write('\n') ; } catch (IOException ex) { IO.exception(ex); } }
/** Create or fetch a {@link ProcessFileLock} for a Location */ public static ProcessFileLock lockForLocation(Location location) { FileOps.ensureDir(location.getDirectoryPath()); String lockFilename = location.getPath(Names.TDB_LOCK_FILE); Path path = Paths.get(lockFilename); try { path.toFile().createNewFile(); } catch(IOException ex) { IO.exception(ex); return null; } return ProcessFileLock.create(lockFilename); }