Another one that was hanging in my commit-queue for way too long.
Implements some missing bits in the ImageIO Stream impl.

2007-02-10  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/imageio/stream/ImageOutputStreamImpl.java
        (flushBits): Implemented.
        (writeBit): Implemented.
        (writeBits): Implemented.

/Roman

Index: javax/imageio/stream/ImageOutputStreamImpl.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/imageio/stream/ImageOutputStreamImpl.java,v
retrieving revision 1.6
diff -u -1 -5 -r1.6 ImageOutputStreamImpl.java
--- javax/imageio/stream/ImageOutputStreamImpl.java	25 Mar 2006 00:15:10 -0000	1.6
+++ javax/imageio/stream/ImageOutputStreamImpl.java	10 Feb 2007 13:51:09 -0000
@@ -26,78 +26,156 @@
 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package javax.imageio.stream;
 
-import gnu.classpath.NotImplementedException;
-
 import java.io.IOException;
 import java.io.UTFDataFormatException;
 import java.nio.ByteOrder;
 
 /**
  * @author Michael Koch ([EMAIL PROTECTED])
  */
 public abstract class ImageOutputStreamImpl extends ImageInputStreamImpl
   implements ImageOutputStream
 {
   public ImageOutputStreamImpl()
   {
     // Do nothing here.
   }
 
   protected final void flushBits()
-    throws IOException, NotImplementedException
+    throws IOException
   {
-    // FIXME: Implement me.
-    throw new Error("not implemented");
+    checkClosed();
+    if (bitOffset != 0)
+      {
+        int offset = bitOffset;
+        int partial = read();
+        if (partial < 0)
+          {
+            partial = 0;
+            bitOffset = 0;
+          }
+        else
+          {
+            seek(getStreamPosition() - 1);
+            partial &= -1 << (8 - offset);
+          }
+        write(partial);
+      }
   }
 
   public void write(byte[] data)
     throws IOException
   {
     write(data, 0, data.length);
   }
 
   public abstract void write(byte[] data, int offset, int len)
     throws IOException;
 
   public abstract void write(int value)
     throws IOException;
 
   public void writeBit(int bit)
-    throws IOException, NotImplementedException
+    throws IOException
   {
-    // FIXME: Implement me.
-    throw new Error("not implemented");
+    writeBits(1L & bit, 1);
   }
 
   public void writeBits(long bits, int numBits)
-    throws IOException, NotImplementedException
+    throws IOException
   {
-    // FIXME: Implement me.
-    throw new Error("not implemented");
+    checkClosed();
+    // Append chunk of bits to any preexisting bits, if any.
+    if (getStreamPosition() > 0 || bitOffset > 0)
+      {
+        int offs = bitOffset;
+        int partial = read();
+        if (partial != -1)
+          seek(getStreamPosition() - 1);
+        else
+          partial = 0;
+        if (numBits + offs < 8)
+          {
+            // Append complete bits to partial byte.
+            int shift = 8 - (offs + numBits);
+            int mask = -1 >>> (32 - numBits);
+            partial &= ~(mask << shift);
+            partial |= (bits & mask) << shift;
+            write(partial);
+            seek(getStreamPosition() - 1);
+            bitOffset = offs + numBits;
+            numBits = 0;
+          }
+        else
+          {
+            // Append bits and decrease numBits accordingly.
+            int num = 8 - offs;
+            int mask = -1 >>> (32 - num);
+            partial &= ~mask;
+            partial |= (bits >> (numBits - num)) & mask;
+            write(partial);
+            numBits -= num;
+          }
+      }
+
+    // Write out whole chunks, if any.
+    if (numBits > 7)
+      {
+        int remaining = numBits % 8;
+        for (int numBytes = numBits / 8; numBytes > 0; numBytes--)
+          {
+            int shift = (numBytes - 1) * 8 + remaining;
+            int value = (int) ((shift == 0) ? bits & 0xff
+                                            : (bits >> shift) & 0xff);
+            write(value);
+          }
+        numBits = remaining;
+      }
+
+    // Write remaing partial bytes.
+    if (numBits != 0)
+      {
+        int partial = read();
+        if (partial == -1)
+          {
+            seek(getStreamPosition() - 1);
+          }
+        else
+          {
+            partial = 0;
+          }
+        int shift = 8 - numBits;
+        int mask = -1 >>> (32 - numBits);
+        partial &= ~(mask << shift);
+        partial |= (bits & mask) << shift;
+        write(partial);
+        seek(getStreamPosition() - 1);
+        bitOffset = numBits;
+      }
   }
 
   public void writeBoolean(boolean value)
     throws IOException
   {
     writeByte(value ? 1 : 0);
   }
 
   public void writeByte(int value)
     throws IOException
   {
     write(value & 0xff);
   }
 
   public void writeBytes(String data)

Reply via email to