[jsword-svn] r1407 - in trunk/common: etc/eclipse src/main/java/org/crosswire/common/compress src/main/java/org/crosswire/common/crypt src/main/java/org/crosswire/common/diff src/main/java/org/crosswire/common/history src/main/java/org/crosswire/common/progress src/main/java/org/crosswire/common/util

dmsmith at www.crosswire.org dmsmith at www.crosswire.org
Fri Jun 15 14:53:56 MST 2007


Author: dmsmith
Date: 2007-06-15 14:53:56 -0700 (Fri, 15 Jun 2007)
New Revision: 1407

Modified:
   trunk/common/etc/eclipse/common-junit-alltests.launch
   trunk/common/src/main/java/org/crosswire/common/compress/AbstractCompressor.java
   trunk/common/src/main/java/org/crosswire/common/compress/Compressor.java
   trunk/common/src/main/java/org/crosswire/common/compress/CompressorType.java
   trunk/common/src/main/java/org/crosswire/common/compress/LZSS.java
   trunk/common/src/main/java/org/crosswire/common/compress/Zip.java
   trunk/common/src/main/java/org/crosswire/common/crypt/Sapphire.java
   trunk/common/src/main/java/org/crosswire/common/diff/Bitap.java
   trunk/common/src/main/java/org/crosswire/common/diff/CommonMiddle.java
   trunk/common/src/main/java/org/crosswire/common/diff/Commonality.java
   trunk/common/src/main/java/org/crosswire/common/diff/Diff.java
   trunk/common/src/main/java/org/crosswire/common/diff/DiffCleanup.java
   trunk/common/src/main/java/org/crosswire/common/diff/DifferenceEngine.java
   trunk/common/src/main/java/org/crosswire/common/diff/Locator.java
   trunk/common/src/main/java/org/crosswire/common/diff/Patch.java
   trunk/common/src/main/java/org/crosswire/common/history/History.java
   trunk/common/src/main/java/org/crosswire/common/progress/Job.java
   trunk/common/src/main/java/org/crosswire/common/util/StringUtil.java
Log:
QA changes (checkstyle, findbugs, pmd, ...)

Modified: trunk/common/etc/eclipse/common-junit-alltests.launch
===================================================================
--- trunk/common/etc/eclipse/common-junit-alltests.launch	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/etc/eclipse/common-junit-alltests.launch	2007-06-15 21:53:56 UTC (rev 1407)
@@ -1,16 +1,23 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <launchConfiguration type="org.eclipse.jdt.junit.launchconfig">
-<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="AllTests"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="jsword"/>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx128m -ea"/>
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
+<listEntry value="/common/src/test/java/AllTests.java"/>
+</listAttribute>
+<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
+<listEntry value="1"/>
+</listAttribute>
+<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
 <listAttribute key="org.eclipse.debug.ui.favoriteGroups">
 <listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
 <listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
 </listAttribute>
 <stringAttribute key="org.eclipse.debug.ui.target_debug_perspective" value="perspective_default"/>
+<stringAttribute key="org.eclipse.debug.ui.target_run_perspective" value="perspective_none"/>
 <stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
-<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
-<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.jdt.debug.ui.javaSourceLocator"/>
-<stringAttribute key="org.eclipse.debug.ui.target_run_perspective" value="perspective_none"/>
+<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
+<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
+<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit3"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="AllTests"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="jsword"/>
+<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx128m -ea"/>
 </launchConfiguration>

Modified: trunk/common/src/main/java/org/crosswire/common/compress/AbstractCompressor.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/compress/AbstractCompressor.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/compress/AbstractCompressor.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -22,6 +22,8 @@
 
 package org.crosswire.common.compress;
 
+import java.io.InputStream;
+
 /**
  * A compressor provides the ability to compress and uncompress text.
  * 
@@ -31,10 +33,10 @@
  */
 public abstract class AbstractCompressor implements Compressor
 {
-    public AbstractCompressor(byte[] input)
+    public AbstractCompressor(InputStream input)
     {
         this.input = input;
     }
 
-    protected byte[] input;
+    protected InputStream input;
 }

Modified: trunk/common/src/main/java/org/crosswire/common/compress/Compressor.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/compress/Compressor.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/compress/Compressor.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -22,6 +22,7 @@
 
 package org.crosswire.common.compress;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 /**
@@ -35,18 +36,23 @@
 public interface Compressor
 {
     /**
+     * The size to read/write when unzipping a compressed byte array of unknown size.
+     */
+    int BUF_SIZE = 2048;
+
+    /**
      * Compresses the input and provides the result.
      * 
      * @return the compressed result
      */
-    byte[] compress() throws IOException;
+    ByteArrayOutputStream compress() throws IOException;
 
     /**
      * Uncompresses the input and provides the result.
      * 
      * @return the uncompressed result
      */
