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.io;
033    
034    import java.io.ByteArrayOutputStream;
035    import java.io.DataOutput;
036    import java.io.DataOutputStream;
037    import java.io.FilterOutputStream;
038    import java.io.IOException;
039    import java.io.OutputStream;
040    
041    /**
042     * LEDataOutputStream.java
043     * <br>
044     * Little-endian writing.
045     * <br>
046     * @author Kouznetsov Andrei
047     *
048     */
049    public class LEDataOutputStream extends FilterOutputStream implements DataOutput {
050    
051            public LEDataOutputStream(OutputStream out) {
052                    super(out);
053            }
054    
055            public final void writeShort(int value) throws IOException {
056                    write(value & 0xFF);
057                    write((value >> 8) & 0xFF);
058            }
059    
060            public final void writeChar(int value) throws IOException {
061                    write(value & 0xFF);
062                    write((value >> 8) & 0xFF);
063            }
064    
065            public final void writeInt(int value) throws IOException {
066                    write(value & 0xFF);
067                    write((value >> 8) & 0xFF);
068                    write((value >> 16) & 0xFF);
069                    write((value >> 24) & 0xFF);
070            }
071    
072            public final void writeLong(long value) throws IOException {
073                    writeInt((int)(value & 0xFFFFFFFF));
074                    writeInt((int)((value >> 32) & 0xFFFFFFFF));
075            }
076    
077            public final void writeFloat(float value) throws IOException {
078                    writeInt(Float.floatToIntBits(value));
079            }
080    
081            public final void writeDouble(double value) throws IOException {
082                    writeLong(Double.doubleToLongBits(value));
083            }
084    
085            public void writeBoolean(boolean b) throws IOException {
086                    out.write(b ? 1 : 0);
087            }
088    
089            public void writeByte(int v) throws IOException {
090                    write(v);
091            }
092    
093            public void writeBytes(String s) throws IOException {
094                    int len = s.length();
095                    for (int i = 0 ; i < len ; i++) {
096                            out.write((byte)s.charAt(i));
097                    }
098            }
099    
100            public void writeChars(String s) throws IOException {
101                    int len = s.length();
102                    byte [] b = new byte[len * 2];
103                    int index = 0;
104                    for(int i = 0; i < len; i++) {
105                            int v = s.charAt(i);
106                            b[index++] = (byte)((v >>> 0) & 0xFF);
107                            b[index++] = (byte)((v >>> 8) & 0xFF);
108                    }
109                    write(b);
110            }
111    
112            public void writeUTF(String str) throws IOException {
113                    ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
114                    DataOutputStream dataOut = new DataOutputStream(out);
115                    dataOut.writeUTF(str);
116                    dataOut.flush();
117                    dataOut.close();
118                    byte[] b = out.toByteArray();
119                    write(b);
120            }
121    }
122    
123