/** * Get a {@link FileBuffer} corresponding to this instance. * It is the caller's responsibility to {@link FileBuffer#close()} it. * @param mode as for {@link java.io.RandomAccessFile#RandomAccessFile(File, String)}, * <code>"r"</code> and <code>"rw"</code> being common choices * @return a new {@link FileBuffer} */ public FileBuffer getFileBuffer(String mode) { return new FileBuffer(this.file.getPath(), mode); }
/** * Retrieve the NIO channel that corresponds to this file. * * @return the file channel. */ private FileChannel getFileChannel() throws FileNotFoundException { if (channel == null) { RandomAccessFile file = new RandomAccessFile(getPath(), mode); channel = file.getChannel(); } return channel; }
/** * Delegates to {@link FileChannel} * * @see FileChannel#write(ByteBuffer, long) */ public int write(ByteBuffer src, long position) throws IOException { return getFileChannel().write(src, position); }
/** * Close the active file buffer, cleaning up any potential messes left by * the file buffer itself. */ private void closeFileBuffer() { try { if (buffer != null) buffer.close(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Buffer could not be closed successfully.", e); } throw new ResourceError( e.getMessage() + " Please check server log."); } }
@RolesAllowed("user") public boolean truncate(long length) { errorIfNotLoaded(); try { if (length < buffer.size()) { buffer.truncate(length); modified(); return true; } return false; } catch (NonWritableChannelException nwce) { throw new SecurityViolation("File not writeable!"); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Buffer write did not occur.", e); } throw new ResourceError(e.getMessage()); } }
@RolesAllowed("user") public void write(byte[] buf, long position, int length) { errorIfNotLoaded(); ByteBuffer nioBuffer = MappedByteBuffer.wrap(buf); nioBuffer.limit(length); if (diskSpaceChecking) { iRepositoryInfo.sanityCheckRepository(); } try { do { position += buffer.write(nioBuffer, position); } while (nioBuffer.hasRemaining()); // Write was successful, update state. modified(); } catch (NonWritableChannelException nwce) { throw new SecurityViolation("File not writeable!"); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Buffer write did not occur.", e); } throw new ResourceError(e.getMessage()); } }
@RolesAllowed("user") public long size() { errorIfNotLoaded(); try { return buffer.size(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Buffer write did not occur.", e); } throw new ResourceError(e.getMessage()); } }
@RolesAllowed("user") public byte[] read(long position, int length) { errorIfNotLoaded(); sec.checkRestriction(BinaryAccessPolicy.NAME, file); byte[] rawBuf = new byte[length]; ByteBuffer buf = ByteBuffer.wrap(rawBuf); try { buffer.read(buf, position); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Buffer could not be read.", e); } throw new ResourceError(e.getMessage()); } return rawBuf; }
/** * Returns FileBuffer based on OriginalFile path * * @param file * @return FileBuffer */ public FileBuffer getFileBuffer(OriginalFile file, String mode) { String path = getFilesPath(file.getId()); createSubpath(path); return new FileBuffer(path, mode); }
@RolesAllowed("user") public boolean exists() { errorIfNotLoaded(); return new File(buffer.getPath()).exists(); }
/** * Delegates to {@link FileChannel} * * @see FileChannel#read(ByteBuffer, long) */ public int read(ByteBuffer dst, long position) throws IOException { return getFileChannel().read(dst, position); }
/** * Delegates to {@link FileChannel} * * @see FileChannel#read(ByteBuffer) */ public int read(ByteBuffer dst) throws IOException { return getFileChannel().read(dst); }
/** * Delegates to {@link FileChannel} * * @see FileChannel#write(ByteBuffer) */ public int write(ByteBuffer src) throws IOException { return getFileChannel().write(src); }
public void truncate(long size) throws IOException { getFileChannel().truncate(size); }
public long size() throws IOException { return getFileChannel().size(); }
/** * Only truncate if the size of the file is less than the size argument. * * @param size * @return true if truncation was performed. */ public boolean truncateIfSmaller(long size) throws IOException { FileChannel fc = getFileChannel(); if (fc.size() < size) { return false; } else { fc.truncate(size); return true; } } }