001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.imageio.stream;
019:
020: import java.io.IOException;
021: import java.nio.ByteOrder;
022: import java.util.Arrays;
023:
024: import javax.imageio.stream.ImageInputStreamImplTest.BasicImageInputStreamImpl;
025:
026: import junit.framework.TestCase;
027:
028: public class ImageOutputStreamImplTest extends TestCase {
029:
030: public void testWriteShot() throws IOException {
031: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
032: 2);
033: final ImageInputStream in = new BasicImageInputStreamImpl(
034: out.buff);
035:
036: out.writeShort(Short.MAX_VALUE);
037: assertEquals(Short.MAX_VALUE, in.readShort());
038:
039: out.reset();
040: in.reset();
041: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
042: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
043: out.writeShort(Short.MAX_VALUE);
044: assertEquals(Short.MAX_VALUE, in.readShort());
045: }
046:
047: public void testWriteInt() throws IOException {
048: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
049: 4);
050: final ImageInputStream in = new BasicImageInputStreamImpl(
051: out.buff);
052:
053: out.writeInt(Integer.MAX_VALUE);
054: assertEquals(Integer.MAX_VALUE, in.readInt());
055:
056: out.reset();
057: in.reset();
058: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
059: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
060: out.writeInt(Integer.MAX_VALUE);
061: assertEquals(Integer.MAX_VALUE, in.readInt());
062: }
063:
064: public void testWriteLong() throws IOException {
065: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
066: 8);
067: final ImageInputStream in = new BasicImageInputStreamImpl(
068: out.buff);
069:
070: out.writeLong(Long.MAX_VALUE);
071: assertEquals(Long.MAX_VALUE, in.readLong());
072:
073: out.reset();
074: in.reset();
075: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
076: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
077: out.writeLong(Long.MAX_VALUE);
078: assertEquals(Long.MAX_VALUE, in.readLong());
079: }
080:
081: public void testWriteChars() throws IOException {
082: final char[] buff = new char[4];
083: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
084: 2 * buff.length);
085: final ImageInputStream in = new BasicImageInputStreamImpl(
086: out.buff);
087:
088: out.writeChars("test");
089: in.readFully(buff, 0, 4);
090: assertEquals("test", new String(buff));
091:
092: out.reset();
093: in.reset();
094: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
095: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
096: out.writeChars("test");
097: in.readFully(buff, 0, 4);
098: assertEquals("test", new String(buff));
099:
100: out.reset();
101: in.reset();
102: out.writeChars(" test".toCharArray(), 1, 4);
103: in.readFully(buff, 0, 4);
104: assertEquals("test", new String(buff));
105: }
106:
107: public void testWriteShorts() throws IOException {
108: final short[] src = new short[] { 1, 2, 3 };
109: final short[] dest = new short[3];
110: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
111: 2 * dest.length);
112: final ImageInputStream in = new BasicImageInputStreamImpl(
113: out.buff);
114:
115: out.writeShorts(src, 0, 3);
116: in.readFully(dest, 0, 3);
117: assertTrue(Arrays.equals(src, dest));
118:
119: out.reset();
120: in.reset();
121: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
122: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
123: out.writeShorts(src, 0, 3);
124: in.readFully(dest, 0, 3);
125: assertTrue(Arrays.equals(src, dest));
126: }
127:
128: public void testWriteInts() throws IOException {
129: final int[] src = new int[] { 1, 2, 3 };
130: final int[] dest = new int[3];
131: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
132: 4 * dest.length);
133: final ImageInputStream in = new BasicImageInputStreamImpl(
134: out.buff);
135:
136: out.writeInts(src, 0, 3);
137: in.readFully(dest, 0, 3);
138: assertTrue(Arrays.equals(src, dest));
139:
140: out.reset();
141: in.reset();
142: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
143: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
144: out.writeInts(src, 0, 3);
145: in.readFully(dest, 0, 3);
146: assertTrue(Arrays.equals(src, dest));
147: }
148:
149: public void testWriteLongs() throws IOException {
150: final long[] src = new long[] { 1, 2, 3 };
151: final long[] dest = new long[3];
152: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
153: 8 * dest.length);
154: final ImageInputStream in = new BasicImageInputStreamImpl(
155: out.buff);
156:
157: out.writeLongs(src, 0, 3);
158: in.readFully(dest, 0, 3);
159: assertTrue(Arrays.equals(src, dest));
160:
161: out.reset();
162: in.reset();
163: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
164: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
165: out.writeLongs(src, 0, 3);
166: in.readFully(dest, 0, 3);
167: assertTrue(Arrays.equals(src, dest));
168: }
169:
170: public void testWriteFloats() throws IOException {
171: final float[] src = new float[] { 1, 2, 3 };
172: final float[] dest = new float[3];
173: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
174: 4 * dest.length);
175: final ImageInputStream in = new BasicImageInputStreamImpl(
176: out.buff);
177:
178: out.writeFloats(src, 0, 3);
179: in.readFully(dest, 0, 3);
180: assertTrue(Arrays.equals(src, dest));
181:
182: out.reset();
183: in.reset();
184: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
185: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
186: out.writeFloats(src, 0, 3);
187: in.readFully(dest, 0, 3);
188: assertTrue(Arrays.equals(src, dest));
189: }
190:
191: // FIXME: it looks like there is a bug in Double.doubleToLongBits
192: public void _testWriteDoubles() throws IOException {
193: final double[] src = new double[] { 1, 2, 3 };
194: final double[] dest = new double[3];
195: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
196: 8 * dest.length);
197: final ImageInputStream in = new BasicImageInputStreamImpl(
198: out.buff);
199:
200: out.writeDoubles(src, 0, 3);
201: in.readFully(dest, 0, 3);
202: assertTrue(Arrays.equals(src, dest));
203:
204: out.reset();
205: in.reset();
206: out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
207: in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
208: out.writeDoubles(src, 0, 3);
209: in.readFully(dest, 0, 3);
210: assertTrue(Arrays.equals(src, dest));
211: }
212:
213: public void testWriteUTF() throws IOException {
214: final BasicImageOutputStreamImpl out = new BasicImageOutputStreamImpl(
215: 100);
216: final ImageInputStream in = new BasicImageInputStreamImpl(
217: out.buff);
218:
219: out.writeUTF("test");
220: assertEquals("test", in.readUTF());
221:
222: // FIXME: fails with ByteOrder.LITTLE_ENDIAN
223: // out.reset();
224: // in.reset();
225: // out.setByteOrder(ByteOrder.LITTLE_ENDIAN);
226: // in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
227: // out.writeUTF("test");
228: // assertEquals("test", in.readUTF());
229: }
230:
231: static class BasicImageOutputStreamImpl extends
232: ImageOutputStreamImpl {
233:
234: byte[] buff;
235:
236: public BasicImageOutputStreamImpl(final int capacity) {
237: this (capacity, ByteOrder.BIG_ENDIAN);
238: }
239:
240: public BasicImageOutputStreamImpl(final int capacity,
241: final ByteOrder order) {
242: buff = new byte[capacity];
243: setByteOrder(order);
244: }
245:
246: @Override
247: public void write(int b) throws IOException {
248: buff[(int) streamPos++] = (byte) b;
249: }
250:
251: @Override
252: public void write(byte[] b, int off, int len)
253: throws IOException {
254: System.arraycopy(b, off, buff, (int) streamPos, len);
255: streamPos += len;
256: }
257:
258: @Override
259: public void reset() throws IOException {
260: super .reset();
261: streamPos = 0;
262: bitOffset = 0;
263: }
264:
265: @Override
266: public int read() throws IOException {
267: throw new RuntimeException("Write only");
268: }
269:
270: @Override
271: public int read(byte[] b, int off, int len) throws IOException {
272: throw new RuntimeException("Write only");
273: }
274: }
275: }
|