-    byte[] uncompress() throws IOException;
+    ByteArrayOutputStream uncompress() throws IOException;
 
     /**
      * Uncompresses the input and provides the result.
@@ -54,5 +60,5 @@
      * @param expectedSize the size of the result buffer
      * @return the uncompressed result
      */
-    byte[] uncompress(int expectedLength) throws IOException;
+    ByteArrayOutputStream uncompress(int expectedLength) throws IOException;
 }

Modified: trunk/common/src/main/java/org/crosswire/common/compress/CompressorType.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/compress/CompressorType.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/compress/CompressorType.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -21,6 +21,7 @@
  */
 package org.crosswire.common.compress;
 
+import java.io.ByteArrayInputStream;
 import java.io.Serializable;
 
 /**
@@ -42,7 +43,7 @@
          */
         public Compressor getCompressor(byte[] input)
         {
-            return new Zip(input);
+            return new Zip(new ByteArrayInputStream(input));
         }
 
         /**
@@ -61,7 +62,7 @@
          */
         public Compressor getCompressor(byte[] input)
         {
-            return new LZSS(input);
+            return new LZSS(new ByteArrayInputStream(input));
         }
 
         /**

Modified: trunk/common/src/main/java/org/crosswire/common/compress/LZSS.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/compress/LZSS.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/compress/LZSS.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -22,7 +22,11 @@
 
 package org.crosswire.common.compress;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
+
 /**
  * The LZSS compression is a port of code as implemented for STEP.
  * The following information gives the history of this implementation.
@@ -89,7 +93,7 @@
      * 
      * @param input to compress or uncompress.
      */
-    public LZSS(byte[] input)
+    public LZSS(InputStream input)
     {
         super(input);
     }
@@ -98,8 +102,10 @@
      * (non-Javadoc)
      * @see org.crosswire.common.compress.Compressor#compress()
      */
