Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Protocol Buffers - Google's data interchange format
  // Copyright 2008 Google Inc.  All rights reserved.
  // http://code.google.com/p/protobuf/
  //
  // Redistribution and use in source and binary forms, with or without
  // modification, are permitted provided that the following conditions are
  // met:
  //
  //     * Redistributions of source code must retain the above copyright
 // notice, this list of conditions and the following disclaimer.
 //     * Redistributions in binary form must reproduce the above
 // copyright notice, this list of conditions and the following disclaimer
 // in the documentation and/or other materials provided with the
 // distribution.
 //     * Neither the name of Google Inc. nor the names of its
 // contributors may be used to endorse or promote products derived from
 // this software without specific prior written permission.
 //
 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 package com.google.protobuf;
 
A partial implementation of the MessageLite interface which implements as many methods of that interface as possible in terms of other methods.

Author(s):
kenton@google.com Kenton Varda
 
 public abstract class AbstractMessageLite implements MessageLite {
   protected int memoizedHashCode = 0;
 
   public ByteString toByteString() {
     try {
       final ByteString.CodedBuilder out =
         ByteString.newCodedBuilder(getSerializedSize());
       writeTo(out.getCodedOutput());
       return out.build();
     } catch (IOException e) {
       throw new RuntimeException(
         "Serializing to a ByteString threw an IOException (should " +
         "never happen)."e);
     }
   }
 
   public byte[] toByteArray() {
     try {
       final byte[] result = new byte[getSerializedSize()];
       final CodedOutputStream output = CodedOutputStream.newInstance(result);
       writeTo(output);
       output.checkNoSpaceLeft();
       return result;
     } catch (IOException e) {
       throw new RuntimeException(
         "Serializing to a byte array threw an IOException " +
         "(should never happen)."e);
     }
   }
 
   public void writeTo(final OutputStream outputthrows IOException {
     final int bufferSize =
         CodedOutputStream.computePreferredBufferSize(getSerializedSize());
     final CodedOutputStream codedOutput =
         CodedOutputStream.newInstance(outputbufferSize);
     writeTo(codedOutput);
     codedOutput.flush();
   }
 
   public void writeDelimitedTo(final OutputStream outputthrows IOException {
     final int serialized = getSerializedSize();
     final int bufferSize = CodedOutputStream.computePreferredBufferSize(
         CodedOutputStream.computeRawVarint32Size(serialized) + serialized);
     final CodedOutputStream codedOutput =
         CodedOutputStream.newInstance(outputbufferSize);
     codedOutput.writeRawVarint32(serialized);
     writeTo(codedOutput);
     codedOutput.flush();
   }


  
Package private helper method for AbstractParser to create UninitializedMessageException.
    return new UninitializedMessageException(this);
  }
  protected static void checkByteStringIsUtf8(ByteString byteString)
      throws IllegalArgumentException {
    if (!byteString.isValidUtf8()) {
      throw new IllegalArgumentException("Byte string is not UTF-8.");
    }
  }

  
A partial implementation of the Message.Builder interface which implements as many methods of that interface as possible in terms of other methods.
  @SuppressWarnings("unchecked")
  public static abstract class Builder<BuilderType extends Builder>
      implements MessageLite.Builder {
    // The compiler produces an error if this is not declared explicitly.
    @Override
    public abstract BuilderType clone();
    public BuilderType mergeFrom(final CodedInputStream input)
                                 throws IOException {
      return mergeFrom(input, ExtensionRegistryLite.getEmptyRegistry());
    }
    // Re-defined here for return type covariance.
    public abstract BuilderType mergeFrom(
        final CodedInputStream input,
        final ExtensionRegistryLite extensionRegistry)
        throws IOException;
    public BuilderType mergeFrom(final ByteString data)
        throws InvalidProtocolBufferException {
      try {
        final CodedInputStream input = data.newCodedInput();
        mergeFrom(input);
        input.checkLastTagWas(0);
        return (BuilderType) this;
      } catch (InvalidProtocolBufferException e) {
        throw e;
      } catch (IOException e) {
        throw new RuntimeException(
          "Reading from a ByteString threw an IOException (should " +
          "never happen)."e);
      }
    }
    public BuilderType mergeFrom(
        final ByteString data,
        final ExtensionRegistryLite extensionRegistry)
        throws InvalidProtocolBufferException {
      try {
        final CodedInputStream input = data.newCodedInput();
        mergeFrom(inputextensionRegistry);
        input.checkLastTagWas(0);
        return (BuilderType) this;
      } catch (InvalidProtocolBufferException e) {
        throw e;
      } catch (IOException e) {
        throw new RuntimeException(
          "Reading from a ByteString threw an IOException (should " +
          "never happen)."e);
      }
    }
    public BuilderType mergeFrom(final byte[] data)
        throws InvalidProtocolBufferException {
      return mergeFrom(data, 0, data.length);
    }
    public BuilderType mergeFrom(final byte[] datafinal int off,
                                 final int len)
                                 throws InvalidProtocolBufferException {
      try {
        final CodedInputStream input =
            CodedInputStream.newInstance(dataofflen);
        mergeFrom(input);
        input.checkLastTagWas(0);
        return (BuilderType) this;
      } catch (InvalidProtocolBufferException e) {
        throw e;
      } catch (IOException e) {
        throw new RuntimeException(
          "Reading from a byte array threw an IOException (should " +
          "never happen)."e);
      }
    }
    public BuilderType mergeFrom(
        final byte[] data,
        final ExtensionRegistryLite extensionRegistry)
        throws InvalidProtocolBufferException {
      return mergeFrom(data, 0, data.lengthextensionRegistry);
    }
    public BuilderType mergeFrom(
        final byte[] datafinal int offfinal int len,
        final ExtensionRegistryLite extensionRegistry)
        throws InvalidProtocolBufferException {
      try {
        final CodedInputStream input =
            CodedInputStream.newInstance(dataofflen);
        mergeFrom(inputextensionRegistry);
        input.checkLastTagWas(0);
        return (BuilderType) this;
      } catch (InvalidProtocolBufferException e) {
        throw e;
      } catch (IOException e) {
        throw new RuntimeException(
          "Reading from a byte array threw an IOException (should " +
          "never happen)."e);
      }
    }
    public BuilderType mergeFrom(final InputStream inputthrows IOException {
      final CodedInputStream codedInput = CodedInputStream.newInstance(input);
      mergeFrom(codedInput);
      codedInput.checkLastTagWas(0);
      return (BuilderType) this;
    }
    public BuilderType mergeFrom(
        final InputStream input,
        final ExtensionRegistryLite extensionRegistry)
        throws IOException {
      final CodedInputStream codedInput = CodedInputStream.newInstance(input);
      mergeFrom(codedInputextensionRegistry);
      codedInput.checkLastTagWas(0);
      return (BuilderType) this;
    }

    
An InputStream implementations which reads from some other InputStream but is limited to a particular number of bytes. Used by mergeDelimitedFrom(). This is intentionally package-private so that UnknownFieldSet can share it.
    static final class LimitedInputStream extends FilterInputStream {
      private int limit;
      LimitedInputStream(InputStream inint limit) {
        super(in);
        this. = limit;
      }
      @Override
      public int available() throws IOException {
        return Math.min(super.available(), );
      }
      @Override
      public int read() throws IOException {
        if ( <= 0) {
          return -1;
        }
        final int result = super.read();
        if (result >= 0) {
          --;
        }
        return result;
      }
      @Override
      public int read(final byte[] bfinal int offint len)
                      throws IOException {
        if ( <= 0) {
          return -1;
        }
        len = Math.min(len);
        final int result = super.read(bofflen);
        if (result >= 0) {
           -= result;
        }
        return result;
      }
      @Override
      public long skip(final long nthrows IOException {
        final long result = super.skip(Math.min(n));
        if (result >= 0) {
           -= result;
        }
        return result;
      }
    }
    public boolean mergeDelimitedFrom(
        final InputStream input,
        final ExtensionRegistryLite extensionRegistry)
        throws IOException {
      final int firstByte = input.read();
      if (firstByte == -1) {
        return false;
      }
      final int size = CodedInputStream.readRawVarint32(firstByteinput);
      final InputStream limitedInput = new LimitedInputStream(inputsize);
      mergeFrom(limitedInputextensionRegistry);
      return true;
    }
    public boolean mergeDelimitedFrom(final InputStream input)
        throws IOException {
      return mergeDelimitedFrom(input,
          ExtensionRegistryLite.getEmptyRegistry());
    }

    
Construct an UninitializedMessageException reporting missing fields in the given message.
    protected static UninitializedMessageException
      return new UninitializedMessageException(message);
    }

    
Adds the values to the list. This is a helper method used by generated code. Users should ignore it.

Throws:
java.lang.NullPointerException if any of the elements of values is null. When that happens, some elements of values may have already been added to the result list.
    protected static <T> void addAll(final Iterable<T> values,
                                     final Collection<? super T> list) {
      if (values instanceof LazyStringList) {
        // For StringOrByteStringLists, check the underlying elements to avoid
        // forcing conversions of ByteStrings to Strings.
        list.addAll((Collection<T>) values);
      } else if (values instanceof Collection) {
        checkForNullValues(values);
        list.addAll((Collection<T>) values);
      } else {
        for (final T value : values) {
          if (value == null) {
            throw new NullPointerException();
          }
          list.add(value);
        }
      }
    }
    private static void checkForNullValues(final Iterable<?> values) {
      for (final Object value : values) {
        if (value == null) {
          throw new NullPointerException();
        }
      }
    }
  }
New to GrepCode? Check out our FAQ X