Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(897)

Side by Side Diff: third_party/protobuf/java/core/src/main/java/com/google/protobuf/AbstractMessageLite.java

Issue 2599263002: third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Address comments Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 27 matching lines...) Expand all
38 38
39 /** 39 /**
40 * A partial implementation of the {@link MessageLite} interface which 40 * A partial implementation of the {@link MessageLite} interface which
41 * implements as many methods of that interface as possible in terms of other 41 * implements as many methods of that interface as possible in terms of other
42 * methods. 42 * methods.
43 * 43 *
44 * @author kenton@google.com Kenton Varda 44 * @author kenton@google.com Kenton Varda
45 */ 45 */
46 public abstract class AbstractMessageLite< 46 public abstract class AbstractMessageLite<
47 MessageType extends AbstractMessageLite<MessageType, BuilderType>, 47 MessageType extends AbstractMessageLite<MessageType, BuilderType>,
48 BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>> 48 BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>>
49 implements MessageLite { 49 implements MessageLite {
50 protected int memoizedHashCode = 0; 50 protected int memoizedHashCode = 0;
51 51
52 @Override 52 @Override
53 public ByteString toByteString() { 53 public ByteString toByteString() {
54 try { 54 try {
55 final ByteString.CodedBuilder out = 55 final ByteString.CodedBuilder out =
56 ByteString.newCodedBuilder(getSerializedSize()); 56 ByteString.newCodedBuilder(getSerializedSize());
57 writeTo(out.getCodedOutput()); 57 writeTo(out.getCodedOutput());
58 return out.build(); 58 return out.build();
59 } catch (IOException e) { 59 } catch (IOException e) {
60 throw new RuntimeException( 60 throw new RuntimeException(getSerializingExceptionMessage("ByteString"), e );
61 "Serializing to a ByteString threw an IOException (should " +
62 "never happen).", e);
63 } 61 }
64 } 62 }
65 63
66 @Override 64 @Override
67 public byte[] toByteArray() { 65 public byte[] toByteArray() {
68 try { 66 try {
69 final byte[] result = new byte[getSerializedSize()]; 67 final byte[] result = new byte[getSerializedSize()];
70 final CodedOutputStream output = CodedOutputStream.newInstance(result); 68 final CodedOutputStream output = CodedOutputStream.newInstance(result);
71 writeTo(output); 69 writeTo(output);
72 output.checkNoSpaceLeft(); 70 output.checkNoSpaceLeft();
73 return result; 71 return result;
74 } catch (IOException e) { 72 } catch (IOException e) {
75 throw new RuntimeException( 73 throw new RuntimeException(getSerializingExceptionMessage("byte array"), e );
76 "Serializing to a byte array threw an IOException " +
77 "(should never happen).", e);
78 } 74 }
79 } 75 }
80 76
81 @Override 77 @Override
82 public void writeTo(final OutputStream output) throws IOException { 78 public void writeTo(final OutputStream output) throws IOException {
83 final int bufferSize = 79 final int bufferSize =
84 CodedOutputStream.computePreferredBufferSize(getSerializedSize()); 80 CodedOutputStream.computePreferredBufferSize(getSerializedSize());
85 final CodedOutputStream codedOutput = 81 final CodedOutputStream codedOutput =
86 CodedOutputStream.newInstance(output, bufferSize); 82 CodedOutputStream.newInstance(output, bufferSize);
87 writeTo(codedOutput); 83 writeTo(codedOutput);
(...skipping 14 matching lines...) Expand all
102 98
103 99
104 /** 100 /**
105 * Package private helper method for AbstractParser to create 101 * Package private helper method for AbstractParser to create
106 * UninitializedMessageException. 102 * UninitializedMessageException.
107 */ 103 */
108 UninitializedMessageException newUninitializedMessageException() { 104 UninitializedMessageException newUninitializedMessageException() {
109 return new UninitializedMessageException(this); 105 return new UninitializedMessageException(this);
110 } 106 }
111 107
108 private String getSerializingExceptionMessage(String target) {
109 return "Serializing " + getClass().getName() + " to a " + target
110 + " threw an IOException (should never happen).";
111 }
112
112 protected static void checkByteStringIsUtf8(ByteString byteString) 113 protected static void checkByteStringIsUtf8(ByteString byteString)
113 throws IllegalArgumentException { 114 throws IllegalArgumentException {
114 if (!byteString.isValidUtf8()) { 115 if (!byteString.isValidUtf8()) {
115 throw new IllegalArgumentException("Byte string is not UTF-8."); 116 throw new IllegalArgumentException("Byte string is not UTF-8.");
116 } 117 }
117 } 118 }
118 119
119 protected static <T> void addAll(final Iterable<T> values, 120 protected static <T> void addAll(final Iterable<T> values,
120 final Collection<? super T> list) { 121 final Collection<? super T> list) {
121 Builder.addAll(values, list); 122 Builder.addAll(values, list);
122 } 123 }
123 124
124 /** 125 /**
125 * A partial implementation of the {@link Message.Builder} interface which 126 * A partial implementation of the {@link Message.Builder} interface which
126 * implements as many methods of that interface as possible in terms of 127 * implements as many methods of that interface as possible in terms of
127 * other methods. 128 * other methods.
128 */ 129 */
129 @SuppressWarnings("unchecked") 130 @SuppressWarnings("unchecked")
130 public abstract static class Builder< 131 public abstract static class Builder<
131 MessageType extends AbstractMessageLite<MessageType, BuilderType>, 132 MessageType extends AbstractMessageLite<MessageType, BuilderType>,
132 BuilderType extends Builder<MessageType, BuilderType>> 133 BuilderType extends Builder<MessageType, BuilderType>>
133 implements MessageLite.Builder { 134 implements MessageLite.Builder {
(...skipping 15 matching lines...) Expand all
149 @Override 150 @Override
150 public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBu fferException { 151 public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBu fferException {
151 try { 152 try {
152 final CodedInputStream input = data.newCodedInput(); 153 final CodedInputStream input = data.newCodedInput();
153 mergeFrom(input); 154 mergeFrom(input);
154 input.checkLastTagWas(0); 155 input.checkLastTagWas(0);
155 return (BuilderType) this; 156 return (BuilderType) this;
156 } catch (InvalidProtocolBufferException e) { 157 } catch (InvalidProtocolBufferException e) {
157 throw e; 158 throw e;
158 } catch (IOException e) { 159 } catch (IOException e) {
159 throw new RuntimeException( 160 throw new RuntimeException(getReadingExceptionMessage("ByteString"), e);
160 "Reading from a ByteString threw an IOException (should " +
161 "never happen).", e);
162 } 161 }
163 } 162 }
164 163
165 @Override 164 @Override
166 public BuilderType mergeFrom( 165 public BuilderType mergeFrom(
167 final ByteString data, final ExtensionRegistryLite extensionRegistry) 166 final ByteString data, final ExtensionRegistryLite extensionRegistry)
168 throws InvalidProtocolBufferException { 167 throws InvalidProtocolBufferException {
169 try { 168 try {
170 final CodedInputStream input = data.newCodedInput(); 169 final CodedInputStream input = data.newCodedInput();
171 mergeFrom(input, extensionRegistry); 170 mergeFrom(input, extensionRegistry);
172 input.checkLastTagWas(0); 171 input.checkLastTagWas(0);
173 return (BuilderType) this; 172 return (BuilderType) this;
174 } catch (InvalidProtocolBufferException e) { 173 } catch (InvalidProtocolBufferException e) {
175 throw e; 174 throw e;
176 } catch (IOException e) { 175 } catch (IOException e) {
177 throw new RuntimeException( 176 throw new RuntimeException(getReadingExceptionMessage("ByteString"), e);
178 "Reading from a ByteString threw an IOException (should " +
179 "never happen).", e);
180 } 177 }
181 } 178 }
182 179
183 @Override 180 @Override
184 public BuilderType mergeFrom(final byte[] data) throws InvalidProtocolBuffer Exception { 181 public BuilderType mergeFrom(final byte[] data) throws InvalidProtocolBuffer Exception {
185 return mergeFrom(data, 0, data.length); 182 return mergeFrom(data, 0, data.length);
186 } 183 }
187 184
188 @Override 185 @Override
189 public BuilderType mergeFrom(final byte[] data, final int off, final int len ) 186 public BuilderType mergeFrom(final byte[] data, final int off, final int len )
190 throws InvalidProtocolBufferException { 187 throws InvalidProtocolBufferException {
191 try { 188 try {
192 final CodedInputStream input = 189 final CodedInputStream input =
193 CodedInputStream.newInstance(data, off, len); 190 CodedInputStream.newInstance(data, off, len);
194 mergeFrom(input); 191 mergeFrom(input);
195 input.checkLastTagWas(0); 192 input.checkLastTagWas(0);
196 return (BuilderType) this; 193 return (BuilderType) this;
197 } catch (InvalidProtocolBufferException e) { 194 } catch (InvalidProtocolBufferException e) {
198 throw e; 195 throw e;
199 } catch (IOException e) { 196 } catch (IOException e) {
200 throw new RuntimeException( 197 throw new RuntimeException(getReadingExceptionMessage("byte array"), e);
201 "Reading from a byte array threw an IOException (should " +
202 "never happen).", e);
203 } 198 }
204 } 199 }
205 200
206 @Override 201 @Override
207 public BuilderType mergeFrom(final byte[] data, final ExtensionRegistryLite extensionRegistry) 202 public BuilderType mergeFrom(final byte[] data, final ExtensionRegistryLite extensionRegistry)
208 throws InvalidProtocolBufferException { 203 throws InvalidProtocolBufferException {
209 return mergeFrom(data, 0, data.length, extensionRegistry); 204 return mergeFrom(data, 0, data.length, extensionRegistry);
210 } 205 }
211 206
212 @Override 207 @Override
213 public BuilderType mergeFrom( 208 public BuilderType mergeFrom(
214 final byte[] data, 209 final byte[] data,
215 final int off, 210 final int off,
216 final int len, 211 final int len,
217 final ExtensionRegistryLite extensionRegistry) 212 final ExtensionRegistryLite extensionRegistry)
218 throws InvalidProtocolBufferException { 213 throws InvalidProtocolBufferException {
219 try { 214 try {
220 final CodedInputStream input = 215 final CodedInputStream input =
221 CodedInputStream.newInstance(data, off, len); 216 CodedInputStream.newInstance(data, off, len);
222 mergeFrom(input, extensionRegistry); 217 mergeFrom(input, extensionRegistry);
223 input.checkLastTagWas(0); 218 input.checkLastTagWas(0);
224 return (BuilderType) this; 219 return (BuilderType) this;
225 } catch (InvalidProtocolBufferException e) { 220 } catch (InvalidProtocolBufferException e) {
226 throw e; 221 throw e;
227 } catch (IOException e) { 222 } catch (IOException e) {
228 throw new RuntimeException( 223 throw new RuntimeException(getReadingExceptionMessage("byte array"), e);
229 "Reading from a byte array threw an IOException (should " +
230 "never happen).", e);
231 } 224 }
232 } 225 }
233 226
234 @Override 227 @Override
235 public BuilderType mergeFrom(final InputStream input) throws IOException { 228 public BuilderType mergeFrom(final InputStream input) throws IOException {
236 final CodedInputStream codedInput = CodedInputStream.newInstance(input); 229 final CodedInputStream codedInput = CodedInputStream.newInstance(input);
237 mergeFrom(codedInput); 230 mergeFrom(codedInput);
238 codedInput.checkLastTagWas(0); 231 codedInput.checkLastTagWas(0);
239 return (BuilderType) this; 232 return (BuilderType) this;
240 } 233 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 final InputStream limitedInput = new LimitedInputStream(input, size); 307 final InputStream limitedInput = new LimitedInputStream(input, size);
315 mergeFrom(limitedInput, extensionRegistry); 308 mergeFrom(limitedInput, extensionRegistry);
316 return true; 309 return true;
317 } 310 }
318 311
319 @Override 312 @Override
320 public boolean mergeDelimitedFrom(final InputStream input) throws IOExceptio n { 313 public boolean mergeDelimitedFrom(final InputStream input) throws IOExceptio n {
321 return mergeDelimitedFrom(input, 314 return mergeDelimitedFrom(input,
322 ExtensionRegistryLite.getEmptyRegistry()); 315 ExtensionRegistryLite.getEmptyRegistry());
323 } 316 }
324 317
325 @Override 318 @Override
326 @SuppressWarnings("unchecked") // isInstance takes care of this 319 @SuppressWarnings("unchecked") // isInstance takes care of this
327 public BuilderType mergeFrom(final MessageLite other) { 320 public BuilderType mergeFrom(final MessageLite other) {
328 if (!getDefaultInstanceForType().getClass().isInstance(other)) { 321 if (!getDefaultInstanceForType().getClass().isInstance(other)) {
329 throw new IllegalArgumentException( 322 throw new IllegalArgumentException(
330 "mergeFrom(MessageLite) can only merge messages of the same type."); 323 "mergeFrom(MessageLite) can only merge messages of the same type.");
331 } 324 }
332 325
333 return internalMergeFrom((MessageType) other); 326 return internalMergeFrom((MessageType) other);
334 } 327 }
335 328
336 protected abstract BuilderType internalMergeFrom(MessageType message); 329 protected abstract BuilderType internalMergeFrom(MessageType message);
337 330
331 private String getReadingExceptionMessage(String target) {
332 return "Reading " + getClass().getName() + " from a " + target
333 + " threw an IOException (should never happen).";
334 }
335
338 /** 336 /**
339 * Construct an UninitializedMessageException reporting missing fields in 337 * Construct an UninitializedMessageException reporting missing fields in
340 * the given message. 338 * the given message.
341 */ 339 */
342 protected static UninitializedMessageException 340 protected static UninitializedMessageException
343 newUninitializedMessageException(MessageLite message) { 341 newUninitializedMessageException(MessageLite message) {
344 return new UninitializedMessageException(message); 342 return new UninitializedMessageException(message);
345 } 343 }
346 344
347 /** 345 /**
(...skipping 29 matching lines...) Expand all
377 375
378 private static void checkForNullValues(final Iterable<?> values) { 376 private static void checkForNullValues(final Iterable<?> values) {
379 for (final Object value : values) { 377 for (final Object value : values) {
380 if (value == null) { 378 if (value == null) {
381 throw new NullPointerException(); 379 throw new NullPointerException();
382 } 380 }
383 } 381 }
384 } 382 }
385 } 383 }
386 } 384 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698