@Override synchronized public int read(ByteBuffer buffer) { checkIfClosed() ; if ( TRACKING ) log("read<<[" + buffer.capacity() + "]") ; int x = bytes.position() ; int len = buffer.limit() - buffer.position() ; if ( len > bytes.remaining() ) len = bytes.remaining() ; // Copy out, moving the position of the bytes of stroage. for (int i = 0; i < len; i++) { byte b = bytes.get() ; buffer.put(b) ; } if ( TRACKING ) log("read>>") ; return len ; }
@Override synchronized public int write(ByteBuffer buffer, long loc) { checkIfClosed() ; if ( TRACKING ) log("write<<@" + loc) ; if ( loc < 0 || loc > bytes.limit() ) // Can write at loc = bytes() throw new StorageException("Out of range(" + name + "[write]): " + loc + " [0," + bytes.limit() + ")") ; int x = bytes.position() ; bytes.position((int)loc) ; int len = write(buffer) ; bytes.position(x) ; if ( TRACKING ) log("write>>@" + loc) ; return len ; }
@Override synchronized public int read(ByteBuffer buffer, long loc) { checkIfClosed() ; if ( TRACKING ) log("read<<@" + loc) ; if ( loc < 0 || loc > bytes.limit() ) throw new StorageException("Out of range(" + name + "[read]): " + loc + " [0," + bytes.limit() + ")") ; if ( loc == bytes.limit() ) log.warn("At the limit(" + name + "[read]): " + loc) ; int x = bytes.position() ; bytes.position((int)loc) ; int len = read(buffer) ; bytes.position(x) ; if ( TRACKING ) log("read>>@" + loc) ; return len ; }
@Override synchronized public int write(ByteBuffer buffer) { checkIfClosed() ; if ( TRACKING ) log("write<<[" + buffer.capacity() + "]") ; int len = buffer.limit() - buffer.position() ; int posn = bytes.position() ; int freespace = bytes.capacity() - bytes.position() ; if ( len > freespace ) { int inc = len - freespace ; inc += INC_SIZE ; ByteBuffer bb2 = ByteBuffer.allocate(bytes.capacity() + inc) ; bytes.position(0) ; // Copy contents; make written bytes area the same as before. bb2.put(bytes) ; bb2.limit(bytes.limit()) ; // limit is used as the end of active // bytes. bb2.position(posn) ; bytes = bb2 ; } if ( bytes.limit() < posn + len ) bytes.limit(posn + len) ; bytes.put(buffer) ; if ( TRACKING ) log("write>>") ; return len ; }