-    public byte[] compress()
+    public ByteArrayOutputStream compress() throws IOException
     {
+        out = new ByteArrayOutputStream(BUF_SIZE);
+
         short i;                        // an iterator
         int r;                          // node number in the binary tree
         short s;                        // position in the ring buffer
@@ -107,7 +113,7 @@
         int lastMatchLength;            // length of last match
         int codeBufPos;                 // position in the output buffer
         byte[] codeBuff = new byte[17]; // the output buffer
-        byte mask;                      // bit mask for byte 0 of out readBuffer
+        byte mask;                      // bit mask for byte 0 of out input
         byte c;                         // character read from string
 
         // Start with a clean tree.
@@ -152,12 +158,12 @@
         //
         // This function loads the buffer with X characters and returns
         // the actual amount loaded.
-        len = getBytes(ringBuffer, r, MAX_STORE_LENGTH);
+        len = input.read(ringBuffer, r, MAX_STORE_LENGTH);
 
         // Make sure there is something to be compressed.
         if (len == 0)
         {
-            return new byte[0];
+            return out;
         }
 
         // Insert the MAX_STORE_LENGTH strings, each of which begins with one or more
@@ -214,7 +220,7 @@
             {
                 // code_buf is the buffer of characters to be output.
                 // code_buf_pos is the number of characters it contains.
-                sendBytes(codeBuff, codeBufPos);
+                out.write(codeBuff, 0, codeBufPos);
 
                 // Reset for next buffer...
                 codeBuff[0] = 0;
@@ -229,12 +235,9 @@
             {
 
                 // Get next character...
-                try
+                c = (byte) input.read();
+                if (c == -1)
                 {
-                    c = getByte();
-                }
-                catch (ArrayIndexOutOfBoundsException e)
-                {
                     break;
                 }
 
@@ -313,31 +316,31 @@
         // There could still be something in the output buffer.  Send it now.
         if (codeBufPos > 1)
         {
-            // code_buf is the encoded string to send.
-            // code_buf_ptr is the number of characters.
-            sendBytes(codeBuff, codeBufPos);
+            // codeBuff is the encoded string to send.
+            // codeBufPos is the number of characters.
+            out.write(codeBuff, 0, codeBufPos);
         }
 
-        return writeBuffer;
+        return out;
     }
 
     /*
      * (non-Javadoc)
-     * @see org.crosswire.common.compress.Compressor#uncompress(int)
+     * @see org.crosswire.common.compress.Compressor#uncompress()
      */
-    public byte[] uncompress(int expectedSize)
+    public ByteArrayOutputStream uncompress() throws IOException
     {
-        writeBufferSizeIncrement = expectedSize;
-        writeBuffer = new byte[expectedSize];
-        return uncompress();
+        return uncompress(BUF_SIZE);
     }
 
     /*
      * (non-Javadoc)
-     * @see org.crosswire.common.compress.Compressor#uncompress()
+     * @see org.crosswire.common.compress.Compressor#uncompress(int)
      */
-    public byte[] uncompress()
+    public ByteArrayOutputStream uncompress(int expectedSize) throws IOException
     {
+        out = new ByteArrayOutputStream(expectedSize);
+
         byte[] c = new byte[MAX_STORE_LENGTH];     // an array of chars
         byte flags;                               // 8 bits of flags
 
@@ -369,13 +372,13 @@
             else
             {
                 // Next byte must be a flag.
-                if (!hasMoreToRead())
+                flags = (byte) input.read();
+
+                if (flags == -1)
                 {
                     break;
                 }
 
-                flags = getByte();
-
                 // Set the flag counter.  While at first it might appear
                 // that this should be an 8 since there are 8 bits in the
                 // flag, it should really be a 7 because the shift must
@@ -387,15 +390,12 @@
             // that the next byte is a single, unencoded character.
             if ((flags & 1) != 0)
             {
-                if (getBytes(c, 1) != 1)
+                if (input.read(c, 0, 1) != 1)
                 {
                     break;
                 }
 
-                if (sendBytes(c, 1) != 1)
-                {
-                    break;
-                }
+                out.write(c[0]);
 
                 // Add to buffer, and increment to next spot. Wrap at end.
                 ringBuffer[r] = c[0];
@@ -418,8 +418,7 @@
                 // I've modified this to only make one input call, and
                 // have changed the variable names to something more
                 // obvious.
-
-                if (getBytes(c, 2) != 2)
+                if (input.read(c, 0, 2) != 2)
                 {
                     break;
                 }
@@ -444,13 +443,10 @@
                 }
 
                 // Add the "len" characters to the output stream.
-                if (sendBytes(c, len) != len)
-                {
-                    break;
-                }
+                out.write(c, 0, len);
             }
         }
-        return writeBuffer;
+        return out;
     }
 
     /**
@@ -659,103 +655,103 @@
         dad[node] = NOT_USED;
     }
 
-    /**
-     * Fill a buffer with some bytes from the input stream.
-     * 
-     * @param readBuffer the buffer to fill
-     * @param start the position in the buffer to start filling
-     * @param count the number of bytes to get
-     * @return the number of bytes added to the buffer
-     */
-    private int getBytes(byte[] ibuf, int start, int len)
-    {
-        int slen = readBuffer.length;
-        int realLen = slen - readOffset > len ? len : slen - readOffset;
-        if (realLen > 0)
-        {
-            System.arraycopy(readBuffer, readOffset, ibuf, start, realLen);
-            readOffset += realLen;
-        }
+//    /**
+//     * Fill a buffer with some bytes from the input stream.
+//     * 
+//     * @param ibuf the buffer to fill
+//     * @param start the position in the buffer to start filling
+//     * @param len the number of bytes to get
+//     * @return the number of bytes added to the buffer
+//     */
+//    private int getBytes(byte[] ibuf, int start, int len)
+//    {
+//        int slen = input.length;
+//        int realLen = slen - readOffset > len ? len : slen - readOffset;
+//        if (realLen > 0)
+//        {
+//            System.arraycopy(input, readOffset, ibuf, start, realLen);
+//            readOffset += realLen;
+//        }
+//
+//        return realLen;
+//    }
+//
+//    /**
+//     * Fill a buffer with some bytes from the input stream.
+//     * 
+//     * @param ibuf the buffer to fill
+//     * @param len the number of bytes to get
+//     * @return the number of bytes added to the buffer
+//     */
+//    private int getBytes(byte[] ibuf, int len)
+//    {
+//        int slen = input.length;
+//        int realLen = slen - readOffset > len ? len : slen - readOffset;
+//        if (realLen > 0)
+//        {
+//            System.arraycopy(input, readOffset, ibuf, 0, realLen);
+//            readOffset += realLen;
+//        }
+//
+//        return realLen;
+//    }
+//
+//    /**
+//     * Return whether there are more bytes to read.
+//     * 
+//     * @return whether there are more bytes to read.
+//     */
+//    private boolean hasMoreToRead()
+//    {
+//        return readOffset < input.length;
+//    }
+//
+//    /**
+//     * Get the next byte from the stream.
+//     * 
+//     * @return the the next byte from the stream
+//     */
+//    private byte getByte()
+//    {
+//        return input[readOffset++];
+//    }
 
-        return realLen;
-    }
+//    private int sendBytes(byte[] ibuf, int len)
+//    {
+//        // Make sure the buffer is more than big enough
+//        bos = ensureCapacity(bos, writeOffset, len);
+//
+//        // Copy the new contents
+//        System.arraycopy(ibuf, 0, bos, writeOffset, len);
+//        writeOffset += len;
+//
+//        return len;
+//    }
+//
+//    private byte[] ensureCapacity(byte[] inputBuf, int currentPosition, int length)
+//    {
+//        // Make sure the buffer is more than big enough
+//        int biggerLength = currentPosition + length + writeBufferSizeIncrement;
+//        if (inputBuf != null)
+//        {
+//            int inputLength = inputBuf.length;
+//            if ((currentPosition + length) > inputLength)
+//            {
+//                byte[] biggerBuf = new byte[biggerLength];
+//                System.arraycopy(inputBuf, 0, biggerBuf, 0, inputLength);
+//                for (int i = inputLength; i < biggerLength; i++)
+//                {
+//                    biggerBuf[i] = '\0';
+//                }
+//                return biggerBuf;
+//            }
+//            return inputBuf;
+//        }
+//
+//        return new byte[length + writeBufferSizeIncrement];
+//    }
 
     /**
-     * Fill a buffer with some bytes from the input stream.
-     * 
-     * @param readBuffer the buffer to fill
-     * @param count the number of bytes to get
-     * @return the number of bytes added to the buffer
-     */
-    private int getBytes(byte[] ibuf, int len)
-    {
-        int slen = readBuffer.length;
-        int realLen = slen - readOffset > len ? len : slen - readOffset;
-        if (realLen > 0)
-        {
-            System.arraycopy(readBuffer, readOffset, ibuf, 0, realLen);
-            readOffset += realLen;
-        }
-
-        return realLen;
-    }
-
-    /**
-     * Return whether there are more bytes to read.
-     * 
-     * @return whether there are more bytes to read.
-     */
-    private boolean hasMoreToRead()
-    {
-        return readOffset < readBuffer.length;
-    }
-
-    /**
-     * Get the next byte from the stream.
-     * 
-     * @return the the next byte from the stream
-     */
-    private byte getByte()
-    {
-        return readBuffer[readOffset++];
-    }
-
-    private int sendBytes(byte[] ibuf, int len)
-    {
-        // Make sure the buffer is more than big enough
-        writeBuffer = ensureCapacity(writeBuffer, writeOffset, len);
-
-        // Copy the new contents
-        System.arraycopy(ibuf, 0, writeBuffer, writeOffset, len);
-        writeOffset += len;
-
-        return len;
-    }
-
-    private byte[] ensureCapacity(byte[] inputBuf, int currentPosition, int length)
-    {
-        // Make sure the buffer is more than big enough
-        int biggerLength = currentPosition + length + writeBufferSizeIncrement;
-        if (inputBuf != null)
-        {
-            int inputLength = inputBuf.length;
-            if ((currentPosition + length) > inputLength)
-            {
-                byte[] biggerBuf = new byte[biggerLength];
-                System.arraycopy(inputBuf, 0, biggerBuf, 0, inputLength);
-                for (int i = inputLength; i < biggerLength; i++)
-                {
-                    biggerBuf[i] = '\0';
-                }
-                return biggerBuf;
-            }
-            return inputBuf;
-        }
-
-        return new byte[length + writeBufferSizeIncrement];
-    }
-
-    /**
      * This is the size of the ring buffer.  It is set to 4K. 
      * It is important to note that a position within the ring buffer requires 12 bits.  
      */
@@ -836,27 +832,7 @@
     private short[] rightSon = new short[RING_SIZE + 257];
 
     /**
-     * The buffer to get or send, when uncompressed.
+     * The output stream containing the result.
      */
-    private byte[] readBuffer;
-
-    /**
-     * The current offset into readBuffer.
-     */
-    private int readOffset;
-
-    /**
-     * The incremental size of the writeBuffer to use.
-     */
-    private int writeBufferSizeIncrement = 1024;
-
-    /**
-     * The buffer to get or send, when compressed.
-     */
-    private byte[] writeBuffer;
-
-    /**
-     * The current offset into writeBuffer.
-     */
-    private int writeOffset;
+    private ByteArrayOutputStream out;
 }

