|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.IO; |
|
21 using SymbianUtils; |
|
22 |
|
23 namespace SymbianUtils.Streams |
|
24 { |
|
25 public class SymbianStreamReaderLE : DisposableObject |
|
26 { |
|
27 #region Enumerations |
|
28 [Flags] |
|
29 public enum TCloseOperation |
|
30 { |
|
31 ENone = 0, |
|
32 ECloseStream = 1, |
|
33 EResetPosition = 2 |
|
34 } |
|
35 #endregion |
|
36 |
|
37 #region Factory |
|
38 public static SymbianStreamReaderLE New( Stream aStream ) |
|
39 { |
|
40 return new SymbianStreamReaderLE( aStream, TCloseOperation.ENone ); |
|
41 } |
|
42 |
|
43 public static SymbianStreamReaderLE New( Stream aStream, TCloseOperation aFlags ) |
|
44 { |
|
45 return new SymbianStreamReaderLE( aStream, aFlags ); |
|
46 } |
|
47 #endregion |
|
48 |
|
49 #region Constructors |
|
50 private SymbianStreamReaderLE( Stream aStream, TCloseOperation aFlags ) |
|
51 { |
|
52 iStream = aStream; |
|
53 iFlags = aFlags; |
|
54 iOriginalPosition = aStream.Position; |
|
55 } |
|
56 #endregion |
|
57 |
|
58 #region API |
|
59 public sbyte ReadInt8() |
|
60 { |
|
61 sbyte b = (sbyte) iStream.ReadByte(); |
|
62 return b; |
|
63 } |
|
64 |
|
65 public short ReadInt16() |
|
66 { |
|
67 sbyte low = ReadInt8(); |
|
68 sbyte high = ReadInt8(); |
|
69 int ret = ( high << 8 ) + low; |
|
70 return (short) ret; |
|
71 } |
|
72 |
|
73 public int ReadInt32() |
|
74 { |
|
75 int low = ReadInt16(); |
|
76 int high = ReadInt16(); |
|
77 int ret = ( high << 16 ) + low; |
|
78 return ret; |
|
79 } |
|
80 |
|
81 public byte ReadUInt8() |
|
82 { |
|
83 return (byte) iStream.ReadByte(); |
|
84 } |
|
85 |
|
86 public ushort ReadUInt16() |
|
87 { |
|
88 byte[] temp = new byte[ 2 ]; |
|
89 iStream.Read( temp, 0, temp.Length ); |
|
90 // |
|
91 ushort ret = 0; |
|
92 for ( int i = 1; i >= 0; i-- ) |
|
93 { |
|
94 ret |= temp[ i ]; |
|
95 if ( i > 0 ) |
|
96 { |
|
97 ret <<= 8; |
|
98 } |
|
99 } |
|
100 return ret; |
|
101 } |
|
102 |
|
103 public uint ReadUInt32() |
|
104 { |
|
105 uint low = ReadUInt16(); |
|
106 uint high = ReadUInt16(); |
|
107 uint ret = ( high << 16 ) + low; |
|
108 return ret; |
|
109 } |
|
110 |
|
111 public string ReadString( int aLengthInCharacters ) |
|
112 { |
|
113 byte[] bytes = new byte[ aLengthInCharacters ]; |
|
114 // |
|
115 string ret = string.Empty; |
|
116 if ( iStream.Read( bytes, 0, bytes.Length ) == bytes.Length ) |
|
117 { |
|
118 ret = SymbianUtils.Strings.StringParsingUtils.BytesToString( bytes ); |
|
119 } |
|
120 // |
|
121 return ret; |
|
122 } |
|
123 |
|
124 public string ReadStringUTF16( int aLengthInCharacters ) |
|
125 { |
|
126 byte[] bytes = new byte[ aLengthInCharacters * 2 ]; |
|
127 // |
|
128 string ret = string.Empty; |
|
129 if ( iStream.Read( bytes, 0, bytes.Length ) == bytes.Length ) |
|
130 { |
|
131 ret = Encoding.Unicode.GetString( bytes ); |
|
132 } |
|
133 // |
|
134 return ret; |
|
135 } |
|
136 |
|
137 public byte[] ReadBytes( int aCount ) |
|
138 { |
|
139 byte[] bytes = new byte[ aCount ]; |
|
140 int ret = iStream.Read( bytes, 0, bytes.Length ); |
|
141 if ( ret != bytes.Length ) |
|
142 { |
|
143 throw new Exception( "Unable to read byte data" ); |
|
144 } |
|
145 return bytes; |
|
146 } |
|
147 |
|
148 public long Seek( long aPosition ) |
|
149 { |
|
150 long ret = Seek( aPosition, SeekOrigin.Begin ); |
|
151 return ret; |
|
152 } |
|
153 |
|
154 public long Seek( long aPosition, SeekOrigin aOrigin ) |
|
155 { |
|
156 long ret = iStream.Seek( aPosition, aOrigin ); |
|
157 return ret; |
|
158 } |
|
159 #endregion |
|
160 |
|
161 #region Properties |
|
162 public Stream BaseStream |
|
163 { |
|
164 get { return iStream; } |
|
165 } |
|
166 |
|
167 public long Position |
|
168 { |
|
169 get { return iStream.Position; } |
|
170 set |
|
171 { |
|
172 Seek( value ); |
|
173 } |
|
174 } |
|
175 |
|
176 public long Offset |
|
177 { |
|
178 get |
|
179 { |
|
180 long ret = Position - iOriginalPosition; |
|
181 return ret; |
|
182 } |
|
183 } |
|
184 #endregion |
|
185 |
|
186 #region From DisposableObject |
|
187 protected override void CleanupManagedResources() |
|
188 { |
|
189 try |
|
190 { |
|
191 base.CleanupManagedResources(); |
|
192 } |
|
193 finally |
|
194 { |
|
195 if ( iStream != null ) |
|
196 { |
|
197 if ( ( iFlags & TCloseOperation.EResetPosition ) != 0 ) |
|
198 { |
|
199 iStream.Seek( iOriginalPosition, SeekOrigin.Begin ); |
|
200 } |
|
201 // |
|
202 if ( ( iFlags & TCloseOperation.ECloseStream ) != 0 ) |
|
203 { |
|
204 iStream.Close(); |
|
205 } |
|
206 } |
|
207 iStream = null; |
|
208 } |
|
209 } |
|
210 #endregion |
|
211 |
|
212 #region Data members |
|
213 private readonly TCloseOperation iFlags; |
|
214 private readonly long iOriginalPosition; |
|
215 private Stream iStream = null; |
|
216 #endregion |
|
217 } |
|
218 } |