Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/MessageStreamIterator.cs

Last change on this file was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 11.4 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System;
38using System.Collections;
39using System.Collections.Generic;
40using System.IO;
41using System.Reflection;
42
43namespace Google.ProtocolBuffers
44{
45    /// <summary>
46    /// Iterates over data created using a <see cref="MessageStreamWriter{T}" />.
47    /// Unlike MessageStreamWriter, this class is not usually constructed directly with
48    /// a stream; instead it is provided with a way of opening a stream when iteration
49    /// is started. The stream is closed when the iteration is completed or the enumerator
50    /// is disposed. (This occurs naturally when using <c>foreach</c>.)
51    /// </summary>
52    public class MessageStreamIterator<TMessage> : IEnumerable<TMessage>
53        where TMessage : IMessage<TMessage>
54    {
55        private readonly StreamProvider streamProvider;
56        private readonly ExtensionRegistry extensionRegistry;
57        private readonly int sizeLimit;
58
59        // Type.EmptyTypes isn't present on the compact framework
60        private static readonly Type[] EmptyTypes = new Type[0];
61
62        /// <summary>
63        /// Delegate created via reflection trickery (once per type) to create a builder
64        /// and read a message from a CodedInputStream with it. Note that unlike in Java,
65        /// there's one static field per constructed type.
66        /// </summary>
67        private static readonly Func<CodedInputStream, ExtensionRegistry, TMessage> messageReader = BuildMessageReader();
68
69        /// <summary>
70        /// Any exception (within reason) thrown within messageReader is caught and rethrown in the constructor.
71        /// This makes life a lot simpler for the caller.
72        /// </summary>
73        private static Exception typeInitializationException;
74
75        /// <summary>
76        /// Creates the delegate later used to read messages. This is only called once per type, but to
77        /// avoid exceptions occurring at confusing times, if this fails it will set typeInitializationException
78        /// to the appropriate error and return null.
79        /// </summary>
80        private static Func<CodedInputStream, ExtensionRegistry, TMessage> BuildMessageReader()
81        {
82            try
83            {
84                Type builderType = FindBuilderType();
85
86                // Yes, it's redundant to find this again, but it's only the once...
87                MethodInfo createBuilderMethod = typeof (TMessage).GetMethod("CreateBuilder", EmptyTypes);
88                Delegate builderBuilder = Delegate.CreateDelegate(
89                    typeof (Func<>).MakeGenericType(builderType), null, createBuilderMethod);
90
91                MethodInfo buildMethod = typeof (MessageStreamIterator<TMessage>)
92                    .GetMethod("BuildImpl", BindingFlags.Static | BindingFlags.NonPublic)
93                    .MakeGenericMethod(typeof (TMessage), builderType);
94
95                return (Func<CodedInputStream, ExtensionRegistry, TMessage>) Delegate.CreateDelegate(
96                    typeof (Func<CodedInputStream, ExtensionRegistry, TMessage>), builderBuilder, buildMethod);
97            }
98            catch (ArgumentException e)
99            {
100                typeInitializationException = e;
101            }
102            catch (InvalidOperationException e)
103            {
104                typeInitializationException = e;
105            }
106            catch (InvalidCastException e)
107            {
108                // Can't see why this would happen, but best to know about it.
109                typeInitializationException = e;
110            }
111            return null;
112        }
113
114        /// <summary>
115        /// Works out the builder type for TMessage, or throws an ArgumentException to explain why it can't.
116        /// </summary>
117        private static Type FindBuilderType()
118        {
119            MethodInfo createBuilderMethod = typeof (TMessage).GetMethod("CreateBuilder", EmptyTypes);
120            if (createBuilderMethod == null)
121            {
122                throw new ArgumentException("Message type " + typeof (TMessage).FullName +
123                                            " has no CreateBuilder method.");
124            }
125            if (createBuilderMethod.ReturnType == typeof (void))
126            {
127                throw new ArgumentException("CreateBuilder method in " + typeof (TMessage).FullName +
128                                            " has void return type");
129            }
130            Type builderType = createBuilderMethod.ReturnType;
131            Type messageInterface = typeof (IMessage<,>).MakeGenericType(typeof (TMessage), builderType);
132            Type builderInterface = typeof (IBuilder<,>).MakeGenericType(typeof (TMessage), builderType);
133            if (Array.IndexOf(typeof (TMessage).GetInterfaces(), messageInterface) == -1)
134            {
135                throw new ArgumentException("Message type " + typeof (TMessage) + " doesn't implement " +
136                                            messageInterface.FullName);
137            }
138            if (Array.IndexOf(builderType.GetInterfaces(), builderInterface) == -1)
139            {
140                throw new ArgumentException("Builder type " + typeof (TMessage) + " doesn't implement " +
141                                            builderInterface.FullName);
142            }
143            return builderType;
144        }
145
146// This is only ever fetched by reflection, so the compiler may
147// complain that it's unused
148#pragma warning disable 0169
149        /// <summary>
150        /// Method we'll use to build messageReader, with the first parameter fixed to TMessage.CreateBuilder. Note that we
151        /// have to introduce another type parameter (TMessage2) as we can't constrain TMessage for just a single method
152        /// (and we can't do it at the type level because we don't know TBuilder). However, by constraining TMessage2
153        /// to not only implement IMessage appropriately but also to derive from TMessage2, we can avoid doing a cast
154        /// for every message; the implicit reference conversion will be fine. In practice, TMessage2 and TMessage will
155        /// be the same type when we construct the generic method by reflection.
156        /// </summary>
157        private static TMessage BuildImpl<TMessage2, TBuilder>(Func<TBuilder> builderBuilder, CodedInputStream input,
158                                                               ExtensionRegistry registry)
159            where TBuilder : IBuilder<TMessage2, TBuilder>
160            where TMessage2 : TMessage, IMessage<TMessage2, TBuilder>
161        {
162            TBuilder builder = builderBuilder();
163            input.ReadMessage(builder, registry);
164            return builder.Build();
165        }
166#pragma warning restore 0414
167
168        private static readonly uint ExpectedTag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
169
170        private MessageStreamIterator(StreamProvider streamProvider, ExtensionRegistry extensionRegistry, int sizeLimit)
171        {
172            if (messageReader == null)
173            {
174                throw typeInitializationException;
175            }
176            this.streamProvider = streamProvider;
177            this.extensionRegistry = extensionRegistry;
178            this.sizeLimit = sizeLimit;
179        }
180
181        private MessageStreamIterator(StreamProvider streamProvider, ExtensionRegistry extensionRegistry)
182            : this(streamProvider, extensionRegistry, CodedInputStream.DefaultSizeLimit)
183        {
184        }
185
186        /// <summary>
187        /// Creates a new instance which uses the same stream provider as this one,
188        /// but the specified extension registry.
189        /// </summary>
190        public MessageStreamIterator<TMessage> WithExtensionRegistry(ExtensionRegistry newRegistry)
191        {
192            return new MessageStreamIterator<TMessage>(streamProvider, newRegistry, sizeLimit);
193        }
194
195        /// <summary>
196        /// Creates a new instance which uses the same stream provider and extension registry as this one,
197        /// but with the specified size limit. Note that this must be big enough for the largest message
198        /// and the tag and size preceding it.
199        /// </summary>
200        public MessageStreamIterator<TMessage> WithSizeLimit(int newSizeLimit)
201        {
202            return new MessageStreamIterator<TMessage>(streamProvider, extensionRegistry, newSizeLimit);
203        }
204
205        public static MessageStreamIterator<TMessage> FromFile(string file)
206        {
207            return new MessageStreamIterator<TMessage>(() => File.OpenRead(file), ExtensionRegistry.Empty);
208        }
209
210        public static MessageStreamIterator<TMessage> FromStreamProvider(StreamProvider streamProvider)
211        {
212            return new MessageStreamIterator<TMessage>(streamProvider, ExtensionRegistry.Empty);
213        }
214
215        public IEnumerator<TMessage> GetEnumerator()
216        {
217            using (Stream stream = streamProvider())
218            {
219                CodedInputStream input = CodedInputStream.CreateInstance(stream);
220                input.SetSizeLimit(sizeLimit);
221                uint tag;
222                string name;
223                while (input.ReadTag(out tag, out name))
224                {
225                    if ((tag == 0 && name == "item") || (tag == ExpectedTag))
226                    {
227                        yield return messageReader(input, extensionRegistry);
228                    }
229                    else
230                    {
231                        throw InvalidProtocolBufferException.InvalidMessageStreamTag();
232                    }
233
234                    input.ResetSizeCounter();
235                }
236            }
237        }
238
239        IEnumerator IEnumerable.GetEnumerator()
240        {
241            return GetEnumerator();
242        }
243    }
244}
Note: See TracBrowser for help on using the repository browser.