Modified: trunk/common/src/main/java/org/crosswire/common/compress/Zip.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/compress/Zip.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/compress/Zip.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -24,9 +24,9 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.zip.Deflater;
 import java.util.zip.DeflaterOutputStream;
 import java.util.zip.Inflater;
@@ -48,7 +48,7 @@
      * 
      * @param input to compress or uncompress.
      */
-    public Zip(byte[] input)
+    public Zip(InputStream input)
     {
         super(input);
     }
@@ -56,13 +56,12 @@
     /* (non-Javadoc)
      * @see org.crosswire.common.compress.Compressor#compress()
      */
-    public byte[] compress() throws IOException
+    public ByteArrayOutputStream compress() throws IOException
     {
-        ByteArrayInputStream bis = new ByteArrayInputStream(input);
-        BufferedInputStream in = new BufferedInputStream(bis);
+        BufferedInputStream in = new BufferedInputStream(input);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        DeflaterOutputStream out = new DeflaterOutputStream(bos, new Deflater(), ZBUF_SIZE);
-        byte[] buf = new byte[ZBUF_SIZE];
+        DeflaterOutputStream out = new DeflaterOutputStream(bos, new Deflater(), BUF_SIZE);
+        byte[] buf = new byte[BUF_SIZE];
 
         for (int count = in.read(buf); count != -1; count = in.read(buf))
         {
@@ -71,26 +70,25 @@
         in.close();
         out.flush();
         out.close();
-        return bos.toByteArray();
+        return bos;
     }
 
     /* (non-Javadoc)
      * @see org.crosswire.common.compress.Compressor#uncompress()
      */
-    public byte[] uncompress() throws IOException
+    public ByteArrayOutputStream uncompress() throws IOException
     {
-        return uncompress(ZBUF_SIZE);
+        return uncompress(BUF_SIZE);
     }
 
     /* (non-Javadoc)
      * @see org.crosswire.common.compress.Compressor#uncompress(int)
      */
-    public byte[] uncompress(int expectedLength) throws IOException
+    public ByteArrayOutputStream uncompress(int expectedLength) throws IOException
     {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         BufferedOutputStream out = new BufferedOutputStream(bos, expectedLength);
-        ByteArrayInputStream bis = new ByteArrayInputStream(input);
-        InflaterInputStream in = new InflaterInputStream(bis, new Inflater(), expectedLength);
+        InflaterInputStream in = new InflaterInputStream(input, new Inflater(), expectedLength);
         byte[] buf = new byte[expectedLength];
 
         for (int count = in.read(buf); count != -1; count = in.read(buf))
@@ -100,11 +98,7 @@
         in.close();
         out.flush();
         out.close();
-        return bos.toByteArray();
+        return bos;
     }
 
-    /**
-     * The size to read/write when unzipping a compressed byte array of unknown size.
-     */
-    private static final int ZBUF_SIZE = 2048;
 }

