Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
369 chris 1
package com.gebauz.Bauzoid.file;
2
 
3
import java.io.DataInput;
4
import java.io.DataInputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
 
8
/** Version of DataInputStream that supports little endian. */
9
public class LittleEndianDataInputStream extends InputStream implements DataInput
10
{
11
        public LittleEndianDataInputStream(InputStream in)
12
        {
13
                this.in = in;
14
                this.d = new DataInputStream(in);
15
                w = new byte[8];
16
        }
17
 
18
        public int available() throws IOException
19
        {
20
                return d.available();
21
        }
22
 
23
        public final short readShort() throws IOException
24
        {
25
                d.readFully(w, 0, 2);
26
                return (short)(
27
                                (w[1]&0xff) << 8 |
28
                                (w[0]&0xff));
29
        }
30
 
31
        /** Note, returns int even though it reads a short. */
32
        public final int readUnsignedShort() throws IOException
33
        {
34
                d.readFully(w, 0, 2);
35
                return (
36
                                (w[1]&0xff) << 8 |
37
                                (w[0]&0xff));
38
        }
39
 
40
         /** like DataInputStream.readChar except little endian. */
41
        public final char readChar() throws IOException
42
        {
43
                d.readFully(w, 0, 2);
44
                return (char) (
45
                                (w[1]&0xff) << 8 |
46
                                (w[0]&0xff));
47
        }
48
 
49
        /** like DataInputStream.readInt except little endian. */
50
        public final int readInt() throws IOException
51
        {
52
                d.readFully(w, 0, 4);
53
                return
54
                        (w[3])      << 24 |
55
                        (w[2]&0xff) << 16 |
56
                        (w[1]&0xff) <<  8 |
57
                        (w[0]&0xff);
58
        }
59
 
60
        /** like DataInputStream.readLong except little endian. */
61
        public final long readLong() throws IOException
62
        {
63
                 d.readFully(w, 0, 8);
64
                 return
65
                 (long)(w[7])      << 56 |
66
                 (long)(w[6]&0xff) << 48 |
67
                 (long)(w[5]&0xff) << 40 |
68
                 (long)(w[4]&0xff) << 32 |
69
                 (long)(w[3]&0xff) << 24 |
70
                 (long)(w[2]&0xff) << 16 |
71
                 (long)(w[1]&0xff) <<  8 |
72
                 (long)(w[0]&0xff);
73
        }
74
 
75
        public final float readFloat() throws IOException
76
        {
77
                return Float.intBitsToFloat(readInt());
78
        }
79
 
80
        public final double readDouble() throws IOException
81
        {
82
                return Double.longBitsToDouble(readLong());
83
        }
84
 
85
        public final int read(byte b[], int off, int len) throws IOException
86
        {
87
                return in.read(b, off, len);
88
        }
89
 
90
        public final void readFully(byte b[]) throws IOException
91
        {
92
                d.readFully(b, 0, b.length);
93
         }
94
 
95
        public final void readFully(byte b[], int off, int len) throws IOException
96
        {
97
                d.readFully(b, off, len);
98
        }
99
 
100
        public final int skipBytes(int n) throws IOException
101
        {
102
                return d.skipBytes(n);
103
        }
104
 
105
        public final boolean readBoolean() throws IOException
106
        {
107
                return d.readBoolean();
108
        }
109
 
110
        public final byte readByte() throws IOException
111
        {
112
                return d.readByte();
113
        }
114
 
115
        public int read() throws IOException
116
        {
117
                return in.read();
118
        }
119
 
120
        public final int readUnsignedByte() throws IOException
121
        {
122
                return d.readUnsignedByte();
123
        }
124
 
125
        @SuppressWarnings("deprecation")
126
        public final String readLine() throws IOException
127
        {
128
                return d.readLine();
129
        }
130
 
131
        public final String readUTF() throws IOException
132
        {
133
                return d.readUTF();
134
        }
135
 
136
        public final void close() throws IOException
137
        {
138
                d.close();
139
        }
140
 
141
        private DataInputStream d; // to get at high level readFully methods of
142
        // DataInputStream
143
        private InputStream in; // to get at the low-level read methods of
144
        // InputStream
145
        private byte w[]; // work array for buffering input
146
 
147
}
148
 
149