Author: andy Date: Mon Nov 11 18:41:29 2013 New Revision: 1540799 URL: http://svn.apache.org/r1540799 Log: Tidy up formatting and where logging operations are performed.
Modified: jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/base/file/BufferChannelMem.java Modified: jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/base/file/BufferChannelMem.java URL: http://svn.apache.org/viewvc/jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/base/file/BufferChannelMem.java?rev=1540799&r1=1540798&r2=1540799&view=diff ============================================================================== --- jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/base/file/BufferChannelMem.java (original) +++ jena/trunk/jena-tdb/src/main/java/com/hp/hpl/jena/tdb/base/file/BufferChannelMem.java Mon Nov 11 18:41:29 2013 @@ -16,7 +16,7 @@ * limitations under the License. */ -package com.hp.hpl.jena.tdb.base.file; +package com.hp.hpl.jena.tdb.base.file ; import java.nio.ByteBuffer ; @@ -26,40 +26,40 @@ import org.slf4j.LoggerFactory ; import com.hp.hpl.jena.tdb.base.StorageException ; -public class BufferChannelMem implements BufferChannel -{ - private static Logger log = LoggerFactory.getLogger(BufferChannelMem.class) ; - private ByteBuffer bytes ; // Position is our file position. - private String name ; - private static int INIT_SIZE = 1024 ; - private static int INC_SIZE = 1024 ; - - private final boolean TRACKING ; - - static public BufferChannel create() { return new BufferChannelMem("unnamed") ; } - static public BufferChannel create(String name) { return new BufferChannelMem(name) ; } - - private BufferChannelMem() - { +public class BufferChannelMem implements BufferChannel { + private static Logger log = LoggerFactory.getLogger(BufferChannelMem.class) ; + // The "file pointer" is the position of this buffer. + private ByteBuffer bytes ; + private String name ; + private static int INIT_SIZE = 1024 ; + private static int INC_SIZE = 1024 ; + + private final boolean TRACKING ; + + static public BufferChannel create() { + return new BufferChannelMem("unnamed") ; + } + + static public BufferChannel create(String name) { + return new BufferChannelMem(name) ; + } + + private BufferChannelMem() { // Unitialized blank. TRACKING = false ; } - - - private BufferChannelMem(String name) - { + + private BufferChannelMem(String name) { bytes = ByteBuffer.allocate(1024) ; bytes.limit(0) ; this.name = name ; TRACKING = false ; // Debugging : pick a filename. - //TRACKING = name.endsWith("prefixes.dat") ; + // TRACKING = name.endsWith("prefixes.dat") ; } @Override - synchronized - public BufferChannel duplicate() - { + synchronized public BufferChannel duplicate() { BufferChannelMem chan = new BufferChannelMem() ; int x = bytes.position() ; bytes.rewind() ; @@ -70,202 +70,170 @@ public class BufferChannelMem implements } @Override - synchronized - public long position() - { + synchronized public long position() { checkIfClosed() ; return bytes.position() ; } @Override - synchronized - public void position(long pos) - { + synchronized public void position(long pos) { checkIfClosed() ; if ( pos < 0 || pos > bytes.capacity() ) - throw new StorageException("Out of range: "+pos) ; + throw new StorageException("Out of range: " + pos) ; bytes.position((int)pos) ; } @Override - synchronized - public int read(ByteBuffer buffer) - { - checkIfClosed() ; - if ( TRACKING ) - log("read(1)["+buffer.capacity()+"]") ; - - int x = bytes.position(); - - int len = buffer.limit()-buffer.position() ; + 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++) - { + // Copy out, moving the position of the bytes of stroage. + for (int i = 0; i < len; i++) { byte b = bytes.get() ; - buffer.put(b); + buffer.put(b) ; } if ( TRACKING ) - log("read(2)") ; + log("read>>") ; return len ; } - + @Override - synchronized - public int read(ByteBuffer buffer, long loc) - { - if ( TRACKING ) - log("read(1)@"+loc) ; + 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()+")") ; + throw new StorageException("Out of range(" + name + "[read]): " + loc + " [0," + bytes.limit() + ")") ; if ( loc == bytes.limit() ) - System.err.println("At the limit("+name+"[read]): "+loc) ; + 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(2)@"+loc) ; + log("read>>@" + loc) ; return len ; } @Override - synchronized - public int write(ByteBuffer buffer) - { - if ( TRACKING ) { - log("write(1)["+buffer.capacity()+"]") ; - if ( bytes.limit() != 0 ) - System.err.println("Not start") ; - } - + synchronized public int write(ByteBuffer buffer) { checkIfClosed() ; - int len = buffer.limit()-buffer.position() ; + 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 ; + + if ( len > freespace ) { + int inc = len - freespace ; inc += INC_SIZE ; - ByteBuffer bb2 = ByteBuffer.allocate(bytes.capacity()+inc) ; + 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.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) ; - + + if ( bytes.limit() < posn + len ) + bytes.limit(posn + len) ; + bytes.put(buffer) ; if ( TRACKING ) - log("write(2)") ; + log("write>>") ; return len ; } - + // Invert : write(ByteBuffer) = write(ByteBuffer,posn) @Override - synchronized - public int write(ByteBuffer buffer, long loc) - { - if ( TRACKING ) - log("write(1)@"+loc) ; - + 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() ; + 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(2)@"+loc) ; + log("write>>@" + loc) ; return len ; } - + @Override - synchronized - public void truncate(long size) - { + synchronized public void truncate(long size) { checkIfClosed() ; - int x = (int) size ; - + int x = (int)size ; if ( x < 0 ) - throw new StorageException("Out of range: "+size) ; + throw new StorageException("Out of range: " + size) ; if ( x > bytes.limit() ) return ; - + if ( bytes.position() > x ) bytes.position(x) ; bytes.limit(x) ; } @Override - synchronized - public long size() - { + synchronized public long size() { checkIfClosed() ; return bytes.limit() ; } - + @Override - synchronized - public boolean isEmpty() - { + synchronized public boolean isEmpty() { checkIfClosed() ; return size() == 0 ; } - + @Override - synchronized - public void sync() - { + synchronized public void sync() { checkIfClosed() ; } @Override - synchronized - public void close() - { checkIfClosed() ; bytes = null ; } - - private void checkIfClosed() - { + synchronized public void close() { + checkIfClosed() ; + bytes = null ; + } + + private void checkIfClosed() { if ( bytes == null ) - throw new StorageException("Closed: "+name) ; + throw new StorageException("Closed: " + name) ; } - + @Override - synchronized - public String getLabel() - { + synchronized public String getLabel() { return name ; } - + @Override - synchronized - public String toString() - { + synchronized public String toString() { return name ; } + @Override - public String getFilename() - { + public String getFilename() { return null ; } - + private void log(String op) { if ( TRACKING ) { - String msg = op+"["+name+"] "+ByteBufferLib.details(bytes) ; - System.err.println(msg); - //log.debug(msg) ; + String msg = op + " [" + name + "] " + ByteBufferLib.details(bytes) ; + log.debug(msg) ; } } }