Modified: trunk/common/src/main/java/org/crosswire/common/crypt/Sapphire.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/crypt/Sapphire.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/crypt/Sapphire.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -552,10 +552,11 @@
         lastPlain = cards[7];
         lastCipher = cards[rsum];
 
+        // ensure that these have no useful values to those that snoop
         toswap = 0;
-        swaptemp = 0;
-        rsum = 0;
-        keypos = 0;
+        swaptemp = toswap;
+        rsum = swaptemp;
+        keypos = rsum;
     }
 
     /**

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Bitap.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Bitap.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Bitap.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -47,6 +47,7 @@
         this.text = text;
         this.pattern = pattern;
         this.loc = loc;
+        alphabet = new HashMap();
     }
 
     /* (non-Javadoc)
@@ -202,7 +203,6 @@
 
         assert len <= Bitap.MAXBITS : "Pattern too long for this application."; //$NON-NLS-1$
 
-        alphabet = new HashMap();
         for (int i = 0; i < len; i++)
         {
             Character c = new Character(pattern.charAt(i));

Modified: trunk/common/src/main/java/org/crosswire/common/diff/CommonMiddle.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/CommonMiddle.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/CommonMiddle.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -111,14 +111,11 @@
      */
     public int hashCode()
     {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ((sourcePrefix == null) ? 0 : sourcePrefix.hashCode());
-        result = prime * result + ((sourceSuffix == null) ? 0 : sourceSuffix.hashCode());
-        result = prime * result + ((targetPrefix == null) ? 0 : targetPrefix.hashCode());
-        result = prime * result + ((targetSuffix == null) ? 0 : targetSuffix.hashCode());
-        result = prime * result + ((commonality == null) ? 0 : commonality.hashCode());
-       return result;
+        int result = 31 + ((sourcePrefix == null) ? 0 : sourcePrefix.hashCode());
+        result = 31 * result + ((sourceSuffix == null) ? 0 : sourceSuffix.hashCode());
+        result = 31 * result + ((targetPrefix == null) ? 0 : targetPrefix.hashCode());
+        result = 31 * result + ((targetSuffix == null) ? 0 : targetSuffix.hashCode());
+        return 31 * result + ((commonality == null) ? 0 : commonality.hashCode());
     }
 
     /* (non-Javadoc)

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Commonality.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Commonality.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Commonality.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -114,9 +114,9 @@
         }
 
         // First check if the second quarter is the seed for a half-match.
-        CommonMiddle hm1 = halfMatch(longText, shortText, (int) Math.ceil(longTextLength / 4));
+        CommonMiddle hm1 = halfMatch(longText, shortText, ceil(longTextLength, 4));
         // Check again based on the third quarter.
-        CommonMiddle hm2 = halfMatch(longText, shortText, (int) Math.ceil(longTextLength / 2));
+        CommonMiddle hm2 = halfMatch(longText, shortText, ceil(longTextLength, 2));
         CommonMiddle hm = null;
         if (hm1 == null && hm2 == null)
         {
@@ -145,6 +145,14 @@
         return new CommonMiddle(hm.getTargetPrefix(), hm.getTargetSuffix(), hm.getSourcePrefix(), hm.getSourceSuffix(), hm.getCommonality());
     }
 
+    private static int ceil(int number, int divisor)
+    {
+        assert divisor > 0;
+        int result = number / divisor + ((number % divisor) > 0 ? 1 : 0);
+        // assert result == (int) Math.ceil(((double) number) / ((double) divisor));
+        return result;
+    }
+
     /**
      * Does a substring of shortText exist within longText such that the substring
      * is at least half the length of longText?

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Diff.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Diff.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Diff.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -104,12 +104,12 @@
         diffs = compute();
 
         // Restore the prefix and suffix
-        if (!commonPrefix.equals("")) //$NON-NLS-1$
+        if (!"".equals(commonPrefix)) //$NON-NLS-1$
         {
             diffs.add(0, new Difference(EditType.EQUAL, commonPrefix));
         }
 
-        if (!commonSuffix.equals("")) //$NON-NLS-1$
+        if (!"".equals(commonSuffix)) //$NON-NLS-1$
         {
             diffs.add(new Difference(EditType.EQUAL, commonSuffix));
         }
@@ -132,14 +132,14 @@
     {
         List diffs = new ArrayList();
 
-        if (text1.equals("")) //$NON-NLS-1$
+        if ("".equals(text1)) //$NON-NLS-1$
         {
             // Just add some text (speedup)
             diffs.add(new Difference(EditType.INSERT, text2));
             return diffs;
         }
 
-        if (text2.equals("")) //$NON-NLS-1$
+        if ("".equals(text2)) //$NON-NLS-1$
         {
             // Just delete some text (speedup)
             diffs.add(new Difference(EditType.DELETE, text1));
@@ -159,10 +159,6 @@
             return diffs;
         }
 
-        // Garbage collect
-        longText = null;
-        shortText = null;
-
         // Check to see if the problem can be split in two.
         CommonMiddle middleMatch = Commonality.halfMatch(text1, text2);
         if (middleMatch != null)
@@ -215,8 +211,8 @@
             diffs.add(new Difference(EditType.EQUAL, "")); //$NON-NLS-1$
             int countDeletes = 0;
             int countInserts = 0;
-            String textDelete = ""; //$NON-NLS-1$
-            String textInsert = ""; //$NON-NLS-1$
+            StringBuffer textDelete = new StringBuffer();
+            StringBuffer textInsert = new StringBuffer();
             ListIterator pointer = diffs.listIterator();
             Difference curDiff = (Difference) pointer.next();
             while (curDiff != null)
@@ -225,12 +221,12 @@
                 if (EditType.INSERT.equals(editType))
                 {
                     countInserts++;
-                    textInsert += curDiff.getText();
+                    textInsert.append(curDiff.getText());
                 }
                 else if (EditType.DELETE.equals(editType))
                 {
                     countDeletes++;
-                    textDelete += curDiff.getText();
+                    textDelete.append(curDiff.getText());
                 }
                 else
                 {
@@ -244,7 +240,7 @@
                             pointer.previous();
                             pointer.remove();
                         }
-                        Diff newDiff = new Diff(textDelete, textInsert, false);
+                        Diff newDiff = new Diff(textDelete.toString(), textInsert.toString(), false);
                         Iterator iter = newDiff.compare().iterator();
                         while (iter.hasNext())
                         {
@@ -253,8 +249,8 @@
                     }
                     countInserts = 0;
                     countDeletes = 0;
-                    textDelete = ""; //$NON-NLS-1$
-                    textInsert = ""; //$NON-NLS-1$
+                    textDelete.delete(0, textDelete.length());
+                    textInsert.delete(0, textInsert.length());
                 }
                 curDiff = pointer.hasNext() ? (Difference) pointer.next() : null;
             }

Modified: trunk/common/src/main/java/org/crosswire/common/diff/DiffCleanup.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/DiffCleanup.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/DiffCleanup.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -267,8 +267,8 @@
 
         int countDelete = 0;
         int countInsert = 0;
-        String textDelete = ""; //$NON-NLS-1$
-        String textInsert = ""; //$NON-NLS-1$
+        StringBuffer textDelete = new StringBuffer();
+        StringBuffer textInsert = new StringBuffer();
 
         int commonLength = 0;
 
@@ -281,13 +281,13 @@
             if (EditType.INSERT.equals(editType))
             {
                 countInsert++;
-                textInsert += curDiff.getText();
+                textInsert.append(curDiff.getText());
                 prevEqual = null;
             }
             else if (EditType.DELETE.equals(editType))
             {
                 countDelete++;
-                textDelete += curDiff.getText();
+                textDelete.append(curDiff.getText());
                 prevEqual = null;
             }
             else if (EditType.EQUAL.equals(editType))
@@ -311,7 +311,7 @@
                     if (countDelete != 0 && countInsert != 0)
                     {
                         // Factor out any common prefixies.
-                        commonLength = Commonality.prefix(textInsert, textDelete);
+                        commonLength = Commonality.prefix(textInsert.toString(), textDelete.toString());
                         if (commonLength > 0)
                         {
                             if (pointer.hasPrevious())
@@ -325,18 +325,18 @@
                             {
                                 pointer.add(new Difference(EditType.EQUAL, textInsert.substring(0, commonLength)));
                             }
-                            textInsert = textInsert.substring(commonLength);
-                            textDelete = textDelete.substring(commonLength);
+                            textInsert.replace(0, textInsert.length(), textInsert.substring(commonLength));
+                            textDelete.replace(0, textDelete.length(), textDelete.substring(commonLength));
                         }
 
                         // Factor out any common suffixies.
-                        commonLength = Commonality.suffix(textInsert, textDelete);
+                        commonLength = Commonality.suffix(textInsert.toString(), textDelete.toString());
                         if (commonLength > 0)
                         {
                             curDiff = (Difference) pointer.next();
                             curDiff.prependText(textInsert.substring(textInsert.length() - commonLength));
-                            textInsert = textInsert.substring(0, textInsert.length() - commonLength);
-                            textDelete = textDelete.substring(0, textDelete.length() - commonLength);
+                            textInsert.replace(0, textInsert.length(), textInsert.substring(0, textInsert.length() - commonLength));
+                            textDelete.replace(0, textDelete.length(), textDelete.substring(0, textDelete.length() - commonLength));
                             pointer.previous();
                         }
                     }
@@ -344,12 +344,12 @@
                     // Insert the merged records.
                     if (textDelete.length() != 0)
                     {
-                        pointer.add(new Difference(EditType.DELETE, textDelete));
+                        pointer.add(new Difference(EditType.DELETE, textDelete.toString()));
                     }
 
                     if (textInsert.length() != 0)
                     {
-                        pointer.add(new Difference(EditType.INSERT, textInsert));
+                        pointer.add(new Difference(EditType.INSERT, textInsert.toString()));
                     }
 
                     // Step forward to the equality.
@@ -366,8 +366,8 @@
 
                 countInsert = 0;
                 countDelete = 0;
-                textDelete = ""; //$NON-NLS-1$
-                textInsert = ""; //$NON-NLS-1$
+                textDelete.delete(0, textDelete.length());
+                textInsert.delete(0, textInsert.length());
                 prevEqual = curDiff;
             }
             curDiff = pointer.hasNext() ? (Difference) pointer.next() : null;

Modified: trunk/common/src/main/java/org/crosswire/common/diff/DifferenceEngine.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/DifferenceEngine.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/DifferenceEngine.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -102,7 +102,7 @@
         boolean done = false;
         // If the total number of characters is odd, then the front path will
         // collide with the reverse path.
-        boolean front = (source.length() + target.length()) % 2 == 1;
+        boolean front = (source.length() + target.length()) % 2 != 0;
         for (int d = 0; d < maxD; d++)
         {
             // Bail out if timeout reached.

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Locator.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Locator.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Locator.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -35,12 +35,12 @@
      * 
      * @return Best match index or -1, if no match found
      */
