KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.DataOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 /**
39  * implementation of RandomAccess
40  *
41  * @author Andrei Kouznetsov
42  * Date: 08.11.2003
43  * Time: 21:37:03
44  */

45 public abstract class AbstractRandomAccess extends AbstractRandomAccessRO implements RandomAccess {
46
47     private DataOutputBE dobe = new DataOutputBE();
48     private DataOutputLE dole = new DataOutputLE();
49
50     EDataOutput dataOutput;
51
52     public void writeBoolean(boolean b) throws IOException JavaDoc {
53         write(b ? 1 : 0);
54     }
55
56     public void writeByte(int b) throws IOException JavaDoc {
57         write(b);
58     }
59
60     public void write(short[] sh) throws IOException JavaDoc {
61         write(sh, getByteOrder());
62     }
63
64     public void write(short[] sh, int byteOrder) throws IOException JavaDoc {
65         write(sh, 0, sh.length, byteOrder);
66     }
67
68     public void write(short[] sh, int offset, int length) throws IOException JavaDoc {
69         write(sh, offset, length, getByteOrder());
70     }
71
72     public void write(short[] sh, int offset, int length, int byteOrder) throws IOException JavaDoc {
73         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(sh, offset, length);
74         ro.setByteOrder(byteOrder);
75         byte[] b = new byte[length << 1];
76         ro.readFully(b);
77         write(b);
78     }
79
80     public void write(char[] sh) throws IOException JavaDoc {
81         write(sh, getByteOrder());
82     }
83
84     public void write(char[] sh, int byteOrder) throws IOException JavaDoc {
85         write(sh, 0, sh.length, byteOrder);
86     }
87
88     public void write(char[] sh, int offset, int length) throws IOException JavaDoc {
89         write(sh, offset, length, getByteOrder());
90     }
91
92     public void write(char[] sh, int offset, int length, int byteOrder) throws IOException JavaDoc {
93         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(sh, offset, length);
94         ro.setByteOrder(byteOrder);
95         byte[] b = new byte[length << 1];
96         ro.readFully(b);
97         write(b);
98     }
99
100     public void write(int[] source) throws IOException JavaDoc {
101         write(source, getByteOrder());
102     }
103
104     public void write(int[] source, int byteOrder) throws IOException JavaDoc {
105         write(source, 0, source.length, byteOrder);
106     }
107
108     public void write(int[] source, int offset, int length) throws IOException JavaDoc {
109         write(source, offset, length, getByteOrder());
110     }
111
112     public void write(int[] source, int offset, int length, int byteOrder) throws IOException JavaDoc {
113         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length);
114         ro.setByteOrder(byteOrder);
115         byte[] b = new byte[length << 2];
116         ro.readFully(b);
117         write(b);
118     }
119
120     public void write(float[] source) throws IOException JavaDoc {
121         write(source, getByteOrder());
122     }
123
124     public void write(float[] source, int byteOrder) throws IOException JavaDoc {
125         write(source, 0, source.length, byteOrder);
126     }
127
128     public void write(float[] source, int offset, int length) throws IOException JavaDoc {
129         write(source, offset, length, getByteOrder());
130     }
131
132     public void write(float[] source, int offset, int length, int byteOrder) throws IOException JavaDoc {
133         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length);
134         ro.setByteOrder(byteOrder);
135         byte[] b = new byte[length << 2];
136         ro.readFully(b);
137         write(b);
138     }
139
140     public void write(long[] source) throws IOException JavaDoc {
141         write(source, getByteOrder());
142     }
143
144     public void write(long[] source, int byteOrder) throws IOException JavaDoc {
145         write(source, 0, source.length, byteOrder);
146     }
147
148     public void write(long[] source, int offset, int length) throws IOException JavaDoc {
149         write(source, offset, length, getByteOrder());
150     }
151
152     public void write(long[] source, int offset, int length, int byteOrder) throws IOException JavaDoc {
153         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length);
154         ro.setByteOrder(byteOrder);
155         byte[] b = new byte[length << 3];
156         ro.readFully(b);
157         write(b);
158     }
159
160     public void write(double[] source) throws IOException JavaDoc {
161         write(source, getByteOrder());
162     }
163
164     public void write(double[] source, int byteOrder) throws IOException JavaDoc {
165         write(source, 0, source.length, byteOrder);
166     }
167
168     public void write(double[] source, int offset, int length) throws IOException JavaDoc {
169         write(source, offset, length, getByteOrder());
170     }
171
172     public void write(double[] source, int offset, int length, int byteOrder) throws IOException JavaDoc {
173         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(source, offset, length);
174         ro.setByteOrder(byteOrder);
175         byte[] b = new byte[length << 3];
176         ro.readFully(b);
177         write(b);
178     }
179
180     public final void writeFloat(float v) throws IOException JavaDoc {
181         final int a = Float.floatToIntBits(v);
182         writeInt(a);
183     }
184
185     public final void writeDouble(double v) throws IOException JavaDoc {
186         writeLong(Double.doubleToLongBits(v));
187     }
188
189     public void writeBytes(String JavaDoc s) throws IOException JavaDoc {
190         write(s.getBytes());
191     }
192
193     public void writeChars(String JavaDoc s) throws IOException JavaDoc {
194         for(int i = 0; i < s.length(); i++) {
195             writeChar(s.charAt(i));
196         }
197     }
198
199     public void writeUTF(String JavaDoc str) throws IOException JavaDoc {
200         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(str.length());
201         DataOutputStream JavaDoc dataOut = new DataOutputStream JavaDoc(out);
202         dataOut.writeUTF(str);
203         dataOut.flush();
204         dataOut.close();
205         byte[] b = out.toByteArray();
206         write(b);
207     }
208
209     public void writeShort(int v) throws IOException JavaDoc {
210         dataOutput.writeShort(v);
211     }
212
213     public void writeChar(int v) throws IOException JavaDoc {
214         dataOutput.writeChar(v);
215     }
216
217     public void writeInt(int v) throws IOException JavaDoc {
218         dataOutput.writeInt(v);
219     }
220
221     public void writeLong(long v) throws IOException JavaDoc {
222         dataOutput.writeLong(v);
223     }
224
225     public void setByteOrder(int byteOrder) throws IOException JavaDoc {
226         if(byteOrder == RandomAccessFactory.AUTO_ENDIAN) {
227             byteOrder = readByteOrder();
228         }
229         if(this.byteOrder == byteOrder) {
230             return;
231         }
232         switch(byteOrder) {
233             case BIG_ENDIAN:
234                 dataOutput = dobe;
235                 break;
236             case LITTLE_ENDIAN:
237                 dataOutput = dole;
238                 break;
239             default:
240                 throw new RuntimeException JavaDoc("Wrong byteOrder:" + byteOrder);
241         }
242         super.setByteOrder(byteOrder);
243     }
244
245     /**
246      * not all images are tiffs, so we don't throw exception if called from constructor
247      * @param byteOrder
248      * @throws IOException
249      */

