001    /*
002     * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003     *
004     * http://uio.imagero.com
005     *
006     * Redistribution and use in source and binary forms, with or without
007     * modification, are permitted provided that the following conditions are met:
008     *
009     *  o Redistributions of source code must retain the above copyright notice,
010     *    this list of conditions and the following disclaimer.
011     *
012     *  o Redistributions in binary form must reproduce the above copyright notice,
013     *    this list of conditions and the following disclaimer in the documentation
014     *    and/or other materials provided with the distribution.
015     *
016     *  o Neither the name of Andrey Kuznetsov nor the names of
017     *    its contributors may be used to endorse or promote products derived
018     *    from this software without specific prior written permission.
019     *
020     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029     * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     */
032    package com.imagero.uio.impl;
033    
034    import com.imagero.uio.ISeekable;
035    import com.imagero.uio.RandomAccessIO;
036    import com.imagero.uio.xform.XtoByteBE;
037    import com.imagero.uio.xform.XtoByteLE;
038    
039    import java.io.ByteArrayOutputStream;
040    import java.io.DataOutputStream;
041    import java.io.IOException;
042    
043    /**
044     * @author Andrey Kuznetsov
045     */
046    public abstract class AbstractRandomAccessIO extends AbstractRandomAccessInput implements RandomAccessIO {
047    
048        public final void write(short[] data) throws IOException {
049            write(data, 0, data.length, byteOrder);
050        }
051    
052        public final void write(short[] data, int byteOrder) throws IOException {
053            write(data, 0, data.length, byteOrder);
054        }
055    
056        public final void write(short[] data, int offset, int length) throws IOException {
057            write(data, offset, length, byteOrder);
058        }
059    
060        public final void write(char[] data) throws IOException {
061            write(data, 0, data.length, byteOrder);
062        }
063    
064        public final void write(char[] data, int byteOrder) throws IOException {
065            write(data, 0, data.length, byteOrder);
066        }
067    
068        public final void write(char[] data, int offset, int length) throws IOException {
069            write(data, offset, length, byteOrder);
070        }
071    
072        public final void write(int[] data) throws IOException {
073            write(data, 0, data.length, byteOrder);
074        }
075    
076        public final void write(int[] data, int byteOrder) throws IOException {
077            write(data, 0, data.length, byteOrder);
078        }
079    
080        public final void write(int[] data, int offset, int length) throws IOException {
081            write(data, offset, length, byteOrder);
082        }
083    
084        public final void write(float[] data) throws IOException {
085            write(data, 0, data.length, byteOrder);
086        }
087    
088        public final void write(float[] data, int byteOrder) throws IOException {
089            write(data, 0, data.length, byteOrder);
090        }
091    
092        public final void write(float[] data, int offset, int length) throws IOException {
093            write(data, offset, length, byteOrder);
094        }
095    
096        public final void write(long[] data) throws IOException {
097            write(data, 0, data.length, byteOrder);
098        }
099    
100        public final void write(long[] data, int byteOrder) throws IOException {
101            write(data, 0, data.length, byteOrder);
102        }
103    
104        public final void write(long[] data, int offset, int length) throws IOException {
105            write(data, offset, length, byteOrder);
106        }
107    
108        public final void write(double[] data) throws IOException {
109            write(data, 0, data.length, byteOrder);
110        }
111    
112        public final void write(double[] data, int byteOrder) throws IOException {
113            write(data, 0, data.length, byteOrder);
114        }
115    
116        public final void write(double[] data, int offset, int length) throws IOException {
117            write(data, offset, length, byteOrder);
118        }
119    
120        public final void write(byte b[]) throws IOException {
121            write(b, 0, b.length);
122        }
123    
124        public final void writeBoolean(boolean v) throws IOException {
125            write(v ? 1 : 0);
126        }
127    
128        public final void writeByte(int v) throws IOException {
129            write(v);
130        }
131    
132        public final void writeShort(int v) throws IOException {
133            writeShort(v, byteOrder);
134        }
135    
136        public final void writeShort(int v, int byteOrder) throws IOException {
137            byte[] dest = new byte[2];
138            com.imagero.uio.Transformer.shortToByte((short) v, dest, 0, byteOrder == BIG_ENDIAN);
139            write(dest);
140        }
141    
142        public final void writeChar(int v) throws IOException {
143            writeChar(v, byteOrder);
144        }
145    
146        public final void writeChar(int v, int byteOrder) throws IOException {
147            byte[] dest = new byte[2];
148            com.imagero.uio.Transformer.charToByte((char) v, dest, 0, byteOrder == BIG_ENDIAN);
149            write(dest);
150        }
151    
152        public final void writeInt(int v) throws IOException {
153            writeInt(v, byteOrder);
154        }
155    
156        public final void writeInt(int v, int byteOrder) throws IOException {
157            byte[] dest = new byte[4];
158            com.imagero.uio.Transformer.intToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
159            write(dest);
160        }
161    
162        public final void writeLong(long v) throws IOException {
163            writeLong(v, byteOrder);
164        }
165    
166        public final void writeLong(long v, int byteOrder) throws IOException {
167            byte[] dest = new byte[8];
168            com.imagero.uio.Transformer.longToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
169            write(dest);
170        }
171    
172        public final void writeFloat(float v) throws IOException {
173            writeFloat(v, byteOrder);
174        }
175    
176        public final void writeFloat(float v, int byteOrder) throws IOException {
177            byte[] dest = new byte[4];
178            com.imagero.uio.Transformer.floatToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
179            write(dest);
180        }
181    
182        public final void writeDouble(double v) throws IOException {
183            writeDouble(v, byteOrder);
184        }
185    
186        public final void writeDouble(double v, int byteOrder) throws IOException {
187            byte[] dest = new byte[8];
188            com.imagero.uio.Transformer.doubleToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
189            write(dest);
190        }
191    
192        public final void writeBytes(String s) throws IOException {
193            write(s.getBytes());
194        }
195    
196        public final void writeChars(String s) throws IOException {
197            for (int i = 0; i < s.length(); i++) {
198                writeChar(s.charAt(i));
199            }
200        }
201    
202        public final void writeUTF(String str) throws IOException {
203            ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
204            DataOutputStream dataOut = new DataOutputStream(out);
205            dataOut.writeUTF(str);
206            dataOut.flush();
207            dataOut.close();
208            byte[] b = out.toByteArray();
209            write(b);
210        }
211    
212        public void write(short[] sh, int offset, int length, int byteOrder) throws IOException {
213            byte[] b = new byte[length << 1];
214            if(byteOrder == ISeekable.BIG_ENDIAN) {
215                XtoByteBE.shortToByteBE(sh, offset, length, b, 0);
216            }
217            else {
218                XtoByteLE.shortToByteLE(sh, offset, length, b, 0);
219            }
220            write(b);
221        }
222    
223        public void write(char[] sh, int offset, int length, int byteOrder) throws IOException {
224            byte[] b = transform(sh, offset, length, byteOrder);
225            write(b);
226        }
227    
228        protected static byte[] transform(char[] sh, int offset, int length, int byteOrder) {
229            byte[] b = new byte[length << 1];
230            if(byteOrder == ISeekable.BIG_ENDIAN) {
231                XtoByteBE.charToByte(sh, offset, length, b, 0);
232            }
233            else {
234                XtoByteLE.charToByte(sh, offset, length, b, 0);
235            }
236            return b;
237        }
238    
239        public void write(int[] source, int offset, int length, int byteOrder) throws IOException {
240            byte[] b = transform(source, offset, length, byteOrder);
241            write(b);
242        }
243    
244        protected static byte[] transform(int[] source, int offset, int length, int byteOrder) {
245            byte[] b = new byte[length << 2];
246            if(byteOrder == ISeekable.BIG_ENDIAN) {
247                XtoByteBE.intToByte(source, offset, length, b, 0);
248            }
249            else {
250                XtoByteLE.intToByte(source, offset, length, b, 0);
251            }
252            return b;
253        }
254    
255        public void write(float[] source, int offset, int length, int byteOrder) throws IOException {
256            byte[] b = transform(source, offset, length, byteOrder);
257            write(b);
258        }
259    
260        protected static byte[] transform(float[] source, int offset, int length, int byteOrder) {
261            byte[] b = new byte[length << 2];
262            if(byteOrder == ISeekable.BIG_ENDIAN) {
263                XtoByteBE.floatToByteBE(source, offset, length, b, 0);
264            }
265            else {
266                XtoByteLE.floatToByteLE(source, offset, length, b, 0);
267            }
268            return b;
269        }
270    
271        public void write(long[] source, int offset, int length, int byteOrder) throws IOException {
272            byte[] b = transform(source, offset, length, byteOrder);
273            write(b);
274        }
275    
276        protected static byte[] transform(long[] source, int offset, int length, int byteOrder) {
277            byte[] b = new byte[length << 3];
278            if(byteOrder == ISeekable.BIG_ENDIAN) {
279                XtoByteBE.longToByteBE(source, offset, length, b, 0);
280            }
281            else {
282                XtoByteLE.longToByteLE(source, offset, length, b, 0);
283            }
284            return b;
285        }
286    
287        public void write(double[] source, int offset, int length, int byteOrder) throws IOException {
288            byte[] b = transform(source, offset, length, byteOrder);
289            write(b);
290        }
291    
292        protected static byte[] transform(double[] source, int offset, int length, int byteOrder) {
293            byte[] b = new byte[length << 3];
294            if(byteOrder == ISeekable.BIG_ENDIAN) {
295                XtoByteBE.doubleToByteBE(source, offset, length, b, 0);
296            }
297            else {
298                XtoByteLE.doubleToByteLE(source, offset, length, b, 0);
299            }
300            return b;
301        }
302    
303        public void flush() throws IOException {
304        }
305    }