• 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 
35 import java.io.IOException;
36 import java.io.InputStream;
37 
38 /**
39  * A partial implementation of the {@link Parser} interface which implements
40  * as many methods of that interface as possible in terms of other methods.
41  *
42  * Note: This class implements all the convenience methods in the
43  * {@link Parser} interface. See {@link Parser} for related javadocs.
44  * Subclasses need to implement
45  * {@link Parser#parsePartialFrom(CodedInputStream, ExtensionRegistryLite)}
46  *
47  * @author liujisi@google.com (Pherl Liu)
48  */
49 public abstract class AbstractParser<MessageType extends MessageLite>
50     implements Parser<MessageType> {
51   /**
52    * Creates an UninitializedMessageException for MessageType.
53    */
54   private UninitializedMessageException
newUninitializedMessageException(MessageType message)55       newUninitializedMessageException(MessageType message) {
56     if (message instanceof AbstractMessageLite) {
57       return ((AbstractMessageLite) message).newUninitializedMessageException();
58     }
59     return new UninitializedMessageException(message);
60   }
61 
62   /**
63    * Helper method to check if message is initialized.
64    *
65    * @throws InvalidProtocolBufferException if it is not initialized.
66    * @return The message to check.
67    */
checkMessageInitialized(MessageType message)68   private MessageType checkMessageInitialized(MessageType message)
69       throws InvalidProtocolBufferException {
70     if (message != null && !message.isInitialized()) {
71       throw newUninitializedMessageException(message)
72           .asInvalidProtocolBufferException()
73           .setUnfinishedMessage(message);
74     }
75     return message;
76   }
77 
78   private static final ExtensionRegistryLite EMPTY_REGISTRY
79       = ExtensionRegistryLite.getEmptyRegistry();
80 
81   @Override
parsePartialFrom(CodedInputStream input)82   public MessageType parsePartialFrom(CodedInputStream input)
83       throws InvalidProtocolBufferException {
84     return parsePartialFrom(input, EMPTY_REGISTRY);
85   }
86 
87   @Override
parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)88   public MessageType parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
89       throws InvalidProtocolBufferException {
90     return checkMessageInitialized(
91         parsePartialFrom(input, extensionRegistry));
92   }
93 
94   @Override
parseFrom(CodedInputStream input)95   public MessageType parseFrom(CodedInputStream input) throws InvalidProtocolBufferException {
96     return parseFrom(input, EMPTY_REGISTRY);
97   }
98 
99   @Override
parsePartialFrom(ByteString data, ExtensionRegistryLite extensionRegistry)100   public MessageType parsePartialFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
101       throws InvalidProtocolBufferException {
102     MessageType message;
103     try {
104       CodedInputStream input = data.newCodedInput();
105       message = parsePartialFrom(input, extensionRegistry);
106       try {
107         input.checkLastTagWas(0);
108       } catch (InvalidProtocolBufferException e) {
109         throw e.setUnfinishedMessage(message);
110       }
111       return message;
112     } catch (InvalidProtocolBufferException e) {
113       throw e;
114     }
115   }
116 
117   @Override
parsePartialFrom(ByteString data)118   public MessageType parsePartialFrom(ByteString data) throws InvalidProtocolBufferException {
119     return parsePartialFrom(data, EMPTY_REGISTRY);
120   }
121 
122   @Override
parseFrom(ByteString data, ExtensionRegistryLite extensionRegistry)123   public MessageType parseFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
124       throws InvalidProtocolBufferException {
125     return checkMessageInitialized(parsePartialFrom(data, extensionRegistry));
126   }
127 
128   @Override
parseFrom(ByteString data)129   public MessageType parseFrom(ByteString data) throws InvalidProtocolBufferException {
130     return parseFrom(data, EMPTY_REGISTRY);
131   }
132 
133   @Override
parsePartialFrom( byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)134   public MessageType parsePartialFrom(
135       byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
136       throws InvalidProtocolBufferException {
137     try {
138       CodedInputStream input = CodedInputStream.newInstance(data, off, len);
139       MessageType message = parsePartialFrom(input, extensionRegistry);
140       try {
141         input.checkLastTagWas(0);
142       } catch (InvalidProtocolBufferException e) {
143         throw e.setUnfinishedMessage(message);
144       }
145       return message;
146     } catch (InvalidProtocolBufferException e) {
147       throw e;
148     }
149   }
150 
151   @Override
parsePartialFrom(byte[] data, int off, int len)152   public MessageType parsePartialFrom(byte[] data, int off, int len)
153       throws InvalidProtocolBufferException {
154     return parsePartialFrom(data, off, len, EMPTY_REGISTRY);
155   }
156 
157   @Override
parsePartialFrom(byte[] data, ExtensionRegistryLite extensionRegistry)158   public MessageType parsePartialFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
159       throws InvalidProtocolBufferException {
160     return parsePartialFrom(data, 0, data.length, extensionRegistry);
161   }
162 
163   @Override
parsePartialFrom(byte[] data)164   public MessageType parsePartialFrom(byte[] data) throws InvalidProtocolBufferException {
165     return parsePartialFrom(data, 0, data.length, EMPTY_REGISTRY);
166   }
167 
168   @Override
parseFrom( byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)169   public MessageType parseFrom(
170       byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
171       throws InvalidProtocolBufferException {
172     return checkMessageInitialized(
173         parsePartialFrom(data, off, len, extensionRegistry));
174   }
175 
176   @Override
parseFrom(byte[] data, int off, int len)177   public MessageType parseFrom(byte[] data, int off, int len)
178       throws InvalidProtocolBufferException {
179     return parseFrom(data, off, len, EMPTY_REGISTRY);
180   }
181 
182   @Override
parseFrom(byte[] data, ExtensionRegistryLite extensionRegistry)183   public MessageType parseFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
184       throws InvalidProtocolBufferException {
185     return parseFrom(data, 0, data.length, extensionRegistry);
186   }
187 
188   @Override
parseFrom(byte[] data)189   public MessageType parseFrom(byte[] data) throws InvalidProtocolBufferException {
190     return parseFrom(data, EMPTY_REGISTRY);
191   }
192 
193   @Override
parsePartialFrom(InputStream input, ExtensionRegistryLite extensionRegistry)194   public MessageType parsePartialFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
195       throws InvalidProtocolBufferException {
196     CodedInputStream codedInput = CodedInputStream.newInstance(input);
197     MessageType message = parsePartialFrom(codedInput, extensionRegistry);
198     try {
199       codedInput.checkLastTagWas(0);
200     } catch (InvalidProtocolBufferException e) {
201       throw e.setUnfinishedMessage(message);
202     }
203     return message;
204   }
205 
206   @Override
parsePartialFrom(InputStream input)207   public MessageType parsePartialFrom(InputStream input) throws InvalidProtocolBufferException {
208     return parsePartialFrom(input, EMPTY_REGISTRY);
209   }
210 
211   @Override
parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry)212   public MessageType parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
213       throws InvalidProtocolBufferException {
214     return checkMessageInitialized(
215         parsePartialFrom(input, extensionRegistry));
216   }
217 
218   @Override
parseFrom(InputStream input)219   public MessageType parseFrom(InputStream input) throws InvalidProtocolBufferException {
220     return parseFrom(input, EMPTY_REGISTRY);
221   }
222 
223   @Override
parsePartialDelimitedFrom( InputStream input, ExtensionRegistryLite extensionRegistry)224   public MessageType parsePartialDelimitedFrom(
225       InputStream input, ExtensionRegistryLite extensionRegistry)
226       throws InvalidProtocolBufferException {
227     int size;
228     try {
229       int firstByte = input.read();
230       if (firstByte == -1) {
231         return null;
232       }
233       size = CodedInputStream.readRawVarint32(firstByte, input);
234     } catch (IOException e) {
235       throw new InvalidProtocolBufferException(e.getMessage());
236     }
237     InputStream limitedInput = new LimitedInputStream(input, size);
238     return parsePartialFrom(limitedInput, extensionRegistry);
239   }
240 
241   @Override
parsePartialDelimitedFrom(InputStream input)242   public MessageType parsePartialDelimitedFrom(InputStream input)
243       throws InvalidProtocolBufferException {
244     return parsePartialDelimitedFrom(input, EMPTY_REGISTRY);
245   }
246 
247   @Override
parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)248   public MessageType parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
249       throws InvalidProtocolBufferException {
250     return checkMessageInitialized(
251         parsePartialDelimitedFrom(input, extensionRegistry));
252   }
253 
254   @Override
parseDelimitedFrom(InputStream input)255   public MessageType parseDelimitedFrom(InputStream input) throws InvalidProtocolBufferException {
256     return parseDelimitedFrom(input, EMPTY_REGISTRY);
257   }
258 }
259