• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import com.google.protobuf.AbstractMessageLite.Builder.LimitedInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.nio.ByteBuffer;
37 
38 /**
39  * A partial implementation of the {@link Parser} interface which implements as many methods of that
40  * interface as possible in terms of other methods.
41  *
42  * <p>Note: This class implements all the convenience methods in the {@link Parser} interface. See
43  * {@link Parser} for related javadocs. Subclasses need to implement {@link
44  * Parser#parsePartialFrom(CodedInputStream, ExtensionRegistryLite)}
45  *
46  * @author liujisi@google.com (Pherl Liu)
47  */
48 public abstract class AbstractParser<MessageType extends MessageLite>
49     implements Parser<MessageType> {
50   /** Creates an UninitializedMessageException for MessageType. */
newUninitializedMessageException(MessageType message)51   private UninitializedMessageException newUninitializedMessageException(MessageType message) {
52     if (message instanceof AbstractMessageLite) {
53       return ((AbstractMessageLite) message).newUninitializedMessageException();
54     }
55     return new UninitializedMessageException(message);
56   }
57 
58   /**
59    * Helper method to check if message is initialized.
60    *
61    * @throws InvalidProtocolBufferException if it is not initialized.
62    * @return The message to check.
63    */
checkMessageInitialized(MessageType message)64   private MessageType checkMessageInitialized(MessageType message)
65       throws InvalidProtocolBufferException {
66     if (message != null && !message.isInitialized()) {
67       throw newUninitializedMessageException(message)
68           .asInvalidProtocolBufferException()
69           .setUnfinishedMessage(message);
70     }
71     return message;
72   }
73 
74   private static final ExtensionRegistryLite EMPTY_REGISTRY =
75       ExtensionRegistryLite.getEmptyRegistry();
76 
77   @Override
parsePartialFrom(CodedInputStream input)78   public MessageType parsePartialFrom(CodedInputStream input)
79       throws InvalidProtocolBufferException {
80     return parsePartialFrom(input, EMPTY_REGISTRY);
81   }
82 
83   @Override
parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)84   public MessageType parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
85       throws InvalidProtocolBufferException {
86     return checkMessageInitialized(parsePartialFrom(input, extensionRegistry));
87   }
88 
89   @Override
parseFrom(CodedInputStream input)90   public MessageType parseFrom(CodedInputStream input) throws InvalidProtocolBufferException {
91     return parseFrom(input, EMPTY_REGISTRY);
92   }
93 
94   @Override
parsePartialFrom(ByteString data, ExtensionRegistryLite extensionRegistry)95   public MessageType parsePartialFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
96       throws InvalidProtocolBufferException {
97     MessageType message;
98     try {
99       CodedInputStream input = data.newCodedInput();
100       message = parsePartialFrom(input, extensionRegistry);
101       try {
102         input.checkLastTagWas(0);
103       } catch (InvalidProtocolBufferException e) {
104         throw e.setUnfinishedMessage(message);
105       }
106       return message;
107     } catch (InvalidProtocolBufferException e) {
108       throw e;
109     }
110   }
111 
112   @Override
parsePartialFrom(ByteString data)113   public MessageType parsePartialFrom(ByteString data) throws InvalidProtocolBufferException {
114     return parsePartialFrom(data, EMPTY_REGISTRY);
115   }
116 
117   @Override
parseFrom(ByteString data, ExtensionRegistryLite extensionRegistry)118   public MessageType parseFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
119       throws InvalidProtocolBufferException {
120     return checkMessageInitialized(parsePartialFrom(data, extensionRegistry));
121   }
122 
123   @Override
parseFrom(ByteString data)124   public MessageType parseFrom(ByteString data) throws InvalidProtocolBufferException {
125     return parseFrom(data, EMPTY_REGISTRY);
126   }
127 
128   @Override
parseFrom(ByteBuffer data, ExtensionRegistryLite extensionRegistry)129   public MessageType parseFrom(ByteBuffer data, ExtensionRegistryLite extensionRegistry)
130       throws InvalidProtocolBufferException {
131     MessageType message;
132     try {
133       CodedInputStream input = CodedInputStream.newInstance(data);
134       message = parsePartialFrom(input, extensionRegistry);
135       try {
136         input.checkLastTagWas(0);
137       } catch (InvalidProtocolBufferException e) {
138         throw e.setUnfinishedMessage(message);
139       }
140     } catch (InvalidProtocolBufferException e) {
141       throw e;
142     }
143 
144     return checkMessageInitialized(message);
145   }
146 
147   @Override
parseFrom(ByteBuffer data)148   public MessageType parseFrom(ByteBuffer data) throws InvalidProtocolBufferException {
149     return parseFrom(data, EMPTY_REGISTRY);
150   }
151 
152   @Override
parsePartialFrom( byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)153   public MessageType parsePartialFrom(
154       byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
155       throws InvalidProtocolBufferException {
156     try {
157       CodedInputStream input = CodedInputStream.newInstance(data, off, len);
158       MessageType message = parsePartialFrom(input, extensionRegistry);
159       try {
160         input.checkLastTagWas(0);
161       } catch (InvalidProtocolBufferException e) {
162         throw e.setUnfinishedMessage(message);
163       }
164       return message;
165     } catch (InvalidProtocolBufferException e) {
166       throw e;
167     }
168   }
169 
170   @Override
parsePartialFrom(byte[] data, int off, int len)171   public MessageType parsePartialFrom(byte[] data, int off, int len)
172       throws InvalidProtocolBufferException {
173     return parsePartialFrom(data, off, len, EMPTY_REGISTRY);
174   }
175 
176   @Override
parsePartialFrom(byte[] data, ExtensionRegistryLite extensionRegistry)177   public MessageType parsePartialFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
178       throws InvalidProtocolBufferException {
179     return parsePartialFrom(data, 0, data.length, extensionRegistry);
180   }
181 
182   @Override
parsePartialFrom(byte[] data)183   public MessageType parsePartialFrom(byte[] data) throws InvalidProtocolBufferException {
184     return parsePartialFrom(data, 0, data.length, EMPTY_REGISTRY);
185   }
186 
187   @Override
parseFrom( byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)188   public MessageType parseFrom(
189       byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
190       throws InvalidProtocolBufferException {
191     return checkMessageInitialized(parsePartialFrom(data, off, len, extensionRegistry));
192   }
193 
194   @Override
parseFrom(byte[] data, int off, int len)195   public MessageType parseFrom(byte[] data, int off, int len)
196       throws InvalidProtocolBufferException {
197     return parseFrom(data, off, len, EMPTY_REGISTRY);
198   }
199 
200   @Override
parseFrom(byte[] data, ExtensionRegistryLite extensionRegistry)201   public MessageType parseFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
202       throws InvalidProtocolBufferException {
203     return parseFrom(data, 0, data.length, extensionRegistry);
204   }
205 
206   @Override
parseFrom(byte[] data)207   public MessageType parseFrom(byte[] data) throws InvalidProtocolBufferException {
208     return parseFrom(data, EMPTY_REGISTRY);
209   }
210 
211   @Override
parsePartialFrom(InputStream input, ExtensionRegistryLite extensionRegistry)212   public MessageType parsePartialFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
213       throws InvalidProtocolBufferException {
214     CodedInputStream codedInput = CodedInputStream.newInstance(input);
215     MessageType message = parsePartialFrom(codedInput, extensionRegistry);
216     try {
217       codedInput.checkLastTagWas(0);
218     } catch (InvalidProtocolBufferException e) {
219       throw e.setUnfinishedMessage(message);
220     }
221     return message;
222   }
223 
224   @Override
parsePartialFrom(InputStream input)225   public MessageType parsePartialFrom(InputStream input) throws InvalidProtocolBufferException {
226     return parsePartialFrom(input, EMPTY_REGISTRY);
227   }
228 
229   @Override
parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry)230   public MessageType parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
231       throws InvalidProtocolBufferException {
232     return checkMessageInitialized(parsePartialFrom(input, extensionRegistry));
233   }
234 
235   @Override
parseFrom(InputStream input)236   public MessageType parseFrom(InputStream input) throws InvalidProtocolBufferException {
237     return parseFrom(input, EMPTY_REGISTRY);
238   }
239 
240   @Override
parsePartialDelimitedFrom( InputStream input, ExtensionRegistryLite extensionRegistry)241   public MessageType parsePartialDelimitedFrom(
242       InputStream input, ExtensionRegistryLite extensionRegistry)
243       throws InvalidProtocolBufferException {
244     int size;
245     try {
246       int firstByte = input.read();
247       if (firstByte == -1) {
248         return null;
249       }
250       size = CodedInputStream.readRawVarint32(firstByte, input);
251     } catch (IOException e) {
252       throw new InvalidProtocolBufferException(e);
253     }
254     InputStream limitedInput = new LimitedInputStream(input, size);
255     return parsePartialFrom(limitedInput, extensionRegistry);
256   }
257 
258   @Override
parsePartialDelimitedFrom(InputStream input)259   public MessageType parsePartialDelimitedFrom(InputStream input)
260       throws InvalidProtocolBufferException {
261     return parsePartialDelimitedFrom(input, EMPTY_REGISTRY);
262   }
263 
264   @Override
parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)265   public MessageType parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
266       throws InvalidProtocolBufferException {
267     return checkMessageInitialized(parsePartialDelimitedFrom(input, extensionRegistry));
268   }
269 
270   @Override
parseDelimitedFrom(InputStream input)271   public MessageType parseDelimitedFrom(InputStream input) throws InvalidProtocolBufferException {
272     return parseDelimitedFrom(input, EMPTY_REGISTRY);
273   }
274 }
275