harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r963698 - in /harmony/enhanced/java/trunk/classlib/modules/imageio/src: main/java/javax/imageio/stream/ImageOutputStreamImpl.java test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
Date Tue, 13 Jul 2010 12:45:43 GMT
Author: tellison
Date: Tue Jul 13 12:45:43 2010
New Revision: 963698

URL: http://svn.apache.org/viewvc?rev=963698&view=rev
Log:
Apply patch for HARMONY-6578 (Implement missing functionalities in ImageOutputStreamImpl class)

Modified:
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
    harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java?rev=963698&r1=963697&r2=963698&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
Tue Jul 13 12:45:43 2010
@@ -190,22 +190,71 @@ public abstract class ImageOutputStreamI
 		}
 	}
 
-	public void writeBit(int bit) throws IOException, NotImplementedException {
-        // TODO: implement
-        throw new NotImplementedException();
+	public void writeBit(int bit) throws IOException {
+	    writeBits((long) bit, 1);
 	}
 
-	public void writeBits(long bits, int numBits) throws IOException, NotImplementedException
{
-        // TODO: implement
-        throw new NotImplementedException();
+	public void writeBits(long bits, int numBits) throws IOException {
+	    checkClosed();
+	    
+	    if (bitOffset > 0) {
+	        int oldBitOffset = bitOffset;
+	        int currentByte = read();
+	        if (currentByte == -1) {
+	            currentByte = 0;
+	        } else {
+	            seek(getStreamPosition() - 1);
+	        }
+	        
+	        int num = 8 - oldBitOffset;
+	        if (numBits >= num) {
+	            int mask = -1 >>> (32 - num);
+		    currentByte &= ~mask;
+		    numBits -= num;
+		    currentByte |= ((bits >> numBits) & mask);
+		    write(currentByte);
+	        } else {
+	            int offset = oldBitOffset + numBits;
+	            int mask = -1 >>> numBits;
+		    currentByte &= ~(mask << (8 - offset));
+		    currentByte |= ((bits & mask) << (8 - offset));
+		    write(currentByte);
+		    seek(getStreamPosition() - 1);
+		    bitOffset = offset;
+		    numBits = 0;
+	        }
+	    }
+	    
+	    while (numBits > 7) {
+	        int mask = -1 >>> 24;
+	        int currentByte = (int) ((bits >> (numBits - 8)) & mask);
+	        write(currentByte);
+	        numBits -= 8;
+	    }
+	    
+	    if (numBits > 0) {
+	        int mask = -1 >>> 24;
+	        int currentByte = (int) ((bits << (8 - numBits)) & mask);
+	        write(currentByte);
+	        seek(getStreamPosition() - 1);
+	        bitOffset = numBits;
+	    }
 	}
 
-	protected final void flushBits() throws IOException, NotImplementedException {
-		if (bitOffset == 0) {
-			return;
-		}
-
-		// TODO: implement
-        throw new NotImplementedException();
+	protected final void flushBits() throws IOException {
+	    checkClosed();
+	    if (bitOffset == 0) {
+	        return;
+	    }
+	    int offset = bitOffset;
+	    int currentByte = read();
+	    if (currentByte == -1) {
+	        currentByte = 0;
+	        bitOffset = 0;
+	    } else {
+	        seek(getStreamPosition() - 1);
+	        currentByte &= -1 << (8 - offset);
+	    }
+	    write(currentByte);	    
 	}
 }

Modified: harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java?rev=963698&r1=963697&r2=963698&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
Tue Jul 13 12:45:43 2010
@@ -17,7 +17,9 @@
 
 package javax.imageio.stream;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.nio.ByteOrder;
 import java.util.Arrays;
 
@@ -176,6 +178,19 @@ public class ImageOutputStreamImplTest e
 		in.readFully(dest, 0, 3);
 		assertTrue(Arrays.equals(src, dest));
 	}
+	
+	public void testWriteBits() throws IOException {
+	    final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(8);
+	    final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+	    
+	    out.writeBits(0xff, 7);
+	    out.writeBit(0);
+	    out.writeBits(0xabcdef, 24);
+	    assertEquals("writeBits failed", 0xfe, in.read());
+	    assertEquals("writeBits failed", 0xab, in.read());
+	    assertEquals("writeBits failed", 0xcd, in.read());
+	    assertEquals("writeBits failed", 0xef, in.read());
+	}
 
 	// FIXME: it looks like there is a bug in Double.doubleToLongBits
 	public void _testWriteDoubles() throws IOException {
@@ -249,12 +264,22 @@ public class ImageOutputStreamImplTest e
 
 		@Override
 		public int read() throws IOException {
-			throw new RuntimeException("Write only");
+		    bitOffset = 0;
+                    return (streamPos >= buff.length) ? -1
+                            : (buff[(int) streamPos++] & 0xff);
 		}
 
 		@Override
 		public int read(byte[] b, int off, int len) throws IOException {
-			throw new RuntimeException("Write only");
+		    int i = 0;
+                    int curByte = -1;
+
+                    for (; (i < len) && ((curByte = read()) != -1); i++) {
+                        b[off] = (byte) curByte;
+                        off++;
+                    }
+
+                    return (i == 0) && (curByte == -1) ? -1 : i;
 		}
 	}
 }



Mime
View raw message