zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.Util
{
    using System;
    using System.IO;
 
    /// <summary>
    /// Wraps an <see cref="T:System.IO.Stream"/> providing <see cref="T:HH.WMS.Utils.NPOI.Util.ILittleEndianInput"/><p/>
    /// 
    /// This class does not buffer any input, so the stream Read position maintained
    /// by this class is consistent with that of the inner stream.
    /// </summary>
    /// <remarks>
    /// @author Josh Micich
    /// </remarks>
    public class LittleEndianInputStream : ILittleEndianInput
    {
        Stream in1 = null;
 
        public int Available()
        {
            return (int)(in1.Length - in1.Position);
        }
 
        public LittleEndianInputStream(Stream is1)
        {
            in1 = is1;
        }
        public int ReadByte()
        {
            return (byte)ReadUByte();
        }
        public int ReadUByte()
        {
            int ch;
            try
            {
                ch = in1.ReadByte();
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            CheckEOF(ch);
            return ch;
        }
        public double ReadDouble()
        {
            return BitConverter.Int64BitsToDouble(ReadLong());
        }
        public int ReadInt()
        {
            int ch1;
            int ch2;
            int ch3;
            int ch4;
            try
            {
                ch1 = in1.ReadByte();
                ch2 = in1.ReadByte();
                ch3 = in1.ReadByte();
                ch4 = in1.ReadByte();
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            CheckEOF(ch1 | ch2 | ch3 | ch4);
            return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0);
        }
        public long ReadLong()
        {
            int b0;
            int b1;
            int b2;
            int b3;
            int b4;
            int b5;
            int b6;
            int b7;
            try
            {
                b0 = in1.ReadByte();
                b1 = in1.ReadByte();
                b2 = in1.ReadByte();
                b3 = in1.ReadByte();
                b4 = in1.ReadByte();
                b5 = in1.ReadByte();
                b6 = in1.ReadByte();
                b7 = in1.ReadByte();
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            CheckEOF(b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7);
            return (((long)b7 << 56) +
                    ((long)b6 << 48) +
                    ((long)b5 << 40) +
                    ((long)b4 << 32) +
                    ((long)b3 << 24) +
                    (b2 << 16) +
                    (b1 << 8) +
                    (b0 << 0));
        }
        public short ReadShort()
        {
            return (short)ReadUShort();
        }
        public int ReadUShort()
        {
            int ch1;
            int ch2;
            try
            {
                ch1 = in1.ReadByte();
                ch2 = in1.ReadByte();
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            CheckEOF(ch1 | ch2);
            return (ch2 << 8) + (ch1 << 0);
        }
        private static void CheckEOF(int value)
        {
            if (value < 0)
            {
                throw new RuntimeException("Unexpected end-of-file");
            }
        }
 
        public void ReadFully(byte[] buf)
        {
            ReadFully(buf, 0, buf.Length);
        }
 
        public void ReadFully(byte[] buf, int off, int len)
        {
            int max = off + len;
            for (int i = off; i < max; i++)
            {
                byte ch;
                try
                {
                    ch = (byte)in1.ReadByte();
                }
                catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
                CheckEOF(ch);
                buf[i] = ch;
            }
        }
    }
}