harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apetre...@apache.org
Subject svn commit: r573172 - in /harmony/enhanced/classlib/trunk/modules/imageio/src: main/java/javax/imageio/stream/ImageOutputStreamImpl.java test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
Date Thu, 06 Sep 2007 07:51:29 GMT
Author: apetrenko
Date: Thu Sep  6 00:51:28 2007
New Revision: 573172

URL: http://svn.apache.org/viewvc?rev=573172&view=rev
Log:
Patch for HARMONY-4745 "[classlib][imageio] There are many unimplemented methods in javax.imageio.stream.ImageOutputStreamImpl"

Added:
    harmony/enhanced/classlib/trunk/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java

Modified: harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java?rev=573172&r1=573171&r2=573172&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/imageio/src/main/java/javax/imageio/stream/ImageOutputStreamImpl.java
Thu Sep  6 00:51:28 2007
@@ -20,136 +20,191 @@
  */
 package javax.imageio.stream;
 
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import java.nio.ByteOrder;
 
-/**
- * @author Rustem V. Rafikov
- * @version $Revision: 1.3 $
- */
 public abstract class ImageOutputStreamImpl extends ImageInputStreamImpl
-        implements ImageOutputStream {
+		implements ImageOutputStream {
 
-    public ImageOutputStreamImpl() {}
+	private final byte[] buff = new byte[8];
 
-    public abstract void write(int b) throws IOException;
+	public ImageOutputStreamImpl() {
+	}
 
-    public void write(byte[] b) throws IOException {
-        write(b, 0, b.length);
-    }
-
-    public abstract void write(byte[] b, int off, int len) throws IOException;
-
-    public void writeBoolean(boolean v) throws IOException {
-        write(v ? 1 : 0);
-    }
-
-    public void writeByte(int v) throws IOException {
-        write(v);
-    }
-
-    public void writeShort(int v) throws IOException {
-        if (byteOrder == ByteOrder.BIG_ENDIAN) {
-
-        } else {
-
-        }
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeChar(int v) throws IOException {
-        writeShort(v);
-    }
-
-    public void writeInt(int v) throws IOException {
-        if (byteOrder == ByteOrder.BIG_ENDIAN) {
-
-        } else {
-
-        }
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeLong(long v) throws IOException {
-        if (byteOrder == ByteOrder.BIG_ENDIAN) {
-
-        } else {
-
-        }
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeFloat(float v) throws IOException {
-        writeInt(Float.floatToIntBits(v));
-    }
-
-    public void writeDouble(double v) throws IOException {
-        writeLong(Double.doubleToLongBits(v));
-    }
-
-    public void writeBytes(String s) throws IOException {
-        write(s.getBytes());
-    }
-
-    public void writeChars(String s) throws IOException {
-        char[] chs = s.toCharArray();
-        writeChars(chs, 0, chs.length);
-    }
-
-    public void writeUTF(String s) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeShorts(short[] s, int off, int len) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeChars(char[] c, int off, int len) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeInts(int[] i, int off, int len) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeLongs(long[] l, int off, int len) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeFloats(float[] f, int off, int len) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeDoubles(double[] d, int off, int len) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeBit(int bit) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    public void writeBits(long bits, int numBits) throws IOException {
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
-
-    protected final void flushBits() throws IOException {
-        if (bitOffset == 0) {
-            return;
-        }
-        
-        //-- TODO implement
-        throw new UnsupportedOperationException("Not implemented yet");
-    }
+	public abstract void write(int b) throws IOException;
+
+	public void write(byte[] b) throws IOException {
+		write(b, 0, b.length);
+	}
+
+	public abstract void write(byte[] b, int off, int len) throws IOException;
+
+	public void writeBoolean(boolean v) throws IOException {
+		write(v ? 1 : 0);
+	}
+
+	public void writeByte(int v) throws IOException {
+		write(v);
+	}
+
+	public void writeShort(int v) throws IOException {
+		if (byteOrder == ByteOrder.BIG_ENDIAN) {
+			buff[0] = (byte) (v >> 8);
+			buff[1] = (byte) v;
+		} else {
+			buff[1] = (byte) (v >> 8);
+			buff[0] = (byte) v;
+		}
+
+		write(buff, 0, 2);
+	}
+
+	public void writeChar(int v) throws IOException {
+		writeShort(v);
+	}
+
+	public void writeInt(int v) throws IOException {
+		if (byteOrder == ByteOrder.BIG_ENDIAN) {
+			buff[0] = (byte) (v >> 24);
+			buff[1] = (byte) (v >> 16);
+			buff[2] = (byte) (v >> 8);
+			buff[3] = (byte) v;
+		} else {
+			buff[3] = (byte) (v >> 24);
+			buff[2] = (byte) (v >> 16);
+			buff[1] = (byte) (v >> 8);
+			buff[0] = (byte) v;
+		}
+
+		write(buff, 0, 4);
+	}
+
+	public void writeLong(long v) throws IOException {
+		if (byteOrder == ByteOrder.BIG_ENDIAN) {
+			buff[0] = (byte) (v >> 56);
+			buff[1] = (byte) (v >> 48);
+			buff[2] = (byte) (v >> 40);
+			buff[3] = (byte) (v >> 32);
+			buff[4] = (byte) (v >> 24);
+			buff[5] = (byte) (v >> 16);
+			buff[6] = (byte) (v >> 8);
+			buff[7] = (byte) (v);
+		} else {
+			buff[7] = (byte) (v >> 56);
+			buff[6] = (byte) (v >> 48);
+			buff[5] = (byte) (v >> 40);
+			buff[4] = (byte) (v >> 32);
+			buff[3] = (byte) (v >> 24);
+			buff[2] = (byte) (v >> 16);
+			buff[1] = (byte) (v >> 8);
+			buff[0] = (byte) (v);
+		}
+
+		write(buff, 0, 8);
+	}
+
+	public void writeFloat(float v) throws IOException {
+		writeInt(Float.floatToIntBits(v));
+	}
+
+	public void writeDouble(double v) throws IOException {
+		writeLong(Double.doubleToLongBits(v));
+	}
+
+	public void writeBytes(String s) throws IOException {
+		write(s.getBytes());
+	}
+
+	public void writeChars(String s) throws IOException {
+		char[] chs = s.toCharArray();
+		writeChars(chs, 0, chs.length);
+	}
+
+	public void writeUTF(String s) throws IOException {
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+		new DataOutputStream(baos).writeUTF(s);
+		write(baos.toByteArray(), 0, baos.size());
+	}
+
+	public void writeShorts(short[] s, int off, int len) throws IOException {
+		if ((off < 0) || (len < 0) || (off + len > s.length)) {
+			throw new IndexOutOfBoundsException();
+		}
+
+		for (int i = 0; i < len; i++) {
+			writeShort(s[off + i]);
+		}
+	}
+
+	public void writeChars(char[] c, int off, int len) throws IOException {
+		if ((off < 0) || (len < 0) || (off + len > c.length)) {
+			throw new IndexOutOfBoundsException();
+		}
+
+		for (int i = 0; i < len; i++) {
+			writeShort(c[off + i]);
+		}
+	}
+
+	public void writeInts(int[] i, int off, int len) throws IOException {
+		if ((off < 0) || (len < 0) || (off + len > i.length)) {
+			throw new IndexOutOfBoundsException();
+		}
+
+		for (int n = 0; n < len; n++) {
+			writeInt(i[off + n]);
+		}
+	}
+
+	public void writeLongs(long[] l, int off, int len) throws IOException {
+		if ((off < 0) || (len < 0) || (off + len > l.length)) {
+			throw new IndexOutOfBoundsException();
+		}
+
+		for (int i = 0; i < len; i++) {
+			writeLong(l[off + i]);
+		}
+	}
+
+	public void writeFloats(float[] f, int off, int len) throws IOException {
+		if ((off < 0) || (len < 0) || (off + len > f.length)) {
+			throw new IndexOutOfBoundsException();
+		}
+
+		for (int i = 0; i < len; i++) {
+			writeFloat(f[off + i]);
+		}
+	}
+
+	public void writeDoubles(double[] d, int off, int len) throws IOException {
+		if ((off < 0) || (len < 0) || (off + len > d.length)) {
+			throw new IndexOutOfBoundsException();
+		}
+
+		for (int i = 0; i < len; i++) {
+			writeDouble(d[off + i]);
+		}
+	}
+
+	public void writeBit(int bit) throws IOException {
+		// -- TODO implement
+		throw new UnsupportedOperationException("Not implemented yet");
+	}
+
+	public void writeBits(long bits, int numBits) throws IOException {
+		// -- TODO implement
+		throw new UnsupportedOperationException("Not implemented yet");
+	}
+
+	protected final void flushBits() throws IOException {
+		if (bitOffset == 0) {
+			return;
+		}
+
+		// -- TODO implement
+		throw new UnsupportedOperationException("Not implemented yet");
+	}
 }

Added: harmony/enhanced/classlib/trunk/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java?rev=573172&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
Thu Sep  6 00:51:28 2007
@@ -0,0 +1,260 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package javax.imageio.stream;
+
+import java.io.IOException;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+
+import javax.imageio.stream.ImageInputStreamImplTest.BasicImageInputStreamImpl;
+
+import junit.framework.TestCase;
+
+public class ImageOutputStreamImplTest extends TestCase {
+
+	public void testWriteShot() throws IOException {
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(2);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeShort(Short.MAX_VALUE);
+		assertEquals(Short.MAX_VALUE, in.readShort());
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeShort(Short.MAX_VALUE);
+		assertEquals(Short.MAX_VALUE, in.readShort());
+	}
+
+	public void testWriteInt() throws IOException {
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(4);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeInt(Integer.MAX_VALUE);
+		assertEquals(Integer.MAX_VALUE, in.readInt());
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeInt(Integer.MAX_VALUE);
+		assertEquals(Integer.MAX_VALUE, in.readInt());
+	}
+
+	public void testWriteLong() throws IOException {
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(8);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeLong(Long.MAX_VALUE);
+		assertEquals(Long.MAX_VALUE, in.readLong());
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeLong(Long.MAX_VALUE);
+		assertEquals(Long.MAX_VALUE, in.readLong());
+	}
+
+	public void testWriteChars() throws IOException {
+		final char[] buff = new char[4];
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				2 * buff.length);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeChars("test");
+		in.readFully(buff, 0, 4);
+		assertEquals("test", new String(buff));
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeChars("test");
+		in.readFully(buff, 0, 4);
+		assertEquals("test", new String(buff));
+
+		out.reset();
+		in.reset();
+		out.writeChars(" test".toCharArray(), 1, 4);
+		in.readFully(buff, 0, 4);
+		assertEquals("test", new String(buff));
+	}
+
+	public void testWriteShorts() throws IOException {
+		final short[] src = new short[] { 1, 2, 3 };
+		final short[] dest = new short[3];
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				2 * dest.length);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeShorts(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeShorts(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+	}
+
+	public void testWriteInts() throws IOException {
+		final int[] src = new int[] { 1, 2, 3 };
+		final int[] dest = new int[3];
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				4 * dest.length);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeInts(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeInts(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+	}
+
+	public void testWriteLongs() throws IOException {
+		final long[] src = new long[] { 1, 2, 3 };
+		final long[] dest = new long[3];
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				8 * dest.length);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeLongs(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeLongs(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+	}
+
+	public void testWriteFloats() throws IOException {
+		final float[] src = new float[] { 1, 2, 3 };
+		final float[] dest = new float[3];
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				4 * dest.length);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeFloats(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeFloats(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+	}
+
+	// FIXME: it looks like there is a bug in Double.doubleToLongBits
+	public void _testWriteDoubles() throws IOException {
+		final double[] src = new double[] { 1, 2, 3 };
+		final double[] dest = new double[3];
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				8 * dest.length);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeDoubles(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+
+		out.reset();
+		in.reset();
+		out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		out.writeDoubles(src, 0, 3);
+		in.readFully(dest, 0, 3);
+		assertTrue(Arrays.equals(src, dest));
+	}
+
+	public void testWriteUTF() throws IOException {
+		final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
+				100);
+		final ImageInputStream in = new BasicImageInputStreamImpl(out.buff);
+
+		out.writeUTF("test");
+		assertEquals("test", in.readUTF());
+
+		// FIXME: fails with ByteOrder.LITTLE_ENDIAN
+		// out.reset();
+		// in.reset();
+		// out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		// in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
+		// out.writeUTF("test");
+		// assertEquals("test", in.readUTF());
+	}
+
+	static class BasicImageOutputStreamImpl extends ImageOutputStreamImpl {
+
+		byte[] buff;
+
+		public BasicImageOutputStreamImpl(final int capacity) {
+			this(capacity, ByteOrder.BIG_ENDIAN);
+		}
+
+		public BasicImageOutputStreamImpl(final int capacity,
+				final ByteOrder order) {
+			buff = new byte[capacity];
+			setByteOrder(order);
+		}
+
+		@Override
+		public void write(int b) throws IOException {
+			buff[(int) streamPos++] = (byte) b;
+		}
+
+		@Override
+		public void write(byte[] b, int off, int len) throws IOException {
+			System.arraycopy(b, off, buff, (int) streamPos, len);
+			streamPos += len;
+		}
+
+		@Override
+		public void reset() throws IOException {
+			super.reset();
+			streamPos = 0;
+			bitOffset = 0;
+		}
+
+		@Override
+		public int read() throws IOException {
+			throw new RuntimeException("Write only");
+		}
+
+		@Override
+		public int read(byte[] b, int off, int len) throws IOException {
+			throw new RuntimeException("Write only");
+		}
+	}
+}

Propchange: harmony/enhanced/classlib/trunk/modules/imageio/src/test/java/javax/imageio/stream/ImageOutputStreamImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message