source: trunk/MgDev/Desktop/MapViewer/MgReadOnlyStream.cs

Last change on this file was 7019, checked in by jng, 4 years ago

#2118: Make the mg-desktop viewer components localization-ready. We use the same Maestro localization tool (a copy of which is included here) to allow for easy translation. The AppLayout? now supports a top-level <Language> element that will set the current application's culture to the CultureInfo? instance indicated by the specified language code. A set of translated satellite assemblies in the specified culture must already exist in the Application's directory for this element to do anything. Similarly, any application using mg-desktop should set their thread culture before initializing the viewer allowing translations from satellite assemblies to kick in.

File size: 9.8 KB
Line 
1#region Disclaimer / License
2// Copyright (C) 2010, Jackie Ng
3// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; either
8// version 2.1 of the License, or (at your option) any later version.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19#endregion
20using System;
21using System.Collections.Generic;
22using System.Text;
23using System.IO;
24using OSGeo.MapGuide;
25
26namespace OSGeo.MapGuide.Viewer
27{
28    /// <summary>
29    /// A read-only, rewindable stream
30    /// </summary>
31    public abstract class ReadOnlyRewindableStream : Stream
32    {
33        /// <summary>
34        /// Resets the internal position of the stream
35        /// </summary>
36        public abstract void Rewind();
37
38        /// <summary>
39        /// Gets whether this stream supports reading
40        /// </summary>
41        public override bool CanRead
42        {
43            get { return true; }
44        }
45
46        /// <summary>
47        /// Gets whether this stream supports seeking
48        /// </summary>
49        public override bool CanSeek
50        {
51            get { return false; }
52        }
53
54        /// <summary>
55        /// Gets whether this stream supports writing
56        /// </summary>
57        public override bool CanWrite
58        {
59            get { return false; }
60        }
61
62        /// <summary>
63        /// Gets whether this stream is rewindable. If not, calls to <see cref="Rewind"/> throw a
64        /// <see cref="T:System.InvalidOperationException"/>
65        /// </summary>
66        public abstract bool CanRewind { get; }
67
68        /// <summary>
69        /// Gets or sets the position (not implemented)
70        /// </summary>
71        public override long Position
72        {
73            get
74            {
75                throw new NotImplementedException();
76            }
77            set
78            {
79                throw new NotImplementedException();
80            }
81        }
82
83        /// <summary>
84        /// Seeks to the specified offset in the stream (not implemented)
85        /// </summary>
86        /// <param name="offset"></param>
87        /// <param name="origin"></param>
88        /// <returns></returns>
89        public override long Seek(long offset, SeekOrigin origin)
90        {
91            throw new NotImplementedException();
92        }
93
94        /// <summary>
95        /// Sets the length of the stream (not implemented)
96        /// </summary>
97        /// <param name="value"></param>
98        public override void SetLength(long value)
99        {
100            throw new NotImplementedException();
101        }
102
103        /// <summary>
104        /// Writes the specified buffer into the stream (not implemented)
105        /// </summary>
106        /// <param name="buffer"></param>
107        /// <param name="offset"></param>
108        /// <param name="count"></param>
109        public override void Write(byte[] buffer, int offset, int count)
110        {
111            throw new NotImplementedException();
112        }
113    }
114
115    /// <summary>
116    /// Represents a method that returns a <see cref="OSGeo.MapGuide.MgByteReader"/> instance
117    /// </summary>
118    /// <returns></returns>
119    public delegate MgByteReader GetByteReaderMethod();
120
121    /// <summary>
122    /// A read-only <see cref="System.IO.Stream"/> adapter for the <see cref="OSGeo.MapGuide.MgByteReader"/>
123    /// class.
124    /// </summary>
125    public class MgReadOnlyStream : ReadOnlyRewindableStream
126    {
127        private MgByteReader _reader;
128
129        /// <summary>
130        /// Creates a new instance
131        /// </summary>
132        /// <param name="method"></param>
133        public MgReadOnlyStream(GetByteReaderMethod method)
134        {
135            _reader = method();
136        }
137
138        /// <summary>
139        /// Initializes a new instance of the <see cref="MgReadOnlyStream"/> class.
140        /// </summary>
141        /// <param name="reader">The reader.</param>
142        public MgReadOnlyStream(MgByteReader reader)
143        {
144            _reader = reader;
145        }
146
147        /// <summary>
148        /// Releases unmanaged resources and performs other cleanup operations before the
149        /// <see cref="MgReadOnlyStream"/> is reclaimed by garbage collection.
150        /// </summary>
151        ~MgReadOnlyStream()
152        {
153            Dispose(false);
154        }
155
156        /// <summary>
157        /// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream"/> and optionally releases the managed resources.
158        /// </summary>
159        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
160        protected override void Dispose(bool disposing)
161        {
162            if (disposing)
163            {
164                _reader.Dispose();
165            }
166            base.Dispose(disposing);
167        }
168
169        /// <summary>
170        /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
171        /// </summary>
172        /// <exception cref="T:System.IO.IOException">
173        /// An I/O error occurs.
174        ///   </exception>
175        public override void Flush()
176        {
177
178        }
179
180        /// <summary>
181        /// When overridden in a derived class, gets the length in bytes of the stream.
182        /// </summary>
183        /// <returns>
184        /// A long value representing the length of the stream in bytes.
185        ///   </returns>
186        ///   
187        /// <exception cref="T:System.NotSupportedException">
188        /// A class derived from Stream does not support seeking.
189        ///   </exception>
190        ///   
191        /// <exception cref="T:System.ObjectDisposedException">
192        /// Methods were called after the stream was closed.
193        ///   </exception>
194        public override long Length
195        {
196            //NOTE: MgByteReader only returns remaining length! Should we
197            //be keeping track of position and adding on this value?
198            get { return _reader.GetLength(); }
199        }
200
201        /// <summary>
202        /// Gets whether this stream is rewindable. If not, calls to <see cref="Rewind"/> throw a
203        /// <see cref="T:System.InvalidOperationException"/>
204        /// </summary>
205        public override bool CanRewind
206        {
207            get { return _reader.IsRewindable(); }
208        }
209
210        /// <summary>
211        /// Resets the internal position of the stream
212        /// </summary>
213        public override void Rewind()
214        {
215            if (!CanRewind)
216                throw new InvalidOperationException(Strings.ErrorStreamNotRewindable);
217
218            _reader.Rewind();
219        }
220
221        /// <summary>
222        /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
223        /// </summary>
224        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source.</param>
225        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
226        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
227        /// <returns>
228        /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
229        /// </returns>
230        /// <exception cref="T:System.ArgumentException">
231        /// The sum of <paramref name="offset"/> and <paramref name="count"/> is larger than the buffer length.
232        ///   </exception>
233        ///   
234        /// <exception cref="T:System.ArgumentNullException">
235        ///   <paramref name="buffer"/> is null.
236        ///   </exception>
237        ///   
238        /// <exception cref="T:System.ArgumentOutOfRangeException">
239        ///   <paramref name="offset"/> or <paramref name="count"/> is negative.
240        ///   </exception>
241        ///   
242        /// <exception cref="T:System.IO.IOException">
243        /// An I/O error occurs.
244        ///   </exception>
245        ///   
246        /// <exception cref="T:System.NotSupportedException">
247        /// The stream does not support reading.
248        ///   </exception>
249        ///   
250        /// <exception cref="T:System.ObjectDisposedException">
251        /// Methods were called after the stream was closed.
252        ///   </exception>
253        public override int Read(byte[] buffer, int offset, int count)
254        {
255            int read = 0;
256            //For good times, please always have the offset as 0
257            if (offset == 0)
258            {
259                read = _reader.Read(buffer, count);
260            }
261            else //So you want to play the hard way eh? Bad performance for you!
262            {
263                byte[] b = new byte[count];
264                read = _reader.Read(b, count);
265                Array.Copy(b, 0, buffer, offset, read);
266            }
267            return read;
268        }
269    }
270}
Note: See TracBrowser for help on using the repository browser.