-    public int locate();
+    int locate();
 
     /**
      * The maximum length a pattern can be.
      * 
      * @return maximum pattern length.
      */
-    public int maxPatternLength();
+    int maxPatternLength();
 }

Modified: trunk/common/src/main/java/org/crosswire/common/diff/Patch.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/Patch.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/diff/Patch.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -154,16 +154,13 @@
                 patch.adjustLength2(len);
             }
 
-            if (EditType.EQUAL.equals(editType) && len >= 2 * margin)
+            // Time for a new patch.
+            if (EditType.EQUAL.equals(editType) && len >= 2 * margin && patch.hasDifferences())
             {
-                // Time for a new patch.
-                if (patch.hasDifferences())
-                {
-                    patch.addContext(prePatchText);
-                    patches.add(patch);
-                    patch = new PatchEntry();
-                    prePatchText = postPatchText;
-                }
+                patch.addContext(prePatchText);
+                patches.add(patch);
+                patch = new PatchEntry();
+                prePatchText = postPatchText;
             }
 
             // Update the current character count.
@@ -287,7 +284,12 @@
         {
             if (bigPatch.getLeftLength() <= maxPatternLength)
             {
-                bigPatch = pointer.hasNext() ? (PatchEntry) pointer.next() : null;
+                if (!pointer.hasNext())
+                {
+                    break;
+                }
+
+                bigPatch = (PatchEntry) pointer.next();
             }
 
             // Remove the big old patch.

Modified: trunk/common/src/main/java/org/crosswire/common/history/History.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/history/History.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/history/History.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -118,7 +118,7 @@
                     pos++;
                 }
                 // At this point pos either == size or the element at pos matches what we are navigating to.
