static public BufferChannel create() { return new BufferChannelMem("unnamed") ; }
@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 boolean isEmpty() { checkIfClosed() ; return size() == 0 ; }
@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 ; }
public static ObjectFile createObjectFileMem(String filename) { BufferChannel file = BufferChannelMem.create(filename) ; return new ObjectFileStorage(file) ; }
@Override synchronized public void close() { checkIfClosed() ; bytes = null ; }
@Override protected BufferChannel open() { return BufferChannelMem.create("Test-"+(counter++)) ; }
@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 ; }
@Override synchronized public void sync() { checkIfClosed() ; }
@Override protected BufferChannel open() { return BufferChannelMem.create("Test-"+(counter++)) ; }
@Override synchronized public long size() { checkIfClosed() ; return bytes.limit() ; }
static public BufferChannel create(String name) { return new BufferChannelMem(name) ; }
protected ObjectFile make(int bufferSize) { BufferChannel chan = BufferChannelMem.create() ; return new ObjectFileStorage(chan, bufferSize) ; }
@Override synchronized public long position() { checkIfClosed() ; return bytes.position() ; }
@Override synchronized public BufferChannel duplicate() { BufferChannelMem chan = new BufferChannelMem() ; int x = bytes.position() ; bytes.rewind() ; chan.bytes = bytes.slice() ; chan.bytes.position(0) ; bytes.position(x) ; return chan ; }
@Override ObjectFile createFile(String basename) { // String dir = ConfigTest.getTestingDir() ; // Location loc = Location.create(dir) ; // String fn = loc.getPath(basename) ; // FileOps.deleteSilent(fn) ; // BufferChannel chan = new BufferChannelFile(fn) ; // return new ObjectFileStorage(chan) ; BufferChannel chan = BufferChannelMem.create() ; // Small buffer return new ObjectFileStorage(chan,10) ; }
@Override synchronized public void position(long pos) { checkIfClosed() ; if ( pos < 0 || pos > bytes.capacity() ) throw new StorageException("Out of range: " + pos) ; bytes.position((int)pos) ; }
protected ObjectFile make(int bufferSize) { BufferChannel chan = BufferChannelMem.create() ; return new ObjectFileStorage(chan, bufferSize) ; }
@Override synchronized public void truncate(long size) { checkIfClosed() ; int x = (int)size ; if ( x < 0 ) throw new StorageException("Out of range: " + size) ; if ( x > bytes.limit() ) return ; if ( bytes.position() > x ) bytes.position(x) ; bytes.limit(x) ; }