| OLD | NEW |
| 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 Loading... |
| 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(getSerializingExceptionMessage("ByteString"), e
); | 60 throw new RuntimeException( |
| 61 "Serializing to a ByteString threw an IOException (should " + |
| 62 "never happen).", e); |
| 61 } | 63 } |
| 62 } | 64 } |
| 63 | 65 |
| 64 @Override | 66 @Override |
| 65 public byte[] toByteArray() { | 67 public byte[] toByteArray() { |
| 66 try { | 68 try { |
| 67 final byte[] result = new byte[getSerializedSize()]; | 69 final byte[] result = new byte[getSerializedSize()]; |
| 68 final CodedOutputStream output = CodedOutputStream.newInstance(result); | 70 final CodedOutputStream output = CodedOutputStream.newInstance(result); |
| 69 writeTo(output); | 71 writeTo(output); |
| 70 output.checkNoSpaceLeft(); | 72 output.checkNoSpaceLeft(); |
| 71 return result; | 73 return result; |
| 72 } catch (IOException e) { | 74 } catch (IOException e) { |
| 73 throw new RuntimeException(getSerializingExceptionMessage("byte array"), e
); | 75 throw new RuntimeException( |
| 76 "Serializing to a byte array threw an IOException " + |
| 77 "(should never happen).", e); |
| 74 } | 78 } |
| 75 } | 79 } |
| 76 | 80 |
| 77 @Override | 81 @Override |
| 78 public void writeTo(final OutputStream output) throws IOException { | 82 public void writeTo(final OutputStream output) throws IOException { |
| 79 final int bufferSize = | 83 final int bufferSize = |
| 80 CodedOutputStream.computePreferredBufferSize(getSerializedSize()); | 84 CodedOutputStream.computePreferredBufferSize(getSerializedSize()); |
| 81 final CodedOutputStream codedOutput = | 85 final CodedOutputStream codedOutput = |
| 82 CodedOutputStream.newInstance(output, bufferSize); | 86 CodedOutputStream.newInstance(output, bufferSize); |
| 83 writeTo(codedOutput); | 87 writeTo(codedOutput); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 98 | 102 |
| 99 | 103 |
| 100 /** | 104 /** |
| 101 * Package private helper method for AbstractParser to create | 105 * Package private helper method for AbstractParser to create |
| 102 * UninitializedMessageException. | 106 * UninitializedMessageException. |
| 103 */ | 107 */ |
| 104 UninitializedMessageException newUninitializedMessageException() { | 108 UninitializedMessageException newUninitializedMessageException() { |
| 105 return new UninitializedMessageException(this); | 109 return new UninitializedMessageException(this); |
| 106 } | 110 } |
| 107 | 111 |
| 108 private String getSerializingExceptionMessage(String target) { | |
| 109 return "Serializing " + getClass().getName() + " to a " + target | |
| 110 + " threw an IOException (should never happen)."; | |
| 111 } | |
| 112 | |
| 113 protected static void checkByteStringIsUtf8(ByteString byteString) | 112 protected static void checkByteStringIsUtf8(ByteString byteString) |
| 114 throws IllegalArgumentException { | 113 throws IllegalArgumentException { |
| 115 if (!byteString.isValidUtf8()) { | 114 if (!byteString.isValidUtf8()) { |
| 116 throw new IllegalArgumentException("Byte string is not UTF-8."); | 115 throw new IllegalArgumentException("Byte string is not UTF-8."); |
| 117 } | 116 } |
| 118 } | 117 } |
| 119 | 118 |
| 120 protected static <T> void addAll(final Iterable<T> values, | 119 protected static <T> void addAll(final Iterable<T> values, |
| 121 final Collection<? super T> list) { | 120 final Collection<? super T> list) { |
| 122 Builder.addAll(values, list); | 121 Builder.addAll(values, list); |
| 123 } | 122 } |
| 124 | 123 |
| 125 /** | 124 /** |
| 126 * A partial implementation of the {@link Message.Builder} interface which | 125 * A partial implementation of the {@link Message.Builder} interface which |
| 127 * implements as many methods of that interface as possible in terms of | 126 * implements as many methods of that interface as possible in terms of |
| 128 * other methods. | 127 * other methods. |
| 129 */ | 128 */ |
| 130 @SuppressWarnings("unchecked") | 129 @SuppressWarnings("unchecked") |
| 131 public abstract static class Builder< | 130 public abstract static class Builder< |
| 132 MessageType extends AbstractMessageLite<MessageType, BuilderType>, | 131 MessageType extends AbstractMessageLite<MessageType, BuilderType>, |
| 133 BuilderType extends Builder<MessageType, BuilderType>> | 132 BuilderType extends Builder<MessageType, BuilderType>> |
| 134 implements MessageLite.Builder { | 133 implements MessageLite.Builder { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 150 @Override | 149 @Override |
| 151 public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBu
fferException { | 150 public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBu
fferException { |
| 152 try { | 151 try { |
| 153 final CodedInputStream input = data.newCodedInput(); | 152 final CodedInputStream input = data.newCodedInput(); |
| 154 mergeFrom(input); | 153 mergeFrom(input); |
| 155 input.checkLastTagWas(0); | 154 input.checkLastTagWas(0); |
| 156 return (BuilderType) this; | 155 return (BuilderType) this; |
| 157 } catch (InvalidProtocolBufferException e) { | 156 } catch (InvalidProtocolBufferException e) { |
| 158 throw e; | 157 throw e; |
| 159 } catch (IOException e) { | 158 } catch (IOException e) { |
| 160 throw new RuntimeException(getReadingExceptionMessage("ByteString"), e); | 159 throw new RuntimeException( |
| 160 "Reading from a ByteString threw an IOException (should " + |
| 161 "never happen).", e); |
| 161 } | 162 } |
| 162 } | 163 } |
| 163 | 164 |
| 164 @Override | 165 @Override |
| 165 public BuilderType mergeFrom( | 166 public BuilderType mergeFrom( |
| 166 final ByteString data, final ExtensionRegistryLite extensionRegistry) | 167 final ByteString data, final ExtensionRegistryLite extensionRegistry) |
| 167 throws InvalidProtocolBufferException { | 168 throws InvalidProtocolBufferException { |
| 168 try { | 169 try { |
| 169 final CodedInputStream input = data.newCodedInput(); | 170 final CodedInputStream input = data.newCodedInput(); |
| 170 mergeFrom(input, extensionRegistry); | 171 mergeFrom(input, extensionRegistry); |
| 171 input.checkLastTagWas(0); | 172 input.checkLastTagWas(0); |
| 172 return (BuilderType) this; | 173 return (BuilderType) this; |
| 173 } catch (InvalidProtocolBufferException e) { | 174 } catch (InvalidProtocolBufferException e) { |
| 174 throw e; | 175 throw e; |
| 175 } catch (IOException e) { | 176 } catch (IOException e) { |
| 176 throw new RuntimeException(getReadingExceptionMessage("ByteString"), e); | 177 throw new RuntimeException( |
| 178 "Reading from a ByteString threw an IOException (should " + |
| 179 "never happen).", e); |
| 177 } | 180 } |
| 178 } | 181 } |
| 179 | 182 |
| 180 @Override | 183 @Override |
| 181 public BuilderType mergeFrom(final byte[] data) throws InvalidProtocolBuffer
Exception { | 184 public BuilderType mergeFrom(final byte[] data) throws InvalidProtocolBuffer
Exception { |
| 182 return mergeFrom(data, 0, data.length); | 185 return mergeFrom(data, 0, data.length); |
| 183 } | 186 } |
| 184 | 187 |
| 185 @Override | 188 @Override |
| 186 public BuilderType mergeFrom(final byte[] data, final int off, final int len
) | 189 public BuilderType mergeFrom(final byte[] data, final int off, final int len
) |
| 187 throws InvalidProtocolBufferException { | 190 throws InvalidProtocolBufferException { |
| 188 try { | 191 try { |
| 189 final CodedInputStream input = | 192 final CodedInputStream input = |
| 190 CodedInputStream.newInstance(data, off, len); | 193 CodedInputStream.newInstance(data, off, len); |
| 191 mergeFrom(input); | 194 mergeFrom(input); |
| 192 input.checkLastTagWas(0); | 195 input.checkLastTagWas(0); |
| 193 return (BuilderType) this; | 196 return (BuilderType) this; |
| 194 } catch (InvalidProtocolBufferException e) { | 197 } catch (InvalidProtocolBufferException e) { |
| 195 throw e; | 198 throw e; |
| 196 } catch (IOException e) { | 199 } catch (IOException e) { |
| 197 throw new RuntimeException(getReadingExceptionMessage("byte array"), e); | 200 throw new RuntimeException( |
| 201 "Reading from a byte array threw an IOException (should " + |
| 202 "never happen).", e); |
| 198 } | 203 } |
| 199 } | 204 } |
| 200 | 205 |
| 201 @Override | 206 @Override |
| 202 public BuilderType mergeFrom(final byte[] data, final ExtensionRegistryLite
extensionRegistry) | 207 public BuilderType mergeFrom(final byte[] data, final ExtensionRegistryLite
extensionRegistry) |
| 203 throws InvalidProtocolBufferException { | 208 throws InvalidProtocolBufferException { |
| 204 return mergeFrom(data, 0, data.length, extensionRegistry); | 209 return mergeFrom(data, 0, data.length, extensionRegistry); |
| 205 } | 210 } |
| 206 | 211 |
| 207 @Override | 212 @Override |
| 208 public BuilderType mergeFrom( | 213 public BuilderType mergeFrom( |
| 209 final byte[] data, | 214 final byte[] data, |
| 210 final int off, | 215 final int off, |
| 211 final int len, | 216 final int len, |
| 212 final ExtensionRegistryLite extensionRegistry) | 217 final ExtensionRegistryLite extensionRegistry) |
| 213 throws InvalidProtocolBufferException { | 218 throws InvalidProtocolBufferException { |
| 214 try { | 219 try { |
| 215 final CodedInputStream input = | 220 final CodedInputStream input = |
| 216 CodedInputStream.newInstance(data, off, len); | 221 CodedInputStream.newInstance(data, off, len); |
| 217 mergeFrom(input, extensionRegistry); | 222 mergeFrom(input, extensionRegistry); |
| 218 input.checkLastTagWas(0); | 223 input.checkLastTagWas(0); |
| 219 return (BuilderType) this; | 224 return (BuilderType) this; |
| 220 } catch (InvalidProtocolBufferException e) { | 225 } catch (InvalidProtocolBufferException e) { |
| 221 throw e; | 226 throw e; |
| 222 } catch (IOException e) { | 227 } catch (IOException e) { |
| 223 throw new RuntimeException(getReadingExceptionMessage("byte array"), e); | 228 throw new RuntimeException( |
| 229 "Reading from a byte array threw an IOException (should " + |
| 230 "never happen).", e); |
| 224 } | 231 } |
| 225 } | 232 } |
| 226 | 233 |
| 227 @Override | 234 @Override |
| 228 public BuilderType mergeFrom(final InputStream input) throws IOException { | 235 public BuilderType mergeFrom(final InputStream input) throws IOException { |
| 229 final CodedInputStream codedInput = CodedInputStream.newInstance(input); | 236 final CodedInputStream codedInput = CodedInputStream.newInstance(input); |
| 230 mergeFrom(codedInput); | 237 mergeFrom(codedInput); |
| 231 codedInput.checkLastTagWas(0); | 238 codedInput.checkLastTagWas(0); |
| 232 return (BuilderType) this; | 239 return (BuilderType) this; |
| 233 } | 240 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 final InputStream limitedInput = new LimitedInputStream(input, size); | 314 final InputStream limitedInput = new LimitedInputStream(input, size); |
| 308 mergeFrom(limitedInput, extensionRegistry); | 315 mergeFrom(limitedInput, extensionRegistry); |
| 309 return true; | 316 return true; |
| 310 } | 317 } |
| 311 | 318 |
| 312 @Override | 319 @Override |
| 313 public boolean mergeDelimitedFrom(final InputStream input) throws IOExceptio
n { | 320 public boolean mergeDelimitedFrom(final InputStream input) throws IOExceptio
n { |
| 314 return mergeDelimitedFrom(input, | 321 return mergeDelimitedFrom(input, |
| 315 ExtensionRegistryLite.getEmptyRegistry()); | 322 ExtensionRegistryLite.getEmptyRegistry()); |
| 316 } | 323 } |
| 317 | 324 |
| 318 @Override | 325 @Override |
| 319 @SuppressWarnings("unchecked") // isInstance takes care of this | 326 @SuppressWarnings("unchecked") // isInstance takes care of this |
| 320 public BuilderType mergeFrom(final MessageLite other) { | 327 public BuilderType mergeFrom(final MessageLite other) { |
| 321 if (!getDefaultInstanceForType().getClass().isInstance(other)) { | 328 if (!getDefaultInstanceForType().getClass().isInstance(other)) { |
| 322 throw new IllegalArgumentException( | 329 throw new IllegalArgumentException( |
| 323 "mergeFrom(MessageLite) can only merge messages of the same type."); | 330 "mergeFrom(MessageLite) can only merge messages of the same type."); |
| 324 } | 331 } |
| 325 | 332 |
| 326 return internalMergeFrom((MessageType) other); | 333 return internalMergeFrom((MessageType) other); |
| 327 } | 334 } |
| 328 | 335 |
| 329 protected abstract BuilderType internalMergeFrom(MessageType message); | 336 protected abstract BuilderType internalMergeFrom(MessageType message); |
| 330 | 337 |
| 331 private String getReadingExceptionMessage(String target) { | |
| 332 return "Reading " + getClass().getName() + " from a " + target | |
| 333 + " threw an IOException (should never happen)."; | |
| 334 } | |
| 335 | |
| 336 /** | 338 /** |
| 337 * Construct an UninitializedMessageException reporting missing fields in | 339 * Construct an UninitializedMessageException reporting missing fields in |
| 338 * the given message. | 340 * the given message. |
| 339 */ | 341 */ |
| 340 protected static UninitializedMessageException | 342 protected static UninitializedMessageException |
| 341 newUninitializedMessageException(MessageLite message) { | 343 newUninitializedMessageException(MessageLite message) { |
| 342 return new UninitializedMessageException(message); | 344 return new UninitializedMessageException(message); |
| 343 } | 345 } |
| 344 | 346 |
| 345 /** | 347 /** |
| (...skipping 29 matching lines...) Expand all Loading... |
| 375 | 377 |
| 376 private static void checkForNullValues(final Iterable<?> values) { | 378 private static void checkForNullValues(final Iterable<?> values) { |
| 377 for (final Object value : values) { | 379 for (final Object value : values) { |
| 378 if (value == null) { | 380 if (value == null) { |
| 379 throw new NullPointerException(); | 381 throw new NullPointerException(); |
| 380 } | 382 } |
| 381 } | 383 } |
| 382 } | 384 } |
| 383 } | 385 } |
| 384 } | 386 } |
| OLD | NEW |