KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > AbstractRandomAccessRO


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package com.imagero.uio;
33
34 import com.imagero.uio.buffer.arrays.AbstractArrayBufferManager;
35 import com.imagero.uio.buffer.arrays.CharArrayBufferManager;
36 import com.imagero.uio.buffer.arrays.DoubleArrayBufferManager;
37 import com.imagero.uio.buffer.arrays.FloatArrayBufferManager;
38 import com.imagero.uio.buffer.arrays.IntArrayBufferManager;
39 import com.imagero.uio.buffer.arrays.LongArrayBufferManager;
40 import com.imagero.uio.buffer.arrays.ShortArrayBufferManager;
41
42 import java.io.DataInputStream JavaDoc;
43 import java.io.EOFException JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * @author Andrei Kouznetsov
48  * Date: 13.11.2003
49  * Time: 20:02:12
50  */

51 public abstract class AbstractRandomAccessRO implements RandomAccessRO {
52
53     protected abstract int _read() throws IOException JavaDoc;
54
55     private DataInputBE dibe = new DataInputBE();
56     private DataInputLE dile = new DataInputLE();
57
58     EDataInput dataInput;
59
60     int byteOrder;
61
62     public final byte readByte() throws IOException JavaDoc {
63         return (byte) (_read());
64     }
65
66     public final int readUnsignedByte() throws IOException JavaDoc {
67         return _read();
68     }
69
70     public boolean readBoolean() throws IOException JavaDoc {
71         return (_read() != 0);
72     }
73
74     public float readFloat() throws IOException JavaDoc {
75         return Float.intBitsToFloat(readInt());
76     }
77
78     public double readDouble() throws IOException JavaDoc {
79         return Double.longBitsToDouble(readLong());
80     }
81
82     public int skipBytes(int n) throws IOException JavaDoc {
83         if (n <= 0) {
84             return 0;
85         }
86         long len = length();
87         long pos = getFilePointer();
88         long newpos = pos + n;
89         if (newpos > len) {
90             newpos = len;
91         }
92         seek(newpos);
93         return (int) (newpos - pos);
94     }
95
96     public void readFully(byte[] b) throws IOException JavaDoc {
97         readFully(b, 0, b.length);
98     }
99
100     public void readFully(byte[] b, int off, int len) throws IOException JavaDoc {
101         int n = 0;
102         do {
103             int count = read(b, off + n, len - n);
104             if (count < 0) {
105                 throw new EOFException JavaDoc();
106             }
107             n += count;
108         }
109         while (n < len);
110     }
111
112     public void readFully(short[] dest) throws IOException JavaDoc {
113         readFully(dest, getByteOrder());
114     }
115
116     public void readFully(short[] dest, int byteOrder) throws IOException JavaDoc {
117         readFully(dest, 0, dest.length, byteOrder);
118     }
119
120     public void readFully(short[] dest, int destOffset, int len) throws IOException JavaDoc {
121         readFully(dest, destOffset, len, getByteOrder());
122     }
123
124     public void readFully(short[] dest, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
125         AbstractArrayBufferManager sm = new ShortArrayBufferManager(dest);
126         writeTo(sm, destOffset, len, byteOrder);
127     }
128
129     public void readFully(char[] dest) throws IOException JavaDoc {
130         readFully(dest, getByteOrder());
131     }
132
133     public void readFully(char[] dest, int byteOrder) throws IOException JavaDoc {
134         readFully(dest, 0, dest.length, byteOrder);
135     }
136
137     public void readFully(char[] dest, int destOffset, int len) throws IOException JavaDoc {
138         readFully(dest, destOffset, len, getByteOrder());
139     }
140
141     public void readFully(char[] dest, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
142         AbstractArrayBufferManager sm = new CharArrayBufferManager(dest, 0, len);
143         writeTo(sm, destOffset, len, byteOrder);
144     }
145
146
147     public void readFully(int[] dest) throws IOException JavaDoc {
148         readFully(dest, getByteOrder());
149     }
150
151     public void readFully(int[] dest, int byteOrder) throws IOException JavaDoc {
152         readFully(dest, 0, dest.length, byteOrder);
153     }
154
155     public void readFully(int[] dest, int destOffset, int len) throws IOException JavaDoc {
156         readFully(dest, destOffset, len, getByteOrder());
157     }
158
159     public void readFully(int[] dest, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
160         AbstractArrayBufferManager sm = new IntArrayBufferManager(dest, 0, len);
161         writeTo(sm, destOffset, len, byteOrder);
162     }
163
164     public void readFully(float[] dest) throws IOException JavaDoc {
165         readFully(dest, getByteOrder());
166     }
167
168     public void readFully(float[] dest, int byteOrder) throws IOException JavaDoc {
169         readFully(dest, 0, dest.length, byteOrder);
170     }
171
172     public void readFully(float[] dest, int destOffset, int len) throws IOException JavaDoc {
173         readFully(dest, destOffset, len, getByteOrder());
174     }
175
176     public void readFully(float[] dest, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
177         AbstractArrayBufferManager sm = new FloatArrayBufferManager(dest);
178         writeTo(sm, destOffset, len, byteOrder);
179     }
180
181     public void readFully(long[] dest) throws IOException JavaDoc {
182         readFully(dest, getByteOrder());
183     }
184
185     public void readFully(long[] dest, int byteOrder) throws IOException JavaDoc {
186         readFully(dest, 0, dest.length, byteOrder);
187     }
188
189     public void readFully(long[] dest, int destOffset, int len) throws IOException JavaDoc {
190         readFully(dest, destOffset, len, getByteOrder());
191     }
192
193     public void readFully(long[] dest, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
194         AbstractArrayBufferManager sm = new LongArrayBufferManager(dest);
195         writeTo(sm, destOffset, len, byteOrder);
196     }
197
198     public void readFully(double[] dest) throws IOException JavaDoc {
199         readFully(dest, getByteOrder());
200     }
201
202     public void readFully(double[] dest, int byteOrder) throws IOException JavaDoc {
203         readFully(dest, 0, dest.length, byteOrder);
204     }
205
206     public void readFully(double[] dest, int destOffset, int len) throws IOException JavaDoc {
207         readFully(dest, destOffset, len, getByteOrder());
208     }
209
210     public void readFully(double[] dest, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
211         AbstractArrayBufferManager sm = new DoubleArrayBufferManager(dest);
212         writeTo(sm, destOffset, len, byteOrder);
213     }
214
215     private void writeTo(AbstractArrayBufferManager sm, int destOffset, int len, int byteOrder) throws IOException JavaDoc {
216         int length = len * sm.getUnitSize();
217         int offset = destOffset * sm.getUnitSize();
218         byte[] b = new byte[length];
219         readFully(b);
220         RandomAccess ra = new RandomAccessBuffer(sm, byteOrder);
221         ra.write(b, offset, length);
222         ra.close();
223     }
224
225     public String JavaDoc readLine() throws IOException JavaDoc {
226         return new String JavaDoc(readByteLine());
227     }
228
229     public byte[] readByteLine() throws IOException JavaDoc {
230         long start = getFilePointer();
231         long end = start;
232         boolean finished = false;
233         int length = 0;
234         while (!finished) {
235             switch (read()) {
236                 case -1:
237                 case '\n':
238                     finished = true;
239                     end = getFilePointer();
240                     length = (int) (end - start);
241                     break;
242                 case '\r':
243                     finished = true;
244                     end = getFilePointer();
245                     length = (int) (end - start);
246                     if ((read()) == '\n') {
247                         end = getFilePointer();
248                     }
249                     break;
250             }
251         }
252         byte[] b = new byte[length];
253         seek(start);
254         readFully(b);
255         seek(end);
256         return b;
257     }
258
259     public String JavaDoc readUTF() throws IOException JavaDoc {
260         return DataInputStream.readUTF(this);
261     }
262
263     public short readShort() throws IOException JavaDoc {
264         return dataInput.readShort();
265     }
266
267     public int readUnsignedShort() throws IOException JavaDoc {
268         return dataInput.readUnsignedShort();
269     }
270
271     public char readChar() throws IOException JavaDoc {
272         return dataInput.readChar();
273     }
274
275     public int readInt() throws IOException JavaDoc {
276         return dataInput.readInt();
277     }
278
279     public long readLong() throws IOException JavaDoc {
280         return dataInput.readLong();
281     }
282
283     /**
284      * not all images are tiffs, so we don't throw exception if called from constructor
285      * @param byteOrder
286      * @throws IOException
287      */

288     protected void _setByteOrder(int byteOrder) throws IOException JavaDoc {
289         if (byteOrder == RandomAccessFactory.AUTO_ENDIAN) {
290             byteOrder = readByteOrder();
291         }
292         if (this.byteOrder == byteOrder) {
293             if (this.dataInput != null) {
294                 return;
295             }
296         }
297         switch (byteOrder) {
298             case BIG_ENDIAN:
299                 this.byteOrder = byteOrder;
300                 dataInput = dibe;
301                 break;
302             case LITTLE_ENDIAN:
303                 this.byteOrder = byteOrder;
304                 dataInput = dile;
305                 break;
306             default:
307                 this.byteOrder = BIG_ENDIAN;
308                 dataInput = dibe;
309         }
310     }
311
312     public void setByteOrder(int byteOrder) throws IOException JavaDoc {
313         if (this.byteOrder == RandomAccessFactory.AUTO_ENDIAN) {
314             this.byteOrder = readByteOrder();
315         }
316         if (this.byteOrder == byteOrder) {
317             if (this.dataInput != null) {
318                 return;
319             }
320         }
321         switch (byteOrder) {
322             case BIG_ENDIAN:
323                 this.byteOrder = byteOrder;
324                 dataInput = dibe;
325                 break;
326             case LITTLE_ENDIAN:
327                 this.byteOrder = byteOrder;
328                 dataInput = dile;
329                 break;
330             default:
331                 throw new RuntimeException JavaDoc("Wrong byteOrder:" + Integer.toHexString(byteOrder));
332         }
333     }
334
335     /**
336      * try to read byte order from file (assuming it is a tiff file)
337      *
338      * @throws IOException
339      */

340     protected int readByteOrder() throws IOException JavaDoc {
341         long pos = getFilePointer();
342         seek(0);
343         int byteOrder = (_read() << 8) | _read();
344         seek(pos);
345         return byteOrder == LITTLE_ENDIAN ? byteOrder : BIG_ENDIAN;
346     }
347
348     public int getByteOrder() {
349         return byteOrder;
350     }
351
352     class DataInputLE implements EDataInput {
353         public short readShort() throws IOException JavaDoc {
354             int ch = _read() | (_read() << 8);
355             return (short) ch;
356         }
357
358         public int readUnsignedShort() throws IOException JavaDoc {
359             int ch = _read() | (_read() << 8);
360             return ch;
361         }
362
363         public char readChar() throws IOException JavaDoc {
364             int ch = _read() | (_read() << 8);
365             return (char) ch;
366         }
367
368         public int readInt() throws IOException JavaDoc {
369             return _read() | (_read() << 8) | (_read() << 16) | (_read() << 24);
370         }
371
372         public long readLong() throws IOException JavaDoc {
373             return (readInt() & 0xFFFFFFFFL) | ((long) readInt() << 32);
374         }
375     }
376
377     class DataInputBE implements EDataInput {
378         public short readShort() throws IOException JavaDoc {
379             return (short) ((_read() << 8) | _read());
380         }
381
382         public int readUnsignedShort() throws IOException JavaDoc {
383             return (_read() << 8) | _read();
384         }
385
386         public char readChar() throws IOException JavaDoc {
387             return (char) ((_read() << 8) | _read());
388         }
389
390         public int readInt() throws IOException JavaDoc {
391             return (_read() << 24) | (_read() << 16) | (_read() << 8) | _read();
392         }
393
394         public long readLong() throws IOException JavaDoc {
395             return (((long) readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
396         }
397     }
398 }
399
Popular Tags