250     protected void _setByteOrder(int byteOrder) throws IOException JavaDoc {
251         if(byteOrder == RandomAccessFactory.AUTO_ENDIAN) {
252             byteOrder = readByteOrder();
253         }
254         if(this.byteOrder == byteOrder) {
255             return;
256         }
257         switch(byteOrder) {
258             case BIG_ENDIAN:
259                 dataOutput = dobe;
260                 break;
261             case LITTLE_ENDIAN:
262                 dataOutput = dole;
263                 break;
264             default:
265                 byteOrder = BIG_ENDIAN;
266                 dataOutput = dobe;
267         }
268         super._setByteOrder(byteOrder);
269     }
270
271     class DataOutputLE implements EDataOutput {
272         public final void writeShort(int a) throws IOException JavaDoc {
273             write(a & 0xFF);
274             write((a >> 8) & 0xFF);
275         }
276
277         public final void writeChar(int a) throws IOException JavaDoc {
278             write(a & 0xFF);
279             write((a >> 8) & 0xFF);
280         }
281
282         public final void writeInt(int a) throws IOException JavaDoc {
283             write(a & 0xFF);
284             write((a >>> 8) & 0xFF);
285             write((a >>> 16) & 0xFF);
286             write((a >>> 24) & 0xFF);
287         }
288
289         public final void writeLong(long a) throws IOException JavaDoc {
290             writeInt((int) (a & 0xFFFFFFFF));
291             writeInt((int) ((a >>> 32) & 0xFFFFFFFF));
292         }
293     }
294
295     class DataOutputBE implements EDataOutput {
296         public void writeShort(int a) throws IOException JavaDoc {
297             write((a >>> 8) & 0xFF);
298             write(a & 0xFF);
299         }
300
301         public void writeChar(int a) throws IOException JavaDoc {
302             write((a >>> 8) & 0xFF);
303             write(a & 0xFF);
304         }
305
306         public void writeInt(int a) throws IOException JavaDoc {
307             write((a >>> 24) & 0xFF);
308             write((a >>> 16) & 0xFF);
309             write((a >>> 8) & 0xFF);
310             write(a & 0xFF);
311         }
312
313         public void writeLong(long a) throws IOException JavaDoc {
314             writeInt((int) ((a >>> 32) & 0xFFFFFFFF));
315             writeInt((int) (a & 0xFFFFFFFF));
316         }
317     }
318 }
319
Popular Tags