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.RandomAccessOutput;
035    import com.imagero.uio.Transformer;
036    
037    import java.io.ByteArrayOutputStream;
038    import java.io.DataOutputStream;
039    import java.io.IOException;
040    import java.io.OutputStream;
041    
042    /**
043     * @author Andrey Kuznetsov
044     */
045    public abstract class AbstractRandomAccessOutput extends OutputStream implements RandomAccessOutput {
046    
047        protected int byteOrder = BIG_ENDIAN;
048    
049        public final void write(short[] data) throws IOException {
050            write(data, 0, data.length, byteOrder);
051        }
052    
053        public final void write(short[] data, int byteOrder) throws IOException {
054            write(data, 0, data.length, byteOrder);
055        }
056    
057        public final void write(short[] data, int offset, int length) throws IOException {
058            write(data, offset, length, byteOrder);
059        }
060    
061        public final void write(char[] data) throws IOException {
062            write(data, 0, data.length, byteOrder);
063        }
064    
065        public final void write(char[] data, int byteOrder) throws IOException {
066            write(data, 0, data.length, byteOrder);
067        }
068    
069        public final void write(char[] data, int offset, int length) throws IOException {
070            write(data, offset, length, byteOrder);
071        }
072    
073        public final void write(int[] data) throws IOException {
074            write(data, 0, data.length, byteOrder);
075        }
076    
077        public final void write(int[] data, int byteOrder) throws IOException {
078            write(data, 0, data.length, byteOrder);
079        }
080    
081        public final void write(int[] data, int offset, int length) throws IOException {
082            write(data, offset, length, byteOrder);
083        }
084    
085        public final void write(float[] data) throws IOException {
086            write(data, 0, data.length, byteOrder);
087        }
088    
089        public final void write(float[] data, int byteOrder) throws IOException {
090            write(data, 0, data.length, byteOrder);
091        }
092    
093        public final void write(float[] data, int offset, int length) throws IOException {
094            write(data, offset, length, byteOrder);
095        }
096    
097        public final void write(long[] data) throws IOException {
098            write(data, 0, data.length, byteOrder);
099        }
100    
101        public final void write(long[] data, int byteOrder) throws IOException {
102            write(data, 0, data.length, byteOrder);
103        }
104    
105        public final void write(long[] data, int offset, int length) throws IOException {
106            write(data, offset, length, byteOrder);
107        }
108    
109        public final void write(double[] data) throws IOException {
110            write(data, 0, data.length, byteOrder);
111        }
112    
113        public final void write(double[] data, int byteOrder) throws IOException {
114            write(data, 0, data.length, byteOrder);
115        }
116    
117        public final void write(double[] data, int offset, int length) throws IOException {
118            write(data, offset, length, byteOrder);
119        }
120    
121        public final void write(byte b[]) throws IOException {
122            write(b, 0, b.length);
123        }
124    
125        public final void writeBoolean(boolean v) throws IOException {
126            write(v ? 1 : 0);
127        }
128    
129        public final void writeByte(int v) throws IOException {
130            write(v);
131        }
132    
133        public final void writeShort(int v) throws IOException {
134            writeShort(v, byteOrder);
135        }
136    
137        public final void writeShort(int v, int byteOrder) throws IOException {
138            byte[] dest = new byte[2];
139            com.imagero.uio.Transformer.shortToByte((short) v, dest, 0, byteOrder == BIG_ENDIAN);
140            write(dest);
141        }
142    
143        public final void writeChar(int v) throws IOException {
144            writeChar(v, byteOrder);
145        }
146    
147        public final void writeChar(int v, int byteOrder) throws IOException {
148            byte[] dest = new byte[2];
149            com.imagero.uio.Transformer.charToByte((char) v, dest, 0, byteOrder == BIG_ENDIAN);
150            write(dest);
151        }
152    
153        public final void writeInt(int v) throws IOException {
154            writeInt(v, byteOrder);
155        }
156    
157        public final void writeInt(int v, int byteOrder) throws IOException {
158            byte[] dest = new byte[4];
159            com.imagero.uio.Transformer.intToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
160            write(dest);
161        }
162    
163        public final void writeLong(long v) throws IOException {
164            writeLong(v, byteOrder);
165        }
166    
167        public final void writeLong(long v, int byteOrder) throws IOException {
168            byte[] dest = new byte[8];
169            com.imagero.uio.Transformer.longToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
170            write(dest);
171        }
172    
173        public final void writeFloat(float v) throws IOException {
174            writeFloat(v, byteOrder);
175        }
176    
177        public final void writeFloat(float v, int byteOrder) throws IOException {
178            byte[] dest = new byte[4];
179            com.imagero.uio.Transformer.floatToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
180            write(dest);
181        }
182    
183        public final void writeDouble(double v) throws IOException {
184            writeDouble(v, byteOrder);
185        }
186    
187        public final void writeDouble(double v, int byteOrder) throws IOException {
188            byte[] dest = new byte[8];
189            com.imagero.uio.Transformer.doubleToByte(v, dest, 0, byteOrder == BIG_ENDIAN);
190            write(dest);
191        }
192    
193        public final void writeBytes(String s) throws IOException {
194            write(s.getBytes());
195        }
196    
197        public final void writeChars(String s) throws IOException {
198            for (int i = 0; i < s.length(); i++) {
199                writeChar(s.charAt(i));
200            }
201        }
202    
203        public final void writeUTF(String str) throws IOException {
204            ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
205            DataOutputStream dataOut = new DataOutputStream(out);
206            dataOut.writeUTF(str);
207            dataOut.flush();
208            dataOut.close();
209            byte[] b = out.toByteArray();
210            write(b);
211        }
212    
213        public boolean isBuffered() {
214            return false;
215        }
216    }