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:
023: import junit.framework.TestCase;
024:
025: public class ImageInputStreamImplTest extends TestCase {
026:
027: public void testReadLine() throws IOException {
028: final ImageInputStream in = new BasicImageInputStreamImpl(
029: "line1\nline2\rline3\r\nline4".getBytes());
030:
031: assertEquals("line1", in.readLine());
032: assertEquals("line2", in.readLine());
033: assertEquals("line3", in.readLine());
034: assertEquals("line4", in.readLine());
035: }
036:
037: public void testReadBit() throws IOException {
038: final ImageInputStream in = new BasicImageInputStreamImpl(
039: new byte[] { (byte) 150 });
040:
041: assertEquals(1, in.readBit());
042: assertEquals(0, in.readBit());
043: assertEquals(0, in.readBit());
044: assertEquals(1, in.readBit());
045: assertEquals(0, in.readBit());
046: assertEquals(1, in.readBit());
047: assertEquals(1, in.readBit());
048: assertEquals(0, in.readBit());
049: }
050:
051: public void testReadBits() throws IOException {
052: final ImageInputStream in = new BasicImageInputStreamImpl(
053: Long.MAX_VALUE);
054:
055: assertEquals(3, in.readBits(3));
056: assertEquals(1023, in.readBits(10));
057: in.reset();
058: assertEquals(Long.MAX_VALUE, in.readBits(64));
059: }
060:
061: public void testReadLong() throws IOException {
062: ImageInputStream in = new BasicImageInputStreamImpl(
063: Long.MAX_VALUE);
064:
065: assertEquals(Long.MAX_VALUE, in.readLong());
066:
067: in = new BasicImageInputStreamImpl(Long.MAX_VALUE,
068: ByteOrder.LITTLE_ENDIAN);
069: assertEquals(Long.MAX_VALUE, in.readLong());
070: }
071:
072: public void testReadInt() throws IOException {
073: ImageInputStream in = new BasicImageInputStreamImpl(
074: Integer.MAX_VALUE);
075:
076: in.readInt();
077: assertEquals(Integer.MAX_VALUE, in.readInt());
078:
079: in = new BasicImageInputStreamImpl(Integer.MAX_VALUE,
080: ByteOrder.LITTLE_ENDIAN);
081: assertEquals(Integer.MAX_VALUE, in.readInt());
082: }
083:
084: public void testReadShort() throws IOException {
085: ImageInputStream in = new BasicImageInputStreamImpl(
086: Short.MAX_VALUE);
087:
088: in.readInt();
089: in.readShort();
090: assertEquals(Short.MAX_VALUE, in.readShort());
091:
092: in = new BasicImageInputStreamImpl(Short.MAX_VALUE,
093: ByteOrder.LITTLE_ENDIAN);
094: assertEquals(Short.MAX_VALUE, in.readShort());
095: }
096:
097: static class BasicImageInputStreamImpl extends ImageInputStreamImpl {
098: final byte[] buff;
099:
100: public BasicImageInputStreamImpl(final long value) {
101: this (value, ByteOrder.BIG_ENDIAN);
102: }
103:
104: public BasicImageInputStreamImpl(final long value,
105: final ByteOrder order) {
106: if (order == ByteOrder.BIG_ENDIAN) {
107: buff = new byte[] { (byte) (value >> 56),
108: (byte) (value >> 48), (byte) (value >> 40),
109: (byte) (value >> 32), (byte) (value >> 24),
110: (byte) (value >> 16), (byte) (value >> 8),
111: (byte) (value) };
112: } else {
113: buff = new byte[] { (byte) value, (byte) (value >> 8),
114: (byte) (value >> 16), (byte) (value >> 24),
115: (byte) (value >> 32), (byte) (value >> 40),
116: (byte) (value >> 48), (byte) (value >> 56) };
117: }
118: setByteOrder(order);
119: }
120:
121: public BasicImageInputStreamImpl(final byte[] buff) {
122: this .buff = buff;
123: }
124:
125: @Override
126: public int read() throws IOException {
127: bitOffset = 0;
128: return (streamPos >= buff.length) ? -1
129: : (buff[(int) streamPos++] & 0xff);
130: }
131:
132: @Override
133: public int read(byte[] b, int off, int len) throws IOException {
134: int i = 0;
135: int curByte = -1;
136:
137: for (; (i < len) && ((curByte = read()) != -1); i++) {
138: b[off] = (byte) curByte;
139: off++;
140: }
141:
142: return (i == 0) && (curByte == -1) ? -1 : i;
143: }
144:
145: @Override
146: public void reset() throws IOException {
147: super .reset();
148: streamPos = 0;
149: bitOffset = 0;
150: }
151: }
152: }
|