31 package com.google.protobuf;
35 import java.io.FilterInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
61 }
catch (IOException e) {
74 }
catch (IOException e) {
90 final int bufferSize =
101 int getMemoizedSerializedSize() {
102 throw new UnsupportedOperationException();
105 void setMemoizedSerializedSize(
int size) {
106 throw new UnsupportedOperationException();
111 int memoizedSerializedSize = getMemoizedSerializedSize();
112 if (memoizedSerializedSize == -1) {
113 memoizedSerializedSize = schema.getSerializedSize(
this);
114 setMemoizedSerializedSize(memoizedSerializedSize);
116 return memoizedSerializedSize;
120 UninitializedMessageException newUninitializedMessageException() {
121 return new UninitializedMessageException(
this);
125 return "Serializing "
126 + getClass().getName()
129 +
" threw an IOException (should never happen).";
133 throws IllegalArgumentException {
134 if (!byteString.isValidUtf8()) {
135 throw new IllegalArgumentException(
"Byte string is not UTF-8.");
141 protected static <T>
void addAll(
final Iterable<T>
values,
final Collection<? super T> list) {
145 protected static <T>
void addAll(
final Iterable<T>
values,
final List<? super T> list) {
153 @SuppressWarnings(
"unchecked")
156 BuilderType extends
Builder<MessageType, BuilderType>>
160 public abstract BuilderType clone();
169 public abstract BuilderType mergeFrom(
178 input.checkLastTagWas(0);
179 return (BuilderType)
this;
182 }
catch (IOException e) {
183 throw new RuntimeException(getReadingExceptionMessage(
"ByteString"), e);
193 mergeFrom(
input, extensionRegistry);
194 input.checkLastTagWas(0);
195 return (BuilderType)
this;
198 }
catch (IOException e) {
199 throw new RuntimeException(getReadingExceptionMessage(
"ByteString"), e);
205 return mergeFrom(
data, 0,
data.length);
214 input.checkLastTagWas(0);
215 return (BuilderType)
this;
218 }
catch (IOException e) {
219 throw new RuntimeException(getReadingExceptionMessage(
"byte array"), e);
226 return mergeFrom(
data, 0,
data.length, extensionRegistry);
238 mergeFrom(
input, extensionRegistry);
239 input.checkLastTagWas(0);
240 return (BuilderType)
this;
243 }
catch (IOException e) {
244 throw new RuntimeException(getReadingExceptionMessage(
"byte array"), e);
251 mergeFrom(codedInput);
253 return (BuilderType)
this;
260 mergeFrom(codedInput, extensionRegistry);
262 return (BuilderType)
this;
270 static final class LimitedInputStream
extends FilterInputStream {
273 LimitedInputStream(InputStream in,
int limit) {
279 public int available() throws IOException {
280 return Math.min(super.available(), limit);
284 public int read() throws IOException {
288 final int result = super.read();
296 public int read(
final byte[]
b,
final int off,
int len)
throws IOException {
300 len = Math.min(
len, limit);
301 final int result = super.read(
b, off,
len);
309 public long skip(
final long n)
throws IOException {
310 final long result = super.skip(Math.min(
n, limit));
321 final int firstByte =
input.read();
322 if (firstByte == -1) {
326 final InputStream limitedInput =
new LimitedInputStream(
input,
size);
327 mergeFrom(limitedInput, extensionRegistry);
337 @SuppressWarnings(
"unchecked")
340 throw new IllegalArgumentException(
341 "mergeFrom(MessageLite) can only merge messages of the same type.");
344 return internalMergeFrom((MessageType) other);
347 protected abstract BuilderType internalMergeFrom(MessageType
message);
351 + getClass().getName()
354 +
" threw an IOException (should never happen).";
359 if (list instanceof ArrayList &&
values instanceof Collection) {
360 ((ArrayList<T>) list).ensureCapacity(list.size() + ((Collection<T>)
values).size());
362 int begin = list.size();
366 String
message =
"Element at index " + (list.size() -
begin) +
" is null.";
367 for (
int i = list.size() - 1;
i >=
begin;
i--) {
370 throw new NullPointerException(
message);
384 protected static <T>
void addAll(
final Iterable<T>
values,
final Collection<? super T> list) {
395 protected static <T>
void addAll(
final Iterable<T>
values,
final List<? super T> list) {
405 int begin = list.size();
406 for (Object
value : lazyValues) {
409 String
message =
"Element at index " + (lazyList.size() -
begin) +
" is null.";
410 for (
int i = lazyList.size() - 1;
i >=
begin;
i--) {
413 throw new NullPointerException(
message);
422 if (
values instanceof PrimitiveNonBoxingCollection) {
423 list.addAll((Collection<T>)
values);
425 addAllCheckingNulls(
values, list);