-                nav.subList(backCount, Math.min(pos++, size)).clear();
+                nav.subList(backCount, Math.min(pos, size)).clear();
             }
 
             // If it matches, then we don't have to do anything more

Modified: trunk/common/src/main/java/org/crosswire/common/progress/Job.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/progress/Job.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/progress/Job.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -76,36 +76,38 @@
             return;
         }
 
-        this.totalWork = workToDo;
-        this.sectionName = name;
-        this.jobName = name;
-        this.work = 0;
-        this.finished = false;
-        this.cancelable = workerThread != null;
-
-        if (totalWork == UNKNOWN)
+        synchronized (this)
         {
-            updater = new Timer();
-            updater.schedule(new PredictTask(), 0, 100);
-        }
+            this.totalWork = workToDo;
+            this.sectionName = name;
+            this.jobName = name;
+            this.work = 0;
+            this.finished = false;
+            this.cancelable = workerThread != null;
 
-        // Set-up the timings files. It's not a disaster if it doesn't load
-        if (predictURI != null)
-        {
-            loadPredictions();
-        }
+            if (totalWork == UNKNOWN)
+            {
+                updater = new Timer();
+                updater.schedule(new PredictTask(), 0, 100);
+            }
 
-        // And the predictions for next time
-        current = new HashMap();
-        start = System.currentTimeMillis();
+            // Set-up the timings files. It's not a disaster if it doesn't load
+            if (predictURI != null)
+            {
+                loadPredictions();
+            }
 
+            // And the predictions for next time
+            current = new HashMap();
+            start = System.currentTimeMillis();
+        }
         JobManager.fireWorkProgressed(this);
     }
 
     /* (non-Javadoc)
      * @see org.crosswire.common.progress.Progress#getTotalWork()
      */
-    public int getTotalWork()
+    public synchronized int getTotalWork()
     {
         return totalWork;
     }
@@ -120,11 +122,13 @@
             return;
         }
 
+        boolean doUpdate = false;
         synchronized (this)
         {
             this.sectionName = statedesc;
 
-            if (updater != null)
+            doUpdate = updater != null;
+            if (doUpdate)
             {
                 if (predictedLength != 0)
                 {
@@ -140,7 +144,7 @@
             current.put(statedesc, new Integer((int) (System.currentTimeMillis() - start)));
         }
 
-        if (updater != null)
+        if (doUpdate)
         {
             JobManager.fireWorkProgressed(this);
         }
@@ -275,7 +279,7 @@
     /* (non-Javadoc)
      * @see org.crosswire.common.progress.Progress#getJobName()
      */
-    public String getJobName()
+    public synchronized String getJobName()
     {
         return jobName;
     }

Modified: trunk/common/src/main/java/org/crosswire/common/util/StringUtil.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/util/StringUtil.java	2007-06-15 21:48:45 UTC (rev 1406)
+++ trunk/common/src/main/java/org/crosswire/common/util/StringUtil.java	2007-06-15 21:53:56 UTC (rev 1407)
@@ -517,6 +517,54 @@
     }
 
     /**
+     * Returns a string representation of the contents of the specified array.
+     * If the array contains other arrays as elements, they are converted to
+     * strings by the {@link Object#toString} method inherited from
+     * <tt>Object</tt>, which describes their <i>identities</i> rather than
+     * their contents.
+     *
+     * <p>The value returned by this method is equal to the value that would
+     * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
+     * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
+     *
+     * @param a the array whose string representation to return
+     * @return a string representation of <tt>a</tt>
+     * @see #deepToString(Object[])
+     * @since 1.5
+    */
+    public static String toString(Object[] a)
+    {
+        if (a == null)
+        {
+            return "null"; //$NON-NLS-1$
+        }
+
+        if (a.length == 0)
+        {
+            return "[]"; //$NON-NLS-1$
+        }
+ 
+        StringBuffer buf = new StringBuffer();
+ 
+        for (int i = 0; i < a.length; i++)
+        {
+            if (i == 0)
+            {
+                buf.append('[');
+            }
+            else
+            {
+                buf.append(", "); //$NON-NLS-1$
+            }
+ 
+            buf.append(String.valueOf(a[i]));
+        }
+ 
+        buf.append("]"); //$NON-NLS-1$
+        return buf.toString();
+    }
+
+    /**
      * An empty immutable <code>String</code> array.
      */
     public static final String[] EMPTY_STRING_ARRAY = new String[0];




More information about the jsword